Back to Biomejs

useGraphqlNamedOperations

src/content/docs/linter/rules/use-graphql-named-operations.mdx

latest3.4 KB
Original Source

import { Tabs, TabItem } from '@astrojs/starlight/components';

<Tabs> <TabItem label="GraphQL" icon="seti:graphql"> ## Summary - Rule available since: `v2.0.0` - Diagnostic Category: [`lint/correctness/useGraphqlNamedOperations`](/reference/diagnostics#diagnostic-category) - This rule is **recommended**, meaning it is enabled by default. - This rule has an [**unsafe**](/linter/#unsafe-fixes) fix. - The default severity of this rule is [**error**](/reference/diagnostics#error). - Sources: - Same as [`@graphql-eslint/no-anonymous-operations`](https://the-guild.dev/graphql/eslint/rules/no-anonymous-operations)

How to configure

json
{
	"linter": {
		"rules": {
			"correctness": {
				"useGraphqlNamedOperations": "error"
			}
		}
	}
}

Description

Enforce specifying the name of GraphQL operations.

This is useful because most GraphQL client libraries use the operation name for caching purposes.

Examples

Invalid

graphql
query {}
<pre class="language-text"><code class="language-text">code-block.graphql:1:1 <a href="https://biomejs.dev/linter/rules/use-graphql-named-operations">lint/correctness/useGraphqlNamedOperations</a> <span style="color: #000; background-color: #ddd;"> FIXABLE </span> ━━━━━━━━━━━━━━━━━━━━━━━━ <strong><span style="color: Tomato;">✖</span></strong> <span style="color: Tomato;">Anonymous GraphQL operations are forbidden. Make sure to name your query.</span> <strong><span style="color: Tomato;">&gt;</span></strong> <strong>1 │ </strong>query &#123;&#125; <strong> │ </strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong> <strong>2 │ </strong> <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">Most GraphQL client libraries use the operation name for caching purposes.</span> <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">Unsafe fix</span><span style="color: lightgreen;">: </span><span style="color: lightgreen;">Rename this query to Query.</span> <strong> 1 │ </strong>query<span style="opacity: 0.8;">·</span><span style="color: MediumSeaGreen;">Q</span><span style="color: MediumSeaGreen;">u</span><span style="color: MediumSeaGreen;">e</span><span style="color: MediumSeaGreen;">r</span><span style="color: MediumSeaGreen;">y</span>&#123;&#125; <strong> │ </strong> <span style="color: MediumSeaGreen;">+</span><span style="color: MediumSeaGreen;">+</span><span style="color: MediumSeaGreen;">+</span><span style="color: MediumSeaGreen;">+</span><span style="color: MediumSeaGreen;">+</span> </code></pre>

Valid

graphql
query Human {
  name
}
</TabItem> </Tabs>