【问题标题】:AppSync and GraphQL Enum mutationsAppSync 和 GraphQL 枚举突变
【发布时间】:2018-11-21 03:51:45
【问题描述】:

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

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

【问题讨论】:

  • 你能提供你的突变定义吗?我们在这里不知道 createTeam 突变期望什么
  • @VasileiosLekakis 的帖子已更新为突变

标签: amazon-web-services graphql aws-amplify aws-appsync


【解决方案1】:

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

【讨论】:

  • 我如何实际指定运动类型?我能够在服务器端使用的 schema.graphql 文件中创建它没有问题(在 enum Sports{...} 上方的代码 sn-p 中),但是如何从客户端请求创建它?
  • 我无法在多行上添加评论,所以我在上面创建了一个答案。 @DanRamos
【解决方案2】:

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
}

【讨论】:

  • 好吧,这对我来说是个脑残。我的假设是因为“体育”的模式类型只存在于我的 graphql 服务器上,客户端查询会知道如何处理$sports: Sports!,但它工作得很好。谢谢!
猜你喜欢
  • 2020-05-30
  • 2020-11-23
  • 2021-06-23
  • 2021-04-24
  • 2020-07-15
  • 2018-12-24
  • 2019-11-24
  • 2020-03-21
  • 1970-01-01
相关资源
最近更新 更多