UNPKG

@graphql-mesh/jit-executor

Version:
45 lines (40 loc) 1.8 kB
'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); const utils = require('@graphql-tools/utils'); const graphqlJit = require('graphql-jit'); const utils$1 = require('@graphql-mesh/utils'); const getLruCacheForSchema = utils.memoize1(function getLruCacheForSchema(schema) { return utils$1.createLruCache(1000, 3600); }); function createJITExecutor(schema, prefix, logger) { const lruCache = getLruCacheForSchema(schema); return function jitExecutor(request) { const { document, variables, context, operationName, rootValue } = request; const documentStr = utils$1.printWithCache(document); logger.debug(`Executing ${documentStr}`); const cacheKey = [prefix, documentStr, operationName].join('_'); let compiledQueryFn = lruCache.get(cacheKey); if (!compiledQueryFn) { logger.debug(`Compiling ${documentStr}`); const compiledQuery = graphqlJit.compileQuery(schema, document, operationName); if (graphqlJit.isCompiledQuery(compiledQuery)) { const { operation } = utils.getOperationASTFromRequest(request); if (operation === 'subscription') { compiledQueryFn = compiledQuery.subscribe.bind(compiledQuery); } else { compiledQueryFn = compiledQuery.query.bind(compiledQuery); } } else { compiledQueryFn = () => compiledQuery; } lruCache.set(cacheKey, compiledQueryFn); } else { logger.debug(`Compiled version found for ${documentStr}`); } return compiledQueryFn(rootValue, context, variables); }; } exports.createJITExecutor = createJITExecutor;