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

Add require-import-fragment rule #1443

Merged
merged 15 commits into from
Feb 9, 2023
Merged

Add require-import-fragment rule #1443

merged 15 commits into from
Feb 9, 2023

Conversation

FloEdelmann
Copy link
Contributor

Description

Fixes #1413

Type of change

  • New feature (non-breaking change which adds functionality)

How Has This Been Tested?

Via unit tests (see changed files).

Test Environment:

  • OS: Ubuntu 22.04
  • NodeJS: 16.19.0

Checklist:

  • I have followed the
    CONTRIBUTING doc and the
    style guidelines of this project
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests and linter rules pass locally with my changes
  • Any dependent changes have been merged and published in downstream modules

@changeset-bot
Copy link

changeset-bot bot commented Feb 7, 2023

🦋 Changeset detected

Latest commit: 007cbf3

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@graphql-eslint/eslint-plugin Minor

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@dimaMachina
Copy link
Owner

also, what will happen if you import not-foo.graphql and this file does not contain Foo fragment

# import Foo from 'not-foo.graphql'

        query MyQuery {
          fooField {
            ...Foo
          }
        }

Copy link
Contributor Author

@FloEdelmann FloEdelmann left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That looks really good, thank you!

Comment on lines 129 to 132
'FragmentSpread .name'(node: GraphQLESTreeNode<NameNode>) {
fragmentSpreadNameNodes.add(node);
},
FragmentDefinition(fragmentDefinitionNode) {
knownFragmentNames.add(fragmentDefinitionNode.name.value);
'FragmentDefinition .name'(node: GraphQLESTreeNode<NameNode>) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why .name and not just Name?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it is because FragmentDefinition Name will match

// some abstract GraphQL AST Node
const node = {
  type: 'FragmentDefinition',
  name: { // ✅ matched 1
    type: 'Name'
    // ...
  },
  foo: { // ✅ matched 2
    type: 'Name',
    bar: { // ✅ matched 3
      type: 'Name' 
      // ...
    }
    // ...
  }
  // ...
}
  1. FragmentDefinition .name will match only node.name
  2. FragmentDefinition > Name will match only node.name and node.foo but not not.foo.bar

So for more safety, it should be FragmentDefinition > .name, I remember I got some issue due too wide selector, so it's better to be explicit while selecting nodes

Copy link
Owner

@dimaMachina dimaMachina left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for working on it! In case you would work on other rules you could extract some pieces of code for parsing#import comments!

@dimaMachina dimaMachina merged commit 9916d8d into dimaMachina:master Feb 9, 2023
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

Successfully merging this pull request may close these issues.

New rule: require import expressions for fragments
2 participants