【问题标题】:AWS amplify graphql mutation: Cannot return null for non-nullable fieldAWS放大graphql突变:不能为不可为空的字段返回null
【发布时间】:2023-03-28 04:01:02
【问题描述】:

我对 GraphQL 和 AWS amplify 都很陌生,所以这可能是一个新手问题。

我在 schema.graphql 中定义了下面列出的类型。如果我使用带有id: ID! 的类型创建一个突变,我会得到一个Cannot return null for non-nullable field Vocabulary.id

如何在 AWS amplify graphql 中指定一个字段应该是 identity 字段?为identity 字段指定id: ID!,在这个AWS amplify workshop 中似乎工作正常。

~\amplify\backend\api\vidaudtranscription\schema.graphql

type Vocabulary @model 
@key(fields:["userId"])
@auth(rules: [{allow: owner}])
{
    id: ID!
  userId: String!
  vocabularies: [String!]!
}

变异请求:

mutation MyMutation {
  createVocabulary(input: {userId: "abc", vocabularies: ["123", "456"]}) {
    id
    owner
    userId
    vocabularies
  }
}

突变反应:

{
  "data": {
    "createVocabulary": null
  },
  "errors": [
    {
      "message": "Cannot return null for non-nullable field Vocabulary.id.",
      "locations": [
        {
          "line": 5,
          "column": 5
        }
      ],
      "path": [
        "createVocabulary",
        "id"
      ]
    }
  ]
}

【问题讨论】:

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


    【解决方案1】:

    您必须在 input 参数中提供 id

    createVocabulary(input: {userId: "abc", vocabularies: ["123", "456"]})
                            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    

    这个错误有点难以阅读,但它包含了你需要破译它的所有信息:

    • "Cannot return null for non-nullable field Vocabulary.id." 抱怨 Vocabulary.id(在您正在创建的词汇对象中)不能为空,但它是
    • "path": ["createVocabulary", "id"] 是缺失字段的位置,即createVocabulary 结构中的“id”字段

    (我在这里忽略了一些细节。从技术上讲,错误是由于 resolver 未能序列化响应对象,而不是解释输入对象。但如果您提供输入对象中的必填字段,其余的应该可以工作。)

    【讨论】:

      猜你喜欢
      • 2021-10-07
      • 2019-10-25
      • 2019-07-08
      • 2019-05-28
      • 2021-08-18
      • 2021-11-27
      • 2021-10-24
      • 2021-08-06
      • 2021-10-29
      相关资源
      最近更新 更多