UNPKG

@graphql-mesh/plugin-jit

Version:
46 lines (45 loc) 1.89 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.useJIT = useJIT; const graphql_jit_1 = require("graphql-jit"); const core_1 = require("@envelop/core"); const utils_1 = require("@graphql-mesh/utils"); function createExecuteFnWithJit() { const compiledQueryByDocument = new WeakMap(); const compiledQueryByDocumentStr = (0, utils_1.createLruCache)(); return function executeWithJit(args) { let compiledQuery = compiledQueryByDocument.get(args.document); if (compiledQuery == null) { const documentStr = (0, core_1.getDocumentString)(args.document); compiledQuery = compiledQueryByDocumentStr.get(documentStr); if (compiledQuery == null) { const compilationResult = (0, graphql_jit_1.compileQuery)(args.schema, args.document, args.operationName, { disableLeafSerialization: true, }); if ((0, graphql_jit_1.isCompiledQuery)(compilationResult)) { compiledQuery = compilationResult; compiledQueryByDocument.set(args.document, compiledQuery); compiledQueryByDocumentStr.set(documentStr, compiledQuery); } else { return compilationResult; } } } if (compiledQuery.subscribe) { return compiledQuery.subscribe(args.rootValue, args.contextValue, args.variableValues); } return compiledQuery.query(args.rootValue, args.contextValue, args.variableValues); }; } function useJIT() { const executeFnWithJit = createExecuteFnWithJit(); return { onExecute({ setExecuteFn }) { setExecuteFn(executeFnWithJit); }, onSubscribe({ setSubscribeFn }) { setSubscribeFn(executeFnWithJit); }, }; }