UNPKG

@graphql-mesh/plugin-jit

Version:
43 lines (42 loc) 1.77 kB
import { compileQuery, isCompiledQuery } from 'graphql-jit'; import { getDocumentString } from '@envelop/core'; import { createLruCache } from '@graphql-mesh/utils'; function createExecuteFnWithJit() { const compiledQueryByDocument = new WeakMap(); const compiledQueryByDocumentStr = createLruCache(); return function executeWithJit(args) { let compiledQuery = compiledQueryByDocument.get(args.document); if (compiledQuery == null) { const documentStr = getDocumentString(args.document); compiledQuery = compiledQueryByDocumentStr.get(documentStr); if (compiledQuery == null) { const compilationResult = compileQuery(args.schema, args.document, args.operationName, { disableLeafSerialization: true, }); if (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); }; } export function useJIT() { const executeFnWithJit = createExecuteFnWithJit(); return { onExecute({ setExecuteFn }) { setExecuteFn(executeFnWithJit); }, onSubscribe({ setSubscribeFn }) { setSubscribeFn(executeFnWithJit); }, }; }