Skip to content

Commit 7eca724

Browse files
captbaritonefacebook-github-bot
authored andcommitted
Add docs page describing the Relay lint rules
Reviewed By: lynnshaoyu Differential Revision: D74739413 Privacy Context Container: L1125407 fbshipit-source-id: beafe4be0a8639109b8b8c6f595df7928f3e5334
1 parent 611f6d9 commit 7eca724

File tree

5 files changed

+92
-2
lines changed

5 files changed

+92
-2
lines changed
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
---
2+
id: lint-rules
3+
title: Relay ESLint Plugin
4+
slug: /getting-started/lint-rules/
5+
description: ESLint Plugin Relay
6+
keywords:
7+
- eslint
8+
- lint
9+
---
10+
11+
One of the unique features enabled by Relay is the ability to statically detect unused GraphQL fields. This can [categorically prevent](https://relay.dev/blog/2023/10/24/how-relay-enables-optimal-data-fetching/) the "append only query" problem that is a common disfunction in many GraphQL clients.
12+
13+
![Relay ESLint Plugin](../../static/img/docs/no-unused-fields.png)
14+
15+
This validation, and other helpful checks, are enabled by Relay's ESLint plugin [`eslint-plugin-relay`](https://www.npmjs.com/package/eslint-plugin-relay). **The Relay ESLint plugin is a key part of the Relay developer experience**.
16+
17+
## Installation
18+
19+
Assuming you have [ESLint](https://eslint.org/) already installed, you can add the Relay ESLint plugin to your project by running:
20+
21+
```sh
22+
npm install --save-dev eslint-plugin-relay
23+
```
24+
25+
Then update your ESLint configuration to include the plugin:
26+
27+
```js tile="eslint.config.js"
28+
import relay from 'eslint-plugin-relay';
29+
30+
export default [
31+
// ... other ESlint Config
32+
{
33+
plugins: { relay },
34+
rules: relay.configs['ts-recommended'].rules,
35+
},
36+
];
37+
```
38+
39+
## Rule Descriptions
40+
41+
The following validation rules are included in the Relay ESLint plugin:
42+
43+
### `relay/unused-fields`
44+
Ensures that every GraphQL field referenced is used within the module that includes it. This helps enable Relay's [optimal data fetching](https://relay.dev/blog/2023/10/24/how-relay-enables-optimal-data-fetching/).
45+
46+
### `relay/no-future-added-value`
47+
Ensures code does not try to explicitly handle the `"%future added value"` enum variant which Relay inserts as a placeholder to ensure you handle the possibility that new enum variants may be added by the server after your application has been deployed.
48+
49+
### `relay/graphql-syntax`
50+
Ensures each `graphql` tagged template literal contains syntactically valid GraphQL. This is also validated by the Relay Compiler, but the ESLint plugin can often provide faster feedback.
51+
52+
### `relay/graphql-naming`
53+
Ensures GraphQL fragments and queries follow Relay's naming conventions. This is also validated by the Relay Compiler, but the ESLint plugin can often provide faster feedback.
54+
55+
### `relay/function-required-argument`
56+
Ensures that `readInlineData` is always passed an explicit argument even though that argument is allowed to be `undefined` at runtime.
57+
58+
### `relay/hook-required-argument`
59+
Ensures that Relay hooks are always passed an explicit argument even though that argument is allowed to be `undefined` at runtime.
60+
61+
### `relay/must-colocate-fragment-spreads`
62+
Ensures that when a fragment spread is added within a module, that module directly imports the module which defines that fragment. This prevents the anti-pattern when one component fetches a fragment that is not used by a direct child component.
63+
**Note**: This rule leans heavily on Meta's globally unique module names. It likely won't work well in other environments.
64+
65+
## Suppressing rules within graphql tags
66+
67+
The following rules support suppression within graphql tags:
68+
69+
- `relay/unused-fields`
70+
- `relay/must-colocate-fragment-spreads`
71+
72+
Supported rules can be suppressed by adding `# eslint-disable-next-line relay/name-of-rule` to the preceding line:
73+
74+
```js
75+
graphql`
76+
fragment foo on Page {
77+
# eslint-disable-next-line relay/must-colocate-fragment-spreads
78+
...unused1
79+
}
80+
`;
81+
```
82+
83+
Note that only the `eslint-disable-next-line` form of suppression works. `eslint-disable-line` doesn't currently work until graphql-js provides support for [parsing Comment nodes](https://github.com/graphql/graphql-js/issues/2241) in their AST.
84+
85+
## Contributing
86+
87+
If you wish to contribute to the Relay ESLint plugin, you can find the code on GitHub at [relay/eslint-plugin-relay](https://github.com/relayjs/eslint-plugin-relay/).

website/docs/getting-started/production.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ A key principal of Relay is data colocation where each component defines its own
2323

2424
We recommend using the [`eslint-plugin-relay`](https://github.com/relayjs/eslint-plugin-relay), especially the `relay/unused-fields` rule.
2525

26+
Learn how to install them: [Relay ESLint Plugin](./lint-rules.md).
27+
2628
## Running the Relay Compiler in CI
2729

2830
We recommend committing Relay's generated artifacts to source control along with your application code. This ensures generated types are present without needing an additional build, and allows for inspection of generated artifacts in code review. To ensure the generated artifacts are always in sync with the source code, we recommend running the Relay compiler in CI and ensuring it does not change any generated files. A example bash script which checks for changes can be found [here](https://github.com/facebook/relay/blob/0414c9ad0744483e349e07defcb6d70a52cf8b3c/scripts/check-git-status.sh).

website/docs/getting-started/quick-start.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ cd relay-example
3232
# Runtime dependencies
3333
npm install relay-runtime react-relay
3434
# Dev dependencies
35-
npm install --dev babel-plugin-relay graphql relay-compiler
35+
npm install --save-dev babel-plugin-relay graphql relay-compiler
3636
# Types
37-
npm install --dev @types/relay-runtime @types/react-relay
37+
npm install --save-dev @types/relay-runtime @types/react-relay
3838
```
3939

4040
## Configure Vite to use Relay

website/sidebars.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ module.exports = {
159159
'getting-started/babel-plugin',
160160
'getting-started/compiler',
161161
'getting-started/compiler-config',
162+
'getting-started/lint-rules',
162163
'editor-support',
163164
'getting-started/production',
164165
],
299 KB
Loading

0 commit comments

Comments
 (0)