Parsing and Validation Caching
By default, Yoga will maintain a parsing and validation cache. If requests contain queries that have been executed already before, they will not be parsed and validated again - only once, on the first request.
Note that 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:
import { createYoga } from 'graphql-yoga'
import { createServer } from 'node:http'
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:
import { DocumentNode, GraphQLError } from 'graphql'
import { createYoga } from 'graphql-yoga'
import { createServer } from 'node:http'
import { schema } from './my-schema'
import {
documentCacheStore,
errorCacheStore,
validationCacheStore
} from './my-cache'
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.
Last updated on October 2, 2022