Skip to content

useGraphqlSortedDefinitions (GraphQL)

Language GraphQL
.vscode/settings.json
{
"editor.codeActionsOnSave": {
"source.action.useGraphqlSortedDefinitions.biome": "explicit",
"source.fixAll.biome": "explicit"
}
}
biome.json
{
"assist": {
"actions": {
"source": {
"useGraphqlSortedDefinitions": "on"
}
}
}
}

Sort the named definitions in a GraphQL document.

This action natural sorts the named definitions of a document by name: the scalar, type, interface, union, enum, and input definitions, their extend counterparts, the directive definitions, the fragment definitions, and the named query, mutation, and subscription operations. Keeping them sorted makes growing documents easier to scan and review. The order of definitions doesn’t affect how a document is executed or how a schema is built.

Definitions are sorted by name in a Natural order, meaning that uppercase letters come before lowercase letters (e.g. A < a < B < b) and numbers are compared to their numerical value (e.g. 9 < 10).

Definitions without a name, such as schema definitions, anonymous operations, and shorthand queries, are placed after the named definitions. Definitions with the same name aren’t sorted.

type User {
id: ID!
}
enum Role {
ADMIN
USER
}
directive @auth on FIELD_DEFINITION
code-block.graphql ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

ℹ Source action diff:

1 │ - type·User·{
2 │ - ··id:·ID!
3 │ - }
1 │ + directive·@auth·on·FIELD_DEFINITION
4 2 │
5 3 │ enum Role {
····· │
8 6 │ }
9 7 │
10 │ - directive·@auth·on·FIELD_DEFINITION
8 │ + type·User·{
9 │ + ··id:·ID!
10 │ + }
11 11 │

directive @auth on FIELD_DEFINITION
enum Role {
ADMIN
USER
}
type User {
id: ID!
}

Comments above a definition and comments at the end of its last line move with that definition. Because moving a comment can change which definition it describes or hide code, the action offers no fix in these cases:

  • A moved end-of-line comment would share a line with the next definition. Since a # comment continues to the end of its line, it would hide that definition. For example, sorting this input would move Apple before Zebra, but the comment would hide Zebra:
type Zebra { id: ID! } type Apple { id: ID! } # Keep Apple
  • A definition with a comment above it is, or would become, the first definition in the document.

Sort such definitions manually, or put each definition on its own line.