Back to Biomejs

useLoneExecutableDefinition

src/content/docs/linter/rules/use-lone-executable-definition.mdx

latest10.2 KB
Original Source

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

<Tabs> <TabItem label="GraphQL" icon="seti:graphql"> :::caution This rule is part of the [nursery](/linter/#nursery) group. This means that it is experimental and the behavior can change at any time. ::: ## Summary - Rule available since: `v2.3.12` - Diagnostic Category: [`lint/nursery/useLoneExecutableDefinition`](/reference/diagnostics#diagnostic-category) - This rule doesn't have a fix. - The default severity of this rule is [**information**](/reference/diagnostics#information). - Sources: - Same as [`@graphql-eslint/lone-executable-definition`](https://the-guild.dev/graphql/eslint/rules/lone-executable-definition)

How to configure

json
{
	"linter": {
		"rules": {
			"nursery": {
				"useLoneExecutableDefinition": "error"
			}
		}
	}
}

Description

Require queries, mutations, subscriptions or fragments each to be located in separate files.

This rule ensures that each GraphQL document only contains a single operation (query, mutation, or subscription) or fragment definition. Having multiple executable definitions in a single file can make code harder to maintain, test, and understand.

Examples

Invalid

graphql
query Foo {
  id
}

fragment Bar on Baz {
  id
}
<pre class="language-text"><code class="language-text">code-block.graphql:5:1 <a href="https://biomejs.dev/linter/rules/use-lone-executable-definition">lint/nursery/useLoneExecutableDefinition</a> ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">Document contains multiple definitions. This definition should be in a separate file.</span> <strong>3 │ </strong>&#125; <strong>4 │ </strong> <strong><span style="color: Tomato;">&gt;</span></strong> <strong>5 │ </strong>fragment Bar on Baz &#123; <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><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><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><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><span style="color: Tomato;">^</span></strong> <strong><span style="color: Tomato;">&gt;</span></strong> <strong>6 │ </strong> id <strong><span style="color: Tomato;">&gt;</span></strong> <strong>7 │ </strong>&#125; <strong> │ </strong><strong><span style="color: Tomato;">^</span></strong> <strong>8 │ </strong> <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">Queries, mutations, subscriptions or fragments each must be defined in separate files.</span> <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit </span><span style="color: lightgreen;"><a href="https://biomejs.dev/linter/#nursery">https://biomejs.dev/linter/#nursery</a></span><span style="color: lightgreen;"> for more information.</span> </code></pre>
graphql
query Foo {
  id
}

mutation ($name: String!) {
  createUser {
    id
  }
}
<pre class="language-text"><code class="language-text">code-block.graphql:5:1 <a href="https://biomejs.dev/linter/rules/use-lone-executable-definition">lint/nursery/useLoneExecutableDefinition</a> ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">Document contains multiple definitions. This definition should be in a separate file.</span> <strong>3 │ </strong>&#125; <strong>4 │ </strong> <strong><span style="color: Tomato;">&gt;</span></strong> <strong>5 │ </strong>mutation ($name: String!) &#123; <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><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><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><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><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><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong> <strong><span style="color: Tomato;">&gt;</span></strong> <strong>6 │ </strong> createUser &#123; <strong><span style="color: Tomato;">&gt;</span></strong> <strong>7 │ </strong> id <strong><span style="color: Tomato;">&gt;</span></strong> <strong>8 │ </strong> &#125; <strong><span style="color: Tomato;">&gt;</span></strong> <strong>9 │ </strong>&#125; <strong> │ </strong><strong><span style="color: Tomato;">^</span></strong> <strong>10 │ </strong> <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">Queries, mutations, subscriptions or fragments each must be defined in separate files.</span> <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit </span><span style="color: lightgreen;"><a href="https://biomejs.dev/linter/#nursery">https://biomejs.dev/linter/#nursery</a></span><span style="color: lightgreen;"> for more information.</span> </code></pre>
graphql
query Foo {
  id
}

query Bar {
  id
}
<pre class="language-text"><code class="language-text">code-block.graphql:5:1 <a href="https://biomejs.dev/linter/rules/use-lone-executable-definition">lint/nursery/useLoneExecutableDefinition</a> ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">Document contains multiple definitions. This definition should be in a separate file.</span> <strong>3 │ </strong>&#125; <strong>4 │ </strong> <strong><span style="color: Tomato;">&gt;</span></strong> <strong>5 │ </strong>query Bar &#123; <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><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><span style="color: Tomato;">^</span></strong> <strong><span style="color: Tomato;">&gt;</span></strong> <strong>6 │ </strong> id <strong><span style="color: Tomato;">&gt;</span></strong> <strong>7 │ </strong>&#125; <strong> │ </strong><strong><span style="color: Tomato;">^</span></strong> <strong>8 │ </strong> <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">Queries, mutations, subscriptions or fragments each must be defined in separate files.</span> <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit </span><span style="color: lightgreen;"><a href="https://biomejs.dev/linter/#nursery">https://biomejs.dev/linter/#nursery</a></span><span style="color: lightgreen;"> for more information.</span> </code></pre>

Valid

graphql
query Foo {
  id
}
graphql
fragment Bar on Baz {
  id
}
</TabItem> </Tabs>