Graphql-code-generator: Non-Anonymous Query without query keyword doesn't throw an error

Created on 12 Aug 2019  路  10Comments  路  Source: dotansimha/graphql-code-generator

Describe the bug

When using the typescript-operations plugin, if a typescript file contains a graphql query that is missing the query label (making it invalid), then no types will be generated for that file and no error is thrown.

To Reproduce

_I attempted to make a codesandbox example here: https://codesandbox.io/s/graphql-codegen-issue-template-mueku_

Steps to reproduce the behavior:

In the provided codesandbox issue template, create a typescript file containing a query like so:

const validQuery = gql`
  query ValidGetUser {
    user("1") {
      id
    }
  }
`;

const const invalidQuery = gql`
  InvalidGetUser {
    user("1") {
      id
    }
  }
`;

Attempt to generate types for this file. No types will be generated. No error will be thrown.

Expected behavior

An error should be thrown indicating that the file contains an invalid query.

Environment:

  • OS: MacOS X
  • @graphql-codegen/...: 1.5.0
  • NodeJS: 10.16.0

Additional context

bug core waiting-for-release

Most helpful comment

@dotansimha I am not sure if we should assume that is query, because this kind of document fails on graphql-js's parse like Unexpected Name: SomeUnnamed.
The expected behavior should be to throw that error in codegen. What do you think?
See failing PR: https://github.com/dotansimha/graphql-code-generator/pull/2372

All 10 comments

Hi @thefliik !
A query without the query keyword is valid in GraphQL, but the codegen shouldn't skip it, and should tread it just like any other query.

@dotansimha I am not sure if we should assume that is query, because this kind of document fails on graphql-js's parse like Unexpected Name: SomeUnnamed.
The expected behavior should be to throw that error in codegen. What do you think?
See failing PR: https://github.com/dotansimha/graphql-code-generator/pull/2372

From graphqljs.org (emphasis mine)

The operation type is either query, mutation, or subscription and describes what type of operation you're intending to do. The operation type is required unless you're using the query shorthand syntax, in which case you can't supply a name or variable definitions for your operation.

It appears that

const const invalidQuery = gql`
  InvalidGetUser {
    user("1") {
      id
    }
  }
`;

Is invalid. What would be valid is

const const validQuery = gql`
  {
    user("1") {
      id
    }
  }
`;

@thefliik That one is working, right? If it is, we can close the issue.

@ardatan

That one is working, right

No. Why do you say that? Why was this closed? The original issue remains. I was merely refuting @dotansimha's statement that

A query without the query keyword is valid in GraphQL

That statement is incorrect. A query without the query keyword is only valid in GraphQL if it is an anonymous query (as cited in my previous comment).

This is not an anonymous query, and it is also lacking the query keyword. Hence invalid.

const const invalidQuery = gql`
  InvalidGetUser {
    user("1") {
      id
    }
  }
`;

Unless I'm missing something, this issue should be re-opened.

@thefliik @dotansimha So the problem here is to throw an error in case of an invalid query like this;

  InvalidGetUser {
    user("1") {
      id
    }
  }

That makes the title wrong because the problem is not to be picked up by codegen but error handling.
So the title should be Non-Anonymous Query without query keyword doesn't throw an error, right?

@ardatan you are correct. Title changed 馃憤

@thefliik Thank you :) I was just trying to get on the same page
@dotansimha Do you think it is related to graphql-toolkit or codegen?

Fixed in [email protected], updated in: https://github.com/dotansimha/graphql-code-generator/pull/2384 , now every parsing error will be thrown from graphql-toolkit, and the codegen will handle it instead of ignoring the file.

Fixed in 1.6.0

Was this page helpful?
0 / 5 - 0 ratings