AppSync 和 GraphQL 枚举突变



我在 AppSync for GraphQL 中有以下架构

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

但是,当我尝试通过以下方式使用 AWS Amplify 库执行查询时

export const CreateTeam = `mutation CreateTeam($name: String!, $sport: String!){
createTeam(input:{name:$name, sport:$sport}) {
id,
name,
sport
}
}
`;
....
API.graphql(graphqlOperation(CreateTeam, this.state))

我收到以下错误:Validation error of type VariableTypeMismatch: Variable type doesn't match

如何更新我的代码以使用此枚举类型?

CreateTeamInput.sport字段类型是枚举,因此$sport变量必须是枚举。

尝试将查询更改为:

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

注意:按照惯例,最好对枚举值使用大写字母,以便于将它们与字符串区分开来。

enum SPORTS {
BASEBALL
BASKETBALL
CROSS_COUNTRY
}

$sport必须是运动类型而不是字符串

最新更新