Skip to Content
🚧 This is WIP documentation for v4 of the plugin. For v3 click here.
ESLGraphQL-ESLint
Rulesno-unused-fields

no-unused-fields

💡 This rule provides suggestions

  • Category: Schema
  • Rule name: @graphql-eslint/no-unused-fields
  • Requires GraphQL Schema: true ℹ️
  • Requires GraphQL Operations: true ℹ️
Requires all fields to be used at some level by siblings operations.

Usage Examples

Incorrect

# eslint @graphql-eslint/no-unused-fields: 'error'
 
type User {
  id: ID!
  name: String
  someUnusedField: String
}
 
type Query {
  me: User
}
 
query {
  me {
    id
    name
  }
}

Correct

# eslint @graphql-eslint/no-unused-fields: 'error'
 
type User {
  id: ID!
  name: String
}
 
type Query {
  me: User
}
 
query {
  me {
    id
    name
  }
}

Correct (ignoring fields)

# eslint @graphql-eslint/no-unused-fields: ['error', { ignoredFieldSelectors: ['[parent.name.value=PageInfo][name.value=/(endCursor|startCursor|hasNextPage|hasPreviousPage)/]', '[parent.name.value=/Edge$/][name.value=cursor]', '[parent.name.value=/Connection$/][name.value=pageInfo]'] }]
 
### 1️⃣ YOUR SCHEMA
 
# Root Query Type
type Query {
  user: User
}
 
# User Type
type User {
  id: ID!
  name: String!
  friends(first: Int, after: String): FriendConnection!
}
 
# FriendConnection Type (Relay Connection)
type FriendConnection {
  edges: [FriendEdge]
  pageInfo: PageInfo!
}
 
# FriendEdge Type
type FriendEdge {
  cursor: String!
  node: Friend!
}
 
# Friend Type
type Friend {
  id: ID!
  name: String!
}
 
# PageInfo Type (Relay Pagination)
type PageInfo {
  hasPreviousPage: Boolean!
  hasNextPage: Boolean!
  startCursor: String
  endCursor: String
}
 
### 2️⃣ YOUR QUERY
 
query {
  user {
    id
    name
    friends(first: 10) {
      edges {
        node {
          id
          name
        }
      }
    }
  }
}

Config Schema

The schema defines the following properties:

ignoredFieldSelectors (array)

Fields that will be ignored and are allowed to be unused.

E.g. The following selector will ignore all the relay pagination fields for every connection exposed in the schema:

[
  "[parent.name.value=PageInfo][name.value=/(endCursor|startCursor|hasNextPage|hasPreviousPage)/]",
  "[parent.name.value=/Edge$/][name.value=cursor]",
  "[parent.name.value=/Connection$/][name.value=pageInfo]"
]

These fields are defined by ESLint selectors. Paste or drop code into the editor in ASTExplorer and inspect the generated AST to compose your selector.

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

Additional restrictions:

  • Minimum items: 1
  • Unique items: true

Resources

Last updated on