UNPKG

@graphql-yoga/plugin-persisted-operations

Version:

Persisted Operations plugin for GraphQL Yoga.

84 lines (83 loc) 3.8 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.usePersistedOperations = exports.defaultExtractPersistedOperationId = void 0; const graphql_yoga_1 = require("graphql-yoga"); const defaultExtractPersistedOperationId = (params) => { if (params.extensions != null && typeof params.extensions === 'object' && params.extensions?.persistedQuery != null && typeof params.extensions?.persistedQuery === 'object' && params.extensions?.persistedQuery.version === 1 && typeof params.extensions?.persistedQuery.sha256Hash === 'string') { return params.extensions?.persistedQuery.sha256Hash; } return null; }; exports.defaultExtractPersistedOperationId = defaultExtractPersistedOperationId; function usePersistedOperations({ allowArbitraryOperations = false, extractPersistedOperationId = exports.defaultExtractPersistedOperationId, getPersistedOperation, skipDocumentValidation = false, customErrors, }) { const operationASTByRequest = new WeakMap(); const persistedOperationRequest = new WeakSet(); const notFoundErrorFactory = createErrorFactory('PersistedQueryNotFound', customErrors?.notFound); const keyNotFoundErrorFactory = createErrorFactory('PersistedQueryKeyNotFound', customErrors?.keyNotFound); const persistentQueryOnlyErrorFactory = createErrorFactory('PersistedQueryOnly', customErrors?.persistedQueryOnly); return { async onParams(payload) { const { request, params, setParams } = payload; if (params.query) { if ((typeof allowArbitraryOperations === 'boolean' ? allowArbitraryOperations : await allowArbitraryOperations(request)) === false) { throw persistentQueryOnlyErrorFactory(payload); } return; } const persistedOperationKey = extractPersistedOperationId(params); if (persistedOperationKey == null) { throw keyNotFoundErrorFactory(payload); } const persistedQuery = await getPersistedOperation(persistedOperationKey, request); if (persistedQuery == null) { throw notFoundErrorFactory(payload); } if (typeof persistedQuery === 'object') { setParams({ query: `__PERSISTED_OPERATION_${persistedOperationKey}__`, variables: params.variables, extensions: params.extensions, }); operationASTByRequest.set(request, persistedQuery); } else { setParams({ query: persistedQuery, variables: params.variables, extensions: params.extensions, }); } persistedOperationRequest.add(request); }, onValidate({ setResult, context: { request } }) { if (skipDocumentValidation && persistedOperationRequest.has(request)) { setResult([]); // skip validation } }, onParse({ setParsedDocument, context: { request } }) { const ast = operationASTByRequest.get(request); if (ast) { setParsedDocument(ast); } }, }; } exports.usePersistedOperations = usePersistedOperations; function createErrorFactory(defaultMessage, options) { if (typeof options === 'string') { return () => (0, graphql_yoga_1.createGraphQLError)(options); } if (typeof options === 'function') { return options; } return () => { return (0, graphql_yoga_1.createGraphQLError)(options?.message ?? defaultMessage, options); }; }