5

我在 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

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

4

2 回答 2

6

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
}
于 2018-06-12T17:20:54.923 回答
0

$sport 需要是 Sports 类型而不是 String

于 2018-06-11T21:50:55.713 回答