GraphQL mutation issue with Enum

39 Views Asked by At

I have object something like below:

type SomeEventInput {
    event: Event 
}

type Event {
    type: EventType! 
}
    
enum EventType {
    MORNING
    EVENING
    AFTERNOON
    MIDNIGHT
}

Mutation.SomeEvent(input: SomeEventInput!): Boolean!

What I am trying to test is something like:

mutation SomeEvent($input: SomeEventInput!) {
    SomeEventInput(input: {Event: {type: MORNING}})
}

But it is showing the following error:

"message": "Variable \"$input\" is never used in operation \"SomeEvent\".",
1

There are 1 best solutions below

0
Michel Floyd On

You want to use an input type to define the parameters of a mutation rather than just a type.

Then like @loganfsmyth points out, you want to actually use the variable you define.

Server:

type Event {
  id: ID!
  name: String!
  type: EventType!
}

enum EventType {
  MORNING
  EVENING
  AFTERNOON
  MIDNIGHT
}

input EventInput {
  name: String!
  type: EventType!
}

type Mutation {
  CreateEvent(input: EventInput!): Event
}

Client:

mutation myMutation ($input: EventInput) {
  CreateEvent(input: $input) {
    id
    name
    type
  }
}

Then pass a variable into the mutation named input with the name and type fields. GraphQL will error if type has a value that is not included in your enum.