繁体   English   中英

AWS 放大 graphql 突变:无法为不可空字段返回 null

[英]AWS amplify graphql mutation: Cannot return null for non-nullable field

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

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

如何在 AWS amplify graphql 中指定一个字段应该是一个identity字段? 指定id: ID! 对于identity字段,在这个AWS 放大研讨会中似乎工作正常。

~\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"
      ]
    }
  ]
}

您必须在input参数中提供id

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

该错误有点难以阅读,但它包含解密它所需的所有信息:

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

(I'm glossing over some of the details here. To be technically correct the error is from the resolver failing to serialize the response object, rather than interpret the input object. But if you provide the required fields in your input object the rest should工作。)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM