@graphql-eslint/eslint-plugin
Version:
GraphQL plugin for ESLint
73 lines (54 loc) • 1.51 kB
Markdown
# `known-fragment-names`
✅ The `"extends": "plugin:@graphql-eslint/operations-recommended"` property in a configuration file
enables this rule.
- Category: `Operations`
- Rule name: `-eslint/known-fragment-names`
- Requires GraphQL Schema: `true` [ℹ️](../../README.md#extended-linting-rules-with-graphql-schema)
- Requires GraphQL Operations: `true`
[ℹ️](../../README.md#extended-linting-rules-with-siblings-operations)
A GraphQL document is only valid if all `...Fragment` fragment spreads refer to fragments defined in
the same document.
> This rule is a wrapper around a `graphql-js` validation function.
## Usage Examples
### Incorrect
```graphql
# eslint @graphql-eslint/known-fragment-names: 'error'
query {
user {
id
...UserFields # fragment not defined in the document
}
}
```
### Correct
```graphql
# eslint @graphql-eslint/known-fragment-names: 'error'
fragment UserFields on User {
firstName
lastName
}
query {
user {
id
...UserFields
}
}
```
### Correct (`UserFields` fragment located in a separate file)
```graphql
# eslint @graphql-eslint/known-fragment-names: 'error'
# user.gql
query {
user {
id
...UserFields
}
}
# user-fields.gql
fragment UserFields on User {
id
}
```
## Resources
- [Rule source](https://github.com/graphql/graphql-js/blob/main/src/validation/rules/KnownFragmentNamesRule.ts)
- [Test source](https://github.com/graphql/graphql-js/tree/main/src/validation/__tests__/KnownFragmentNamesRule-test.ts)