question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Processor `@graphql-eslint/graphql` might fail to process due to GraphQL comments.

See original GitHub issue

Intro Not sure if this is really graphql-eslint or graphql-tag-pluck issue, but I’ve found a clear reproducible case using this repository, so I thought, I’ll report it here. Also, I’ve made a repository where the issue is reproducible.

Describe the bug

It seems that if processor: '@graphql-eslint/graphql' option is used together with .graphql or .gql files and if those files contain comments, processor might fail to process the file.

E.g.

  • This one is processed without any issues:
    fragment UserFields on User {
      id
      name
    }
    
  • This one is processed as well without any issues:
    # Just any comment without any other file mentions
    fragment UserFields on User {
      id
      name
    }
    
  • This file fails:
    # Lets mention another file, e.g.: User1.fragment.gql in this comment
    fragment UserFields on User {
      id
      name
    }
    

To Reproduce

Use this repository: https://github.com/lukaskl/graphql-eslint-parse-issue

git clone git@github.com:lukaskl/graphql-eslint-parse-issue.git
cd graphql-eslint-parse-issue
yarn install --frozen-lockfile
yarn eslint .

✅ Expected result

The output should be something like this:

➜  graphql-eslint-parse-issue yarn eslint .
yarn run v1.15.2

***/src/User0.fragment.gql
  3:4  warning  This field is marked as deprecated in your GraphQL schema (reason: Use `firstName` instead.)  @graphql-eslint/no-deprecated

***/src/User1.fragment.gql
  4:4  warning  This field is marked as deprecated in your GraphQL schema (reason: Use `firstName` instead.)  @graphql-eslint/no-deprecated

***/src/User2.fragment.gql
  4:4  warning  This field is marked as deprecated in your GraphQL schema (reason: Use `firstName` instead.)  @graphql-eslint/no-deprecated

✖ 3 problems (0 errors, 3 warnings)

🔴 Actual result

➜  graphql-eslint-parse-issue yarn eslint .
yarn run v1.15.2
$ ***/node_modules/.bin/eslint .
[graphql-eslint/processor]: Unable to process file "***/src/User2.fragment.gql":  SyntaxError: Unexpected token (1:0)
    at Object._raise (***/node_modules/@graphql-tools/graphql-tag-pluck/node_modules/@babel/parser/lib/index.js:776:17)
    at Object.raiseWithData (***/node_modules/@graphql-tools/graphql-tag-pluck/node_modules/@babel/parser/lib/index.js:769:17)
    at Object.raise (***/node_modules/@graphql-tools/graphql-tag-pluck/node_modules/@babel/parser/lib/index.js:737:17)
    at Object.unexpected (***/node_modules/@graphql-tools/graphql-tag-pluck/node_modules/@babel/parser/lib/index.js:9253:16)
    at Object.parseExprAtom (***/node_modules/@graphql-tools/graphql-tag-pluck/node_modules/@babel/parser/lib/index.js:10743:20)
    at Object.parseExprAtom (***/node_modules/@graphql-tools/graphql-tag-pluck/node_modules/@babel/parser/lib/index.js:4996:20)
    at Object.parseExprSubscripts (***/node_modules/@graphql-tools/graphql-tag-pluck/node_modules/@babel/parser/lib/index.js:10318:23)
    at Object.parseUpdate (***/node_modules/@graphql-tools/graphql-tag-pluck/node_modules/@babel/parser/lib/index.js:10298:21)
    at Object.parseMaybeUnary (***/node_modules/@graphql-tools/graphql-tag-pluck/node_modules/@babel/parser/lib/index.js:10276:23)
    at Object.parseExprOps (***/node_modules/@graphql-tools/graphql-tag-pluck/node_modules/@babel/parser/lib/index.js:10141:23) {
  loc: Position { line: 1, column: 0 },
  pos: 0
}

***/src/User0.fragment.gql
  3:4  warning  This field is marked as deprecated in your GraphQL schema (reason: Use `firstName` instead.)  @graphql-eslint/no-deprecated

***/src/User1.fragment.gql
  4:4  warning  This field is marked as deprecated in your GraphQL schema (reason: Use `firstName` instead.)  @graphql-eslint/no-deprecated

***/src/User2.fragment.gql
  4:4  warning  This field is marked as deprecated in your GraphQL schema (reason: Use `firstName` instead.)  @graphql-eslint/no-deprecated

✖ 3 problems (0 errors, 3 warnings)

Environment:

  • OS: macOS Catalina
  • @graphql-eslint/eslint-plugin@: 0.9.1
  • NodeJS: v14.13.1

Additional context

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
B2o5Tcommented, May 11, 2021

If you have documents in both javascript files and .graphql files, your config need to be looks like this

module.exports = {
  env: {
    es6: true
  },
  overrides: [
    {
      files: ['*.js'],
      processor: '@graphql-eslint/graphql',
    },
    {
      files: ['*.graphql', '*.gql'],
      parser: '@graphql-eslint/eslint-plugin',
      parserOptions: {
        schema: './**/schema.gql',
      },
      plugins: ['@graphql-eslint'],
      rules: {
        '@graphql-eslint/no-deprecated': 'warn',
      },
    },
  ],
}

and try to add this fragment to javascript file

const someFragment = gql`
  # Just some another comment
  fragment SomeUserFields on User {
    id
    name
  }
`

We’ll give me this output

/Users/dimitri/graphql-eslint-parse-issue/src/User0.fragment.gql
  3:4  warning  This field is marked as deprecated in your GraphQL schema (reason: Use `firstName` instead.)  @graphql-eslint/no-deprecated

/Users/dimitri/graphql-eslint-parse-issue/src/User1.fragment.gql
  4:4  warning  This field is marked as deprecated in your GraphQL schema (reason: Use `firstName` instead.)  @graphql-eslint/no-deprecated

/Users/dimitri/graphql-eslint-parse-issue/src/User2.fragment.gql
  4:4  warning  This field is marked as deprecated in your GraphQL schema (reason: Use `firstName` instead.)  @graphql-eslint/no-deprecated

/Users/dimitri/graphql-eslint-parse-issue/src/index.js
  4:6  warning  This field is marked as deprecated in your GraphQL schema (reason: Use `firstName` instead.)  @graphql-eslint/no-deprecated

✖ 4 problems (0 errors, 4 warnings)
1reaction
lukasklcommented, May 18, 2021

I can confirm that, moving processor: '@graphql-eslint/graphql', inside of the overrides works well 👍 Thank you!

And if there is no wish to do anything here more (e.g. documentation or examples update), then we can simply close this ticket.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Processor `@graphql-eslint/graphql` might fail to process due ...
gql files and if those files contain comments, processor might fail to process the file. E.g.. This one is processed without any issues:....
Read more >
graphql-eslint - Bountysource
Processor `@graphql-eslint/graphql` might fail to process due to GraphQL comments. $ 0. Created 1 year ago in dotansimha/graphql-eslint with 5 comments.
Read more >
Full Stack Error Handling with GraphQL and Apollo
As a package author, I would like to be able to write components/middleware that could handle errors automatically in the most appropriate ...
Read more >
@graphql-eslint/eslint-plugin - npm
If you are using GraphQL fragments in separate files, some rules might yield incorrect results, due the missing information. To workaround that, ...
Read more >
Linting GraphQL Schema and queries - Rule of Tech
Here are some pointers to tools you can use to start linting your GraphQL schema. Resources: eslint-plugin-graphql: Checks tagged query strings ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found