Vladislav
Vladislav

Reputation: 3

Is it possible to ignore required type used with @include in graphql

Is it possible to ignore the required type (in this situation, type input of createCarEvent contains variable $from as String!) when using @include? I tried to use $from as String instead of String! but it led to an exception:

Variable "$from" of type "String" used in position expecting type "String!"

The $isNeedCreateCarEvent checks if two variables ($from and $to) exist.

mutation rejectBooking(... $from: String, $to: String, $isNeedCreateCarEvent: Boolean!) {
...
createCarEvent(
  input: {
    ...
    from: $from
    to: $to
  }
) @include(if: $isNeedCreateCarEvent) {
  ...eventItem
}

The scheme:

    input CarEventInput {
    ...
    from: String!
    to: String
  }

Upvotes: 0

Views: 886

Answers (1)

Bergi
Bergi

Reputation: 664425

There's no way for GraphQL to know that $from is always supplied if $isNeedCreateCarEvent is true, the type declarations cannot express that (and your calling code cannot guarantee that).

You better use two separate mutations, one with the createCarEvent and one without, if you want to make $from and $to optional.

If that would mean too much repetition for you (because of the rest of the query), the only way is to declare the parameters as non-null and pass empty strings as dummy values when using $isNeedCreateCarEvent = false.

Upvotes: 1

Related Questions