简体   繁体   中英

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

I am fairly new to both GraphQL and AWS amplify so this might be a newbie question.

I have defined the type listed below in schema.graphql. If I create a mutation using a type with id: ID! , I get a Cannot return null for non-nullable field Vocabulary.id .

How do I specify a field should be an identity field in AWS amplify graphql? specifying id: ID! for an identity field, in this AWS amplify workshop seems to work fine.

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

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

Mutation Request:

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

Mutation Response:

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

You must provide id in your input argument:

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

The error is a bit hard to read but it contains all the information you need to decipher it:

  • "Cannot return null for non-nullable field Vocabulary.id." is complaining that Vocabulary.id (in the Vocabulary object you're creating) can't be null, but it is
  • "path": ["createVocabulary", "id"] is the location of the missing field, ie the "id" field in the createVocabulary structure

(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 work.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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