What is @envelop/validation-cache?
@envelop/validation-cache is a plugin for GraphQL that provides caching for validation results. This can significantly improve the performance of GraphQL servers by avoiding redundant validation of the same queries.
What are @envelop/validation-cache's main functionalities?
Validation Caching
This feature allows you to cache the results of GraphQL query validations. By using the `useValidationCache` plugin, you can avoid re-validating the same queries, thus improving performance.
const { envelop } = require('@envelop/core');
const { useValidationCache } = require('@envelop/validation-cache');
const getEnveloped = envelop({
plugins: [
useValidationCache(),
],
});
Other packages similar to @envelop/validation-cache
graphql-query-complexity
graphql-query-complexity is a package that helps in limiting the complexity of GraphQL queries to prevent abuse. While it doesn't cache validation results, it provides a way to control the resource usage of queries, which can complement the functionality of @envelop/validation-cache.
graphql-cost-analysis
graphql-cost-analysis is another package that helps in analyzing and limiting the cost of GraphQL queries. It focuses on preventing expensive queries from being executed, which can be used alongside @envelop/validation-cache to ensure both performance and security.
graphql-depth-limit
graphql-depth-limit is a package that restricts the depth of GraphQL queries to prevent overly complex queries. This can be used in conjunction with @envelop/validation-cache to ensure that only queries of acceptable complexity are cached and executed.
@envelop/validation-cache
This plugins adds simple LRU caching to your validate
, to improve performance by caching the
validation result.
This plugins improves performance of validating by ~50% (based on benchmarks).
Getting Started
yarn add @envelop/validation-cache
Usage Example
import { execute, parse, specifiedRules, subscribe, validate } from 'graphql'
import { envelop, useEngine } from '@envelop/core'
import { useValidationCache } from '@envelop/validation-cache'
const getEnveloped = envelop({
plugins: [
useEngine({ parse, validate, specifiedRules, execute, subscribe }),
useValidationCache({
})
]
})
API Reference
cache
Set this to pass in a cache instance. By default a new LRU cache is created using default max
and
ttl
.