7

I have the following schema in AppSync for GraphQL

input CreateTeamInput {
    name: String!
    sport: Sports!
    createdAt: String
}

enum Sports {
    baseball
    basketball
    cross_country
}
type Mutation{
    createTeam(input: CreateTeamInput!): Team
}

However when I try to execute the query using AWS Amplify library via

export const CreateTeam = `mutation CreateTeam($name: String!, $sport: String!){
  createTeam(input:{name:$name, sport:$sport}) {
    id,
    name,
    sport
  }
}
`;

....

API.graphql(graphqlOperation(CreateTeam, this.state))

I get the following error: Validation error of type VariableTypeMismatch: Variable type doesn't match.

How can I update my code to work with this enum type?

Yves M.
  • 29,855
  • 23
  • 108
  • 144
Dan Ramos
  • 1,092
  • 2
  • 19
  • 35

2 Answers2

8

CreateTeamInput.sport field type is an enum, hence your $sport variable must be an enum.

Try changing your query to:

export const CreateTeam = `mutation CreateTeam($name: String!, $sport: Sports!){
  createTeam(input:{name:$name, sport:$sport}) {
    id,
    name,
    sport
  }
};

Note: As a convention, prefer using uppercase for your enum values so it's easy to tell them apart from strings.

enum SPORTS {
    BASEBALL
    BASKETBALL
    CROSS_COUNTRY
}
Tinou
  • 5,908
  • 4
  • 21
  • 24
  • 1
    Well this was a brain fart on my part. I was under the assumption since the schema type of "Sports" existed just on my graphql server, that the client query would have idea what to do with `$sports: Sports!`, but it worked just fine. Thank you! – Dan Ramos Jun 12 '18 at 17:49
1

$sport needs to be a Sports type not a String

Vasileios Lekakis
  • 5,492
  • 2
  • 15
  • 17
  • 4
    How do I actually specify a Sports type though? I was able to create it no problem in my schema.graphql file used on the server side (in the code snippet above enum Sports{...}), but how do I create it from a client request? – Dan Ramos Jun 12 '18 at 14:40
  • I couldn't add a comment on multiple lines so I created an answer above. @DanRamos – Tinou Jun 12 '18 at 17:21