Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validate GraphQL queries in gatsby-node.js with Relay Compiler #1689

Closed
3axap4eHko opened this issue Aug 2, 2017 · 9 comments
Closed

Validate GraphQL queries in gatsby-node.js with Relay Compiler #1689

3axap4eHko opened this issue Aug 2, 2017 · 9 comments

Comments

@3axap4eHko
Copy link
Contributor

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

@KyleAMathews
Copy link
Contributor

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

@KyleAMathews KyleAMathews changed the title Unknown ast kind: NullValue Validate GraphQL queries in gatsby-node.js with Relay Compiler Aug 2, 2017
@KyleAMathews
Copy link
Contributor

Updated the issue title to reflect this :-)

@cr101
Copy link

cr101 commented Aug 3, 2017

Just sharing this announcement about a new GraphQL tool

@nkohari
Copy link
Contributor

nkohari commented Aug 16, 2017

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?

@jquense
Copy link
Contributor

jquense commented Aug 16, 2017

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)

@nkohari
Copy link
Contributor

nkohari commented Aug 16, 2017

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

let compilerContext = new RelayCompilerContext(this.schema)
try {
compilerContext = compilerContext.addAll(
ASTConvert.convertASTDocuments(this.schema, documents, validationRules)
)
} catch (error) {
this.reportError(graphqlError(namePathMap, nameDefMap, error))
return compiledNodes
}

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?

@nickeblewis
Copy link

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"

facebook-github-bot pushed a commit to facebook/relay that referenced this issue Aug 17, 2017
Summary:
Hi there! This patch is the result of debugging gatsbyjs/gatsby#1689. It seems like the switch statement inside of `RelayParser._transformValue()` was missing a case for `NullValue`, so queries which contained a `null` literal would throw an error.

Adding a case fixes the downstream issue that some folks have experienced with Gatsby, but I'm not sure if there's a better way to solve this problem.
Closes #2035

Reviewed By: leebyron

Differential Revision: D5650583

Pulled By: kassens

fbshipit-source-id: 9e76f553b35ab106711930150045d285670d36e2
@tim-field
Copy link

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

@KyleAMathews
Copy link
Contributor

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants