Gatsby: Validate GraphQL queries in gatsby-node.js with Relay Compiler

Created on 2 Aug 2017  路  9Comments  路  Source: gatsbyjs/gatsby

If I put in a page template graphql filter like:

filter: { frontmatter: { date: { ne: null } } }

it throws an error Unknown ast kind: NullValue But it works in the gatsby-node.js with createPages method

All 9 comments

We use the Relay Compiler to validate queries in pages so we get more warnings there. Would like to do the same validation for queries written elsewhere e.g. in gatsby-node.js

Updated the issue title to reflect this :-)

Just sharing this announcement about a new GraphQL tool

I ran into this problem as well when trying to get a list of all blog posts without the static pages ("about me", etc.) Here's the query:

{
  allMarkdownRemark(filter:{ frontmatter: { date: { ne: null }}}) {
    edges {
      node {
        frontmatter {
          title
          date
        }
      }
    }
  }
}

This works as expected in GraphiQL, but returns the Unknown ast kind: NullValue error in the site itself. I'm not sure that this is a problem with validation, because the query seems to be correct. Is there another way to go about this?

I'm not sure actually where the null literal error is coming from. The null literal is a fairly new addition to the GQL language, so it's possible some place is using an older version of graphql, or the Relay compiler is intentionally protecting against it (which we should change)

I think you're right. As best as I can tell, the error seems to be coming from the Relay compiler, from this block:

https://github.com/gatsbyjs/gatsby/blob/f8956a6449475c1dc364f328ab173857b4e9ebcd/packages/gatsby/src/internal-plugins/query-runner/query-compiler.js#L123-L131

Tracking it down in the Relay source, it looks like RelayParser._transformValue() is the culprit:

https://github.com/facebook/relay/blob/23864eeddc915f119f7b0c698a3d48ceab7488f9/packages/relay-compiler/graphql-compiler/core/RelayParser.js#L735

This switch statement doesn't contain a case for NullValue, but if I add one like this:

...
switch (ast.kind) {
  case 'NullValue':
    return {
      kind: 'Literal',
      metadata: null,
      value: null
    };
  ...
}

The error no longer occurs, and the query runs as expected. I'm not very familiar with the inner-workings of GraphQL and Relay, but it seems like this is an issue in Relay instead, right?

Will be keen to know how this pans out, as I tried the same thing today and having to work around it at the moment by ensuring my data has no null paths!! However there are occasions where I may wish to set them as null, for example, to signify that they are not "published"

For those finding this outside of gatsby, the issues has been fixed and merged into relay and realy-compiler ^1.3.0

Sweet! Thanks @nkohari for fixing the bug upstream!!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

rossPatton picture rossPatton  路  3Comments

dustinhorton picture dustinhorton  路  3Comments

timbrandin picture timbrandin  路  3Comments

Oppenheimer1 picture Oppenheimer1  路  3Comments

benstr picture benstr  路  3Comments