-
Notifications
You must be signed in to change notification settings - Fork 106
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
dimaMachina
merged 15 commits into
graphql-hive:master
from
FloEdelmann:require-import-fragment
Feb 9, 2023
Merged
Changes from 1 commit
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
0a3bf5c
Add `require-import-fragment` rule
FloEdelmann 19b442f
Generate configs
FloEdelmann e2ba549
Add changeset
FloEdelmann 1ecc373
Also allow locally defined fragments
FloEdelmann 4033be1
Add suggestion
FloEdelmann e939550
Pass only name node
FloEdelmann d9b0a8b
Use `trimStart` instead of `trim`
FloEdelmann ca6085a
Adjust test case name
FloEdelmann 3b7513f
apply fixes
dimaMachina 012c5e4
this is no needed
dimaMachina f33745e
fix tests
dimaMachina 9a708e9
some fixes
dimaMachina 65f0a45
prettify
dimaMachina 1660617
simplify
dimaMachina 007cbf3
last refactor
dimaMachina File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
122 changes: 59 additions & 63 deletions
122
packages/plugin/tests/__snapshots__/require-import-fragment.spec.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,87 +1,83 @@ | ||
// Vitest Snapshot v1 | ||
|
||
exports[`should report fragments when there are no appropriately named import expressions 1`] = ` | ||
exports[`should report fragments when there are no import expressions 1`] = ` | ||
#### ⌨️ Code | ||
|
||
1 | # import Bar from 'foo.graphql' | ||
2 | | ||
3 | query MyQuery { | ||
4 | fooField { | ||
5 | ...Foo | ||
6 | } | ||
7 | } | ||
1 | { | ||
2 | foo { | ||
3 | ...FooFields | ||
4 | } | ||
5 | } | ||
|
||
#### ❌ Error | ||
|
||
4 | fooField { | ||
> 5 | ...Foo | ||
| ^^^ Expected 'Foo' fragment to be imported. | ||
6 | } | ||
|
||
#### 💡 Suggestion: Add import expression for 'Foo' | ||
|
||
1 | # import Foo from 'PLEASE_CHANGE.graphql' | ||
2 | # import Bar from 'foo.graphql' | ||
3 | | ||
4 | query MyQuery { | ||
5 | fooField { | ||
6 | ...Foo | ||
7 | } | ||
8 | } | ||
2 | foo { | ||
> 3 | ...FooFields | ||
| ^^^^^^^^^ Expected "FooFields" fragment to be imported. | ||
4 | } | ||
|
||
#### 💡 Suggestion: Add import expression for "FooFields". | ||
|
||
1 | # import FooFields from 'CHANGE_ME.graphql' | ||
2 | { | ||
3 | foo { | ||
4 | ...FooFields | ||
5 | } | ||
6 | } | ||
`; | ||
|
||
exports[`should report fragments when there are no import expressions 1`] = ` | ||
exports[`should report with default import 1`] = ` | ||
#### ⌨️ Code | ||
|
||
1 | query MyQuery { | ||
2 | fooField { | ||
3 | ...Foo | ||
4 | } | ||
5 | } | ||
1 | #import 'bar-fragment.gql' | ||
2 | query { | ||
3 | foo { | ||
4 | ...FooFields | ||
5 | } | ||
6 | } | ||
|
||
#### ❌ Error | ||
|
||
2 | fooField { | ||
> 3 | ...Foo | ||
| ^^^ Expected 'Foo' fragment to be imported. | ||
4 | } | ||
3 | foo { | ||
> 4 | ...FooFields | ||
| ^^^^^^^^^ Expected "FooFields" fragment to be imported. | ||
5 | } | ||
|
||
#### 💡 Suggestion: Add import expression for 'Foo' | ||
#### 💡 Suggestion: Add import expression for "FooFields". | ||
|
||
1 | # import Foo from 'PLEASE_CHANGE.graphql' | ||
2 | query MyQuery { | ||
3 | fooField { | ||
4 | ...Foo | ||
5 | } | ||
6 | } | ||
1 | # import FooFields from 'foo-fragment.gql' | ||
2 | #import 'bar-fragment.gql' | ||
3 | query { | ||
4 | foo { | ||
5 | ...FooFields | ||
6 | } | ||
7 | } | ||
`; | ||
|
||
exports[`should report fragments when there are only invalid import expressions 1`] = ` | ||
exports[`should report with named import 1`] = ` | ||
#### ⌨️ Code | ||
|
||
1 | # import 'foo.graphql' | ||
2 | | ||
3 | query MyQuery { | ||
4 | fooField { | ||
5 | ...Foo | ||
6 | } | ||
7 | } | ||
1 | #import FooFields from "bar-fragment.gql" | ||
2 | query { | ||
3 | foo { | ||
4 | ...FooFields | ||
5 | } | ||
6 | } | ||
|
||
#### ❌ Error | ||
|
||
4 | fooField { | ||
> 5 | ...Foo | ||
| ^^^ Expected 'Foo' fragment to be imported. | ||
6 | } | ||
|
||
#### 💡 Suggestion: Add import expression for 'Foo' | ||
|
||
1 | # import Foo from 'PLEASE_CHANGE.graphql' | ||
2 | # import 'foo.graphql' | ||
3 | | ||
4 | query MyQuery { | ||
5 | fooField { | ||
6 | ...Foo | ||
7 | } | ||
8 | } | ||
3 | foo { | ||
> 4 | ...FooFields | ||
| ^^^^^^^^^ Expected "FooFields" fragment to be imported. | ||
5 | } | ||
|
||
#### 💡 Suggestion: Add import expression for "FooFields". | ||
|
||
1 | # import FooFields from 'foo-fragment.gql' | ||
2 | #import FooFields from "bar-fragment.gql" | ||
3 | query { | ||
4 | foo { | ||
5 | ...FooFields | ||
6 | } | ||
7 | } | ||
`; |
3 changes: 3 additions & 0 deletions
3
packages/plugin/tests/mocks/import-fragments/bar-fragment.gql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
fragment BarFields on Bar { | ||
id | ||
} |
3 changes: 3 additions & 0 deletions
3
packages/plugin/tests/mocks/import-fragments/foo-fragment.gql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
fragment FooFields on Foo { | ||
id | ||
} |
6 changes: 6 additions & 0 deletions
6
packages/plugin/tests/mocks/import-fragments/invalid-query-default.gql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
#import 'bar-fragment.gql' | ||
query { | ||
foo { | ||
...FooFields | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
packages/plugin/tests/mocks/import-fragments/invalid-query.gql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
#import FooFields from "bar-fragment.gql" | ||
query { | ||
foo { | ||
...FooFields | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
packages/plugin/tests/mocks/import-fragments/valid-query-default.gql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# Imports could have extra whitespace and double/single quotes | ||
|
||
# import 'foo-fragment.gql' | ||
|
||
query { | ||
foo { | ||
...FooFields | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# Imports could have extra whitespace and double/single quotes | ||
|
||
# import FooFields from "foo-fragment.gql" | ||
|
||
query { | ||
foo { | ||
...FooFields | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why
.name
and not justName
?There was a problem hiding this comment.
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 matchFragmentDefinition .name
will match onlynode.name
FragmentDefinition > Name
will match onlynode.name
andnode.foo
but notnot.foo.bar
So for more safety, it should be
FragmentDefinition > .name
, I remember I got some issue due toowide
selector, so it's better to be explicit while selecting nodes