2

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.

pmiranda
  • 7,602
  • 14
  • 72
  • 155

1 Answers1

0

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
Ezra Siton
  • 6,887
  • 2
  • 25
  • 37
  • 2
    Thats what I thought but checking the data, createdAt exists on every item of the table. Now, if I put `createdAt: String` on Transaction type, I get each createdAt as null. I think I have a mistake on how Im using the federation options (@key, @external, etc) – pmiranda Sep 02 '20 at 00:06