Parsing and Validation Caching

GraphQL Yoga v3 documentation (superseded by v5): By default, Yoga will maintain a parsing and validation cache. If requests contain documents that have been executed before, they will not be parsed and validated again.

By default, Yoga will maintain a parsing and validation cache. If requests contain documents that have been executed before, they will not be parsed and validated again.

Using the parser cache can improve performance up to ~60%, and using the validation cache up to ~50% (based on benchmarks).

This behavior is built-in and can be optionally disabled using the parserCache and validationCache options:

Disable validation and parser caching
import { createServer } from 'node:http'
import { createYoga } from 'graphql-yoga'
import { schema } from './my-schema'

const yoga = createYoga({
  schema,
  parserCache: false, // disable parse caching
  validationCache: false // disable validation caching
})

const server = createServer(yoga)
server.listen(4000, () => {
  console.info('Server is running on http://localhost:4000/graphql')
})

Furthermore, you can provide your own cache store to both of these plugins by implementing the following interface:

Custom cache store
import { createServer } from 'node:http'
import { DocumentNode, GraphQLError } from 'graphql'
import { createYoga } from 'graphql-yoga'
import { documentCacheStore, errorCacheStore, validationCacheStore } from './my-cache'
import { schema } from './my-schema'

interface CacheStore<T> {
  get(key: string): T | undefined
  set(key: string, value: T): void
}

const yoga = createYoga({
  schema,
  parserCache: {
    documentCache: documentCacheStore as CacheStore<DocumentNode>,
    errorCache: errorCacheStore as CacheStore<Error>
  },
  validationCache: validationCacheStore as CacheStore<readonly GraphQLError[]>
})

const server = createServer(yoga)
server.listen(4000, () => {
  console.info('Server is running on http://localhost:4000/graphql')
})

Yoga uses envelop’s plugins useParserCache and useValidationCache underneath, read more about them on their respective websites.