user9526901
user9526901

Reputation:

"Cannot return null for non-nullable type: 'Person' within parent 'Messages' (/getMessages/sendBy)" in GraphQL SDL( aws appsync)

Iam new to graphql.Iam implementing a react-native app using aws appsync.Following is the code i have written in schema

type Messages {
id: ID!
createdAt: String!
updateAt: String!
text: String!
sendBy: Person!
    @relation(name: "UserMessages")}

type Person {
id: ID!
createdAt: String!
updateAt: String!
name: String!
messages: [Messages!]!
    @relation(name: "UserMessages")}

When i tried to query the sendBy value it is giving me an error saying

  query getMessages{
  getMessages(id : "a0546b5d-1faf-444c-b243-fab5e1f47d2d") {
    id
    text
    sendBy {
      name
    }
  }
}


 {
  "data": {
    "getMessages": null
  },
  "errors": [
    {
      "path": [
        "getMessages",
        "sendBy"
      ],
      "locations": null,
      "message": "Cannot return null for non-nullable type: 'Person' within parent 'Messages' (/getMessages/sendBy)"
    }
  ]
}

Am not understanding that error please help me.Thanks!! in Advance

Upvotes: 6

Views: 12724

Answers (5)

Manoranjan Kumar
Manoranjan Kumar

Reputation: 55

I had a similar issue.

for me the problem was lying with the return type of the schema . As i was doing a query with PK on dynamodb table ..it was returning a list of items or data you can say . but in my schema i had a schema define as a singular struct format .

Error was resolved when i just made the return type in schema as list of items .

like

type mySchema { 
[ID]
}

instead of type mySchema

{ 
id : ID!
name : String!
details : String!
}

This error is thrown for multiple reasons . so your reason could be else but still i just posted one of the scenarios.

Upvotes: 0

Toufeeq
Toufeeq

Reputation: 406

This might sound silly, but still, developers do this kind of mistakes so did I. In subscription, the client can retrieve only those fields which are outputted in the mutation query. For example, if your mutation query looks like this:

mutation newMessage { addMessage(input:{ field_1: "", field_2: "", field_n: "", }){ field_1, field_2 } }

In the above mutation since we are outputting only field_1 & field_2. A client can retrieve the only subset of these fields.

So if in the schema, for a subscription if you have defined field_3 as required(!), and since you are not outputting field_3 in the above mutation, this will throw the error saying Cannot return null for non-nullable type: field_3.

Upvotes: 11

Aecio Levy
Aecio Levy

Reputation: 155

I had a similar issue.

What happened to me was a problem with an update resolver. I was updating a field that was used as GSI (Global Secondary Index). But I was not updating the GSI, so when query by GSI the index exists but the key for that attribute had changed.

If you are using Dynamo DB, you can start debugging there. You can check the item and see if you have any reference to the primary key or the indexes.

Upvotes: 0

Clark Burns
Clark Burns

Reputation: 1

I encountered a similar issue while working on my setup with CloudFormation. In my particular situation I didn't configure the Projection correctly for the Global Secondary Indexes. Since the attributes weren't projected into the index, I was getting an ID in the response but null for all other values. Updating the ProjectionType to 'ALL' resolved my issue. Not to say that is the "correct" setting but for my particular implementation it was needed.

More on Global Secondary Index Projection for CloudFormation can be found here: https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-dynamodb-projectionobject.html

Attributes that are copied (projected) from the source table into the index. These attributes are additions to the primary key attributes and index key attributes, which are automatically projected.

Upvotes: 0

Shankar Raju
Shankar Raju

Reputation: 4546

Looks like the path [getMessages, sendBy] is resolving to a null value, and your schema definition (sendBy: Person!) says sendBy field cannot resolve to null. Please check if a resolver is attached to the field sendBy in type Messages.

If there is a resolver attached, please enable CloudWatch logs for this API (This can be done on the Settings page in Console, select ALL option). You should be able to check what the resolved Request/Response mapping was for the path [getMessages, 0, sendBy].

Upvotes: 2

Related Questions