Gatsby: GraphQL cannot query where field is null for all entities

Created on 27 Jul 2018  路  7Comments  路  Source: gatsbyjs/gatsby

I'm not sure if this is a bug, or intended behaviour, but if I were to create nodes with createNode where each node was something like this:

{
    name: 'Chris',
    age: null
},
{
    name: 'Christopher',
    age: null
},
{
    name: null,
    age: null
}

The field name would exist within GraphQL, but the field age would not because it has a value of null for every node that's created. Is this something that can be worked around?

I have a list of hockey matches, which take a hometeam, awayteam and a winningteam. The hockey season doesn't start for another month, so no matches have been played yet, hence no teams have won. This causes the winningteam field to be omitted from GraphQL, and then my GraphQL queries break.

I'm trying to use them within a filter, and also the body of the query.

question or discussion

Most helpful comment

You can use the new schema customization API to tell Gatsby that the fields exist https://www.gatsbyjs.org/blog/2019-03-04-new-schema-customization/

eg

type person {
  name: String
  age: String

}

All 7 comments

This is limitation of schema creation - to create field Gatsby would need to know type of it, which Gatsby can't infer from null so it skips those fields. You can potentially workaround it by setting to empty string instead of null, which gatsby will pick up as string field.

Just found this which also happens with gatsby-source-drupal.

I was thinking of making a transformer to change null fields to empty strings, however this is probably not a nice generalization.

It even happens for gatsby-source-wordpress

Wouldn't it make more sense to default it to a string type?

You can use the new schema customization API to tell Gatsby that the fields exist https://www.gatsbyjs.org/blog/2019-03-04-new-schema-customization/

eg

type person {
  name: String
  age: String

}

Thanks @jonlow that was easier than I expected.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

magicly picture magicly  路  3Comments

kalinchernev picture kalinchernev  路  3Comments

brandonmp picture brandonmp  路  3Comments

mikestopcontinues picture mikestopcontinues  路  3Comments

timbrandin picture timbrandin  路  3Comments