Skip to Content
This is documentation for v4 of the plugin. For v3 click here.
ESLGraphQL-ESLint
Rulesnaming-convention

naming-convention

✅ The "extends": "plugin:@graphql-eslint/schema-recommended" and "plugin:@graphql-eslint/operations-recommended" property in a configuration file enables this rule.

💡 This rule provides suggestions

  • Category: Schema & Operations
  • Rule name: @graphql-eslint/naming-convention
  • Requires GraphQL Schema: false ℹ️
  • Requires GraphQL Operations: false ℹ️
Require names to follow specified conventions.

Usage Examples

Incorrect

# eslint @graphql-eslint/naming-convention: ['error', { types: 'PascalCase', FieldDefinition: 'camelCase' }]
 
type user {
  first_name: String!
}

Incorrect

# eslint @graphql-eslint/naming-convention: ['error', { FragmentDefinition: { style: 'PascalCase', forbiddenSuffixes: ['Fragment'] } }]
 
fragment UserFragment on User {
  # ...
}

Incorrect

# eslint @graphql-eslint/naming-convention: ['error', { 'FieldDefinition[parent.name.value=Query]': { forbiddenPrefixes: ['get'] } }]
 
type Query {
  getUsers: [User!]!
}

Correct

# eslint @graphql-eslint/naming-convention: ['error', { types: 'PascalCase', FieldDefinition: 'camelCase' }]
 
type User {
  firstName: String
}

Correct

# eslint @graphql-eslint/naming-convention: ['error', { FragmentDefinition: { style: 'PascalCase', forbiddenSuffixes: ['Fragment'] } }]
 
fragment UserFields on User {
  # ...
}

Correct

# eslint @graphql-eslint/naming-convention: ['error', { 'FieldDefinition[parent.name.value=Query]': { forbiddenPrefixes: ['get'] } }]
 
type Query {
  users: [User!]!
}

Correct

# eslint @graphql-eslint/naming-convention: ['error', { FieldDefinition: { style: 'camelCase', ignorePattern: '^(EAN13|UPC|UK)' } }]
 
type Product {
  EAN13: String
  UPC: String
  UKFlag: String
}

Correct

# eslint @graphql-eslint/naming-convention: ['error', { 'FieldDefinition[gqlType.name.value=Boolean]': { style: 'camelCase', requiredPrefixes: ['is', 'has'] }, 'FieldDefinition[gqlType.gqlType.name.value=Boolean]': { style: 'camelCase', requiredPrefixes: ['is', 'has'] } }]
 
type Product {
  isBackordered: Boolean
  isNew: Boolean!
  hasDiscount: Boolean!
}

Correct

# eslint @graphql-eslint/naming-convention: ['error', { 'FieldDefinition[gqlType.gqlType.name.value=SensitiveSecret]': { style: 'camelCase', requiredSuffixes: ['SensitiveSecret'] } }]
 
scalar SensitiveSecret
 
type Account {
  accountSensitiveSecret: SensitiveSecret!
}

Config Schema

It’s possible to use a selector that starts with allowed ASTNode names which are described below.

Paste or drop code into the editor in ASTExplorer and inspect the generated AST to compose your selector.

Example: pattern property FieldDefinition[parent.name.value=Query] will match only fields for type Query.

The schema defines the following properties:

types

Includes:

  • ObjectTypeDefinition
  • InterfaceTypeDefinition
  • EnumTypeDefinition
  • ScalarTypeDefinition
  • InputObjectTypeDefinition
  • UnionTypeDefinition

The object must be one of the following types:

  • asString
  • asObject

Argument

Note

Read more about this kind on spec.graphql.org.

The object must be one of the following types:

  • asString
  • asObject

DirectiveDefinition

Note

Read more about this kind on spec.graphql.org.

The object must be one of the following types:

  • asString
  • asObject

EnumTypeDefinition

Note

Read more about this kind on spec.graphql.org.

The object must be one of the following types:

  • asString
  • asObject

EnumValueDefinition

Note

Read more about this kind on spec.graphql.org.

The object must be one of the following types:

  • asString
  • asObject

FieldDefinition

Note

Read more about this kind on spec.graphql.org.

The object must be one of the following types:

  • asString
  • asObject

FragmentDefinition

Note

Read more about this kind on spec.graphql.org.

The object must be one of the following types:

  • asString
  • asObject

InputObjectTypeDefinition

Note

Read more about this kind on spec.graphql.org.

The object must be one of the following types:

  • asString
  • asObject

InputValueDefinition

Note

Read more about this kind on spec.graphql.org.

The object must be one of the following types:

  • asString
  • asObject

InterfaceTypeDefinition

Note

Read more about this kind on spec.graphql.org.

The object must be one of the following types:

  • asString
  • asObject

ObjectTypeDefinition

Note

Read more about this kind on spec.graphql.org.

The object must be one of the following types:

  • asString
  • asObject

OperationDefinition

Note

Read more about this kind on spec.graphql.org.

The object must be one of the following types:

  • asString
  • asObject

ScalarTypeDefinition

Note

Read more about this kind on spec.graphql.org.

The object must be one of the following types:

  • asString
  • asObject

UnionTypeDefinition

Note

Read more about this kind on spec.graphql.org.

The object must be one of the following types:

  • asString
  • asObject

VariableDefinition

Note

Read more about this kind on spec.graphql.org.

The object must be one of the following types:

  • asString
  • asObject

allowLeadingUnderscore (boolean)

Default: false

allowTrailingUnderscore (boolean)

Default: false


Sub Schemas

The schema defines the following additional types:

asString (enum)

One of: camelCase, PascalCase, snake_case, UPPER_CASE

asObject (object)

Properties of the asObject object:

style (enum)

This element must be one of the following enum values:

  • camelCase
  • PascalCase
  • snake_case
  • UPPER_CASE

prefix (string)

suffix (string)

forbiddenPatterns (array)

Should be of instance of RegEx

The object is an array with all elements of the type object.

The array object has the following properties:

Additional restrictions:

  • Minimum items: 1
  • Unique items: true

requiredPatterns (array)

Should be of instance of RegEx

The object is an array with all elements of the type object.

The array object has the following properties:

Additional restrictions:

  • Minimum items: 1
  • Unique items: true

forbiddenPrefixes (array)

⚠️
Warning

This option is deprecated and will be removed in the next major release. Use forbiddenPatterns instead.

The object is an array with all elements of the type string.

Additional restrictions:

  • Minimum items: 1
  • Unique items: true

forbiddenSuffixes (array)

⚠️
Warning

This option is deprecated and will be removed in the next major release. Use forbiddenPatterns instead.

The object is an array with all elements of the type string.

Additional restrictions:

  • Minimum items: 1
  • Unique items: true

requiredPrefixes (array)

⚠️
Warning

This option is deprecated and will be removed in the next major release. Use requiredPatterns instead.

The object is an array with all elements of the type string.

Additional restrictions:

  • Minimum items: 1
  • Unique items: true

requiredSuffixes (array)

⚠️
Warning

This option is deprecated and will be removed in the next major release. Use requiredPatterns instead.

The object is an array with all elements of the type string.

Additional restrictions:

  • Minimum items: 1
  • Unique items: true

ignorePattern (string)

Option to skip validation of some words, e.g. acronyms

Resources

Last updated on