Search code examples
amazon-web-servicesgraphqlaws-amplifyaws-appsync

AppSync and GraphQL Enum mutations


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?


Solution

  • 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
    }