4
votes

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?

2
Can you provide your mutation definition? We don't know here what the createTeam mutation expects - Vasileios Lekakis
@VasileiosLekakis post has been updated with the mutation - Dan Ramos

2 Answers

5
votes

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
}
0
votes

$sport needs to be a Sports type not a String