0
votes

I get this error while querying:

"Cannot return null for non-nullable field Transaction.createdAt."

This is the query:

query getMyTransactions {
  getMyTransactions {
    id
    createdAt
  }
}

The schema for this query is:

extend type Transaction @key(fields: "id") {
  id: ID! @external
}

type Query {
  getMyTransactions: [Transaction!]!
}

And the other schema has Transaction type:

type Transaction @key(fields: "id") {
  id: ID!
  createdAt: String!
}

What's the problem?

EDIT: If I query for:

getMyTransactions {
  id
}

Works ok I and get all the id of the query, but if I include another attribute the query fails.

1

1 Answers

1
votes

In simple words - you declare createdAt type to be non-null value, but somewhere in your data createdAt is null.

createdAt: String!

! after the type name. This means that our server always expects to return a non-null value for this field, and if it ends up getting a null value that will actually trigger a GraphQL execution error, letting the client know that something has gone wrong. GraphQl docs

Example

For this remote/local data (createdAt missing from the first object):

const Transaction = [
  {
    id: "1",
    /* createdAt: "01/09/2020" */ /* missing from the data */
  },
  {
    id: "2",
    createdAt: "02/09/2020"
  }
];

Executing query

query{
  getMyTransactions {
    id
    createdAt
    }
}

throw error:

"message": "Cannot return null for non-nullable field Transaction.createdAt.",

enter image description here

To solve this:

  • Option 1: Add some resolver logic and/or add validation related to your required data.
  • Option 2: Remove the require from createdAt (return null allowed): enter image description here