graphql-eslint: Cannot query field "admin_notification" on type "Subscription" when multiple projects defined in .graphqlrc.yml

Describe the bug

Here is my .graphqlrc.yml:

projects:
  first:
    schema: ./first.graphql
    documents: ./src/first-gql.js
  second:
    schema: ./second.graphql
    documents: ./src/second-gql.js

Here is my ./src/first-gql.js file:

const queryUsers = gql`
  query users($first: Int) {
    users(first: $first) {
      id
      symbol
    }
  }
`;

Here is my ./src/second-gql.js file:

const subscribeAdminNotification = gql`
  subscription admin_notification(
    $limit: Int
    $order_by: [admin_notification_order_by!]
    $where: admin_notification_bool_exp
  ) {
    admin_notification(limit: $limit, order_by: $order_by, where: $where) {
      id
      level
    }
  }
`;

When running npx eslint --ext graphql,js . the graphql-eslint linter complains that Cannot query field "admin_notification" on type "Subscription", because it is looking in the wrong .graphql schema!!!

If I remove the first schema definition from .graphqlrc.yml, then it works OK.

Expected behavior

graphql-eslint should lint .js files against their related .graphql schema definition (as defined by the documents field in .graphqlrc.yml)

Otherwise, it breaks compatibility with graphql-config@3 which documentation states that it’s supporting multiple projects

May I know how do you lint when you have multiple .graphql files/projects in .graphqlrc.yml?

Thanks for your help!

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 2
  • Comments: 19 (6 by maintainers)

Most upvoted comments

Wow, it just works!

graphql-eslint is now correctly linting gql tags belonging to multiple projects.

And as a bonus, vscode.graphql extension now works perfectly.

Amazing, Thanks @B2o5T and @dotansimha for the fix and your precious help!

I let you close the issue when you feel appropriate 😃

Fixed in @graphql-eslint/eslint-plugin@1.1.4 😃 Thanks @B2o5T ❤️

Ok, I don’t know then what could be the cause…What is reproducible using this public repo is that:

  • direct linting (npx eslint ./query2.js) of js files containing gql tags belonging to different graphql schema works
  • global linting (npx eslint --ext graphql,js .) of js files containing gql tags belonging to different graphql schema doesn’t work

This should explain why vscode.graphql extension doesn’t work for me when .graphqlrc has multiple projects, as it’s probably using a global expression (my guess).

Or you can npm pack in the source directory and point Yarn/Npm to load the artifact tgz.

@B2o5T if you’ll push it and create the PR from a branch in this repo (instead of a fork), you’ll get an alpha release from the PR.

I think multi-project should work, since we are using graphql-config, and it does support. We are using getProjectForFile to get the correct schema per each file we are running graphql-eslint on, so if the file is included correctly in the project of graphql-config, it should load the schema correctly.

I think there might be an issue here related to virtual files loaded from code-files? @B2o5T what do you think? I think that here we get the virtual path, and then it leads to an an empty project, since the virtual file doesn’t match the project?

@waterdrop01 can you please try to put your GraphQL operation in a .graphql file and try to update your GraphQL-Config file accordingly? Just to verify that it only happens with virtual files.

It seems like your issue is related to https://github.com/dotansimha/graphql-eslint/issues/525 that will be fixed soon

Thank you so much @waterdrop01 for your reproduction repo, it is so much helpful to found where the issue comes! You can try to install 1.1.4-alpha-4a8e08f.0 version of @graphql-eslint/eslint-plugin.

The issue is comes from caching schemas. Currently, schema is cached by filedir of every file, and schema for query2.js file was incorrectly loaded due query1.js and query2.js are located in the same directory. I changed caching schemas by real file path @dotansimha I think it’s better.

UPDATE: For better performance, it’s better if we’ll cache schema by schemaKey like we already cache without graphql-config. I added a new commit in my PR to fix this.

Well, actually directly linting the documents file works: npx eslint ./query2.js

BUT linting all files with npx eslint --ext graphql,js . still throw the error:

/home/me/code/github.com/waterdrop01/graphql-eslint-prettier/query2.js
  5:6  error  Cannot query field "book" on type "Query"  @graphql-eslint/fields-on-correct-type

✖ 1 problem (1 error, 0 warnings)

I created a repository so you can reproduce the issue.

Ok, I confirm the fix works for me 👍 Awesome

@dotansimha thanks, I’ll try for my next one 😆