UNPKG

@envelop/rate-limiter

Version:

This plugins uses [`graphql-rate-limit`](https://github.com/teamplanes/graphql-rate-limit#readme) in order to limit the rate of calling queries and mutations.

121 lines (120 loc) 6.14 kB
import { defaultFieldResolver, isObjectType, responsePathAsArray, } from 'graphql'; import picomatch from 'picomatch'; import { createGraphQLError, getDirectiveExtensions } from '@graphql-tools/utils'; import { handleMaybePromise } from '@whatwg-node/promise-helpers'; import { getGraphQLRateLimiter } from './get-graphql-rate-limiter.js'; import { InMemoryStore } from './in-memory-store.js'; import { RateLimitError } from './rate-limit-error.js'; import { RedisStore } from './redis-store.js'; import { Store } from './store.js'; export { InMemoryStore, RateLimitError, RedisStore, Store, }; export const DIRECTIVE_SDL = /* GraphQL */ ` directive @rateLimit( max: Int window: String message: String identityArgs: [String] arrayLengthField: String readOnly: Boolean uncountRejected: Boolean ) on FIELD_DEFINITION `; export const defaultInterpolateMessageFn = (message, identifier) => interpolateByArgs(message, { id: identifier }); export const useRateLimiter = (options) => { const rateLimiterFn = getGraphQLRateLimiter({ ...options, identifyContext: options.identifyFn, }); const interpolateMessage = options.interpolateMessage || defaultInterpolateMessageFn; const configByField = options.configByField?.map(config => ({ ...config, isMatch: { type: picomatch(config.type), field: picomatch(config.field), }, })); const directiveName = options.rateLimitDirectiveName ?? 'rateLimit'; return { onSchemaChange({ schema: _schema }) { if (!_schema) { return; } const schema = _schema; for (const type of Object.values(schema.getTypeMap())) { if (!isObjectType(type)) { continue; } for (const field of Object.values(type.getFields())) { const fieldConfigs = configByField?.filter(({ isMatch }) => isMatch.type(type.name) && isMatch.field(field.name)); if (fieldConfigs && fieldConfigs.length > 1) { throw new Error(`Config error: field '${type.name}.${field.name}' has multiple matching configuration`); } const fieldConfig = fieldConfigs?.[0]; const rateLimitDirective = getDirectiveExtensions(field, schema)[directiveName]?.[0]; if (rateLimitDirective && fieldConfig) { throw new Error(`Config error: field '${type.name}.${field.name}' has both a configuration and a directive`); } const baseConfig = rateLimitDirective ?? fieldConfig; if (baseConfig) { const rateLimitConfig = { ...baseConfig }; rateLimitConfig.max = rateLimitConfig.max && Number(rateLimitConfig.max); if (fieldConfig?.identifyFn) { rateLimitConfig.identityArgs = [ 'identifier', ...(rateLimitConfig.identityArgs ?? []), ]; } const originalResolver = field.resolve ?? defaultFieldResolver; field.resolve = (parent, args, context, info) => { const resolverRateLimitConfig = { ...rateLimitConfig }; const executionArgs = { parent, args, context, info }; const identifier = (fieldConfig?.identifyFn ?? options.identifyFn)(context); if (fieldConfig?.identifyFn) { executionArgs.args = { identifier, ...args }; } if (resolverRateLimitConfig.message && identifier) { const messageArgs = { root: parent, args, context, info }; resolverRateLimitConfig.message = interpolateMessage(resolverRateLimitConfig.message, identifier, messageArgs); } return handleMaybePromise(() => rateLimiterFn(executionArgs, resolverRateLimitConfig), rateLimitError => { if (!rateLimitError) { return originalResolver(parent, args, context, info); } if (options.onRateLimitError) { options.onRateLimitError({ error: rateLimitError, identifier, context, info, }); } if (options.transformError) { throw options.transformError(rateLimitError); } const errorOptions = { extensions: { http: { statusCode: 429 } }, path: responsePathAsArray(info.path), nodes: info.fieldNodes, }; if (resolverRateLimitConfig.window) { errorOptions.extensions.http.headers = { 'Retry-After': resolverRateLimitConfig.window, }; } throw createGraphQLError(rateLimitError, errorOptions); }); }; } } } }, onContextBuilding({ extendContext }) { extendContext({ rateLimiterFn, }); }, }; }; function interpolateByArgs(message, args) { return message.replace(/\{{([^)]*)\}}/g, (_, key) => args[key.trim()]); }