Contentful logo

Contentful Community

GraphQL returning an error if the field is not populated

I’m having problems with structuring a content and using it using gatsby/graphql.

The scenario is this:

  1. I have a Product content Model that has an optional field that is a reference to another field, let’s call this optional field “Color”.
  2. Let’s say i create a product, and i leave “Color” empty.
  3. Now i go on my graphql queries i add color to my query fragment.
  4. If i query “All products” everything is good, i’ll get an array with all the products and the color field set to null
  5. instead if a query the single product Product($id) and I add in the query color, it fails compiling the query, cause it can’t find Color in the schema. It seems that contentful not returning empty field while querying a single resource. I would like to have all the fields available in my schema but if not set they should be null.

do you have any clue on how to solve this stuff?

Hi @Scerelli,
Have you tried to query the single product the same way you query the ‘All products’ collection by filtered by the specific entry-id?

Maybe in that case it should give you the same schema with the field set to null.