@envelop/graphql-jit 5.0.6 → 5.0.7-alpha-20230220175948-cdba6d9f

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/cjs/index.js CHANGED
@@ -4,21 +4,21 @@ exports.useGraphQlJit = void 0;
4
4
  const tslib_1 = require("tslib");
5
5
  /* eslint-disable no-console */
6
6
  const core_1 = require("@envelop/core");
7
- const graphql_1 = require("graphql");
8
7
  const graphql_jit_1 = require("graphql-jit");
9
8
  const lru_cache_1 = tslib_1.__importDefault(require("lru-cache"));
10
9
  const DEFAULT_MAX = 1000;
11
10
  const DEFAULT_TTL = 3600000;
12
11
  const useGraphQlJit = (compilerOptions = {}, pluginOptions = {}) => {
13
- const documentSourceMap = new WeakMap();
14
- const jitCache = typeof pluginOptions.cache !== 'undefined'
12
+ const jitCacheByDocumentString = typeof pluginOptions.cache !== 'undefined'
15
13
  ? pluginOptions.cache
16
14
  : new lru_cache_1.default({ max: DEFAULT_MAX, maxAge: DEFAULT_TTL });
15
+ const jitCacheByDocument = new WeakMap();
17
16
  function getCacheEntry(args) {
18
17
  let cacheEntry;
19
- const documentSource = documentSourceMap.get(args.document);
20
- if (documentSource) {
21
- cacheEntry = jitCache.get(documentSource);
18
+ cacheEntry = jitCacheByDocument.get(args.document);
19
+ const documentSource = (0, core_1.getDocumentString)(args.document);
20
+ if (!cacheEntry && documentSource) {
21
+ cacheEntry = jitCacheByDocumentString.get(documentSource);
22
22
  }
23
23
  if (!cacheEntry) {
24
24
  const compilationResult = (0, graphql_jit_1.compileQuery)(args.schema, args.document, args.operationName ?? undefined, compilerOptions);
@@ -36,21 +36,14 @@ const useGraphQlJit = (compilerOptions = {}, pluginOptions = {}) => {
36
36
  else {
37
37
  cacheEntry = compilationResult;
38
38
  }
39
+ jitCacheByDocument.set(args.document, cacheEntry);
39
40
  if (documentSource) {
40
- jitCache.set(documentSource, cacheEntry);
41
+ jitCacheByDocumentString.set(documentSource, cacheEntry);
41
42
  }
42
43
  }
43
44
  return cacheEntry;
44
45
  }
45
46
  return {
46
- onParse({ params: { source } }) {
47
- const key = source instanceof graphql_1.Source ? source.body : source;
48
- return ({ result }) => {
49
- if (!result || result instanceof Error)
50
- return;
51
- documentSourceMap.set(result, key);
52
- };
53
- },
54
47
  async onExecute({ args, setExecuteFn }) {
55
48
  if (!pluginOptions.enableIf || (pluginOptions.enableIf && (await pluginOptions.enableIf(args)))) {
56
49
  setExecuteFn((0, core_1.makeExecute)(function jitExecutor(args) {
package/esm/index.js CHANGED
@@ -1,20 +1,20 @@
1
1
  /* eslint-disable no-console */
2
- import { makeExecute, makeSubscribe } from '@envelop/core';
3
- import { Source } from 'graphql';
2
+ import { getDocumentString, makeExecute, makeSubscribe } from '@envelop/core';
4
3
  import { compileQuery, isCompiledQuery } from 'graphql-jit';
5
4
  import LRU from 'lru-cache';
6
5
  const DEFAULT_MAX = 1000;
7
6
  const DEFAULT_TTL = 3600000;
8
7
  export const useGraphQlJit = (compilerOptions = {}, pluginOptions = {}) => {
9
- const documentSourceMap = new WeakMap();
10
- const jitCache = typeof pluginOptions.cache !== 'undefined'
8
+ const jitCacheByDocumentString = typeof pluginOptions.cache !== 'undefined'
11
9
  ? pluginOptions.cache
12
10
  : new LRU({ max: DEFAULT_MAX, maxAge: DEFAULT_TTL });
11
+ const jitCacheByDocument = new WeakMap();
13
12
  function getCacheEntry(args) {
14
13
  let cacheEntry;
15
- const documentSource = documentSourceMap.get(args.document);
16
- if (documentSource) {
17
- cacheEntry = jitCache.get(documentSource);
14
+ cacheEntry = jitCacheByDocument.get(args.document);
15
+ const documentSource = getDocumentString(args.document);
16
+ if (!cacheEntry && documentSource) {
17
+ cacheEntry = jitCacheByDocumentString.get(documentSource);
18
18
  }
19
19
  if (!cacheEntry) {
20
20
  const compilationResult = compileQuery(args.schema, args.document, args.operationName ?? undefined, compilerOptions);
@@ -32,21 +32,14 @@ export const useGraphQlJit = (compilerOptions = {}, pluginOptions = {}) => {
32
32
  else {
33
33
  cacheEntry = compilationResult;
34
34
  }
35
+ jitCacheByDocument.set(args.document, cacheEntry);
35
36
  if (documentSource) {
36
- jitCache.set(documentSource, cacheEntry);
37
+ jitCacheByDocumentString.set(documentSource, cacheEntry);
37
38
  }
38
39
  }
39
40
  return cacheEntry;
40
41
  }
41
42
  return {
42
- onParse({ params: { source } }) {
43
- const key = source instanceof Source ? source.body : source;
44
- return ({ result }) => {
45
- if (!result || result instanceof Error)
46
- return;
47
- documentSourceMap.set(result, key);
48
- };
49
- },
50
43
  async onExecute({ args, setExecuteFn }) {
51
44
  if (!pluginOptions.enableIf || (pluginOptions.enableIf && (await pluginOptions.enableIf(args)))) {
52
45
  setExecuteFn(makeExecute(function jitExecutor(args) {
package/package.json CHANGED
@@ -1,9 +1,9 @@
1
1
  {
2
2
  "name": "@envelop/graphql-jit",
3
- "version": "5.0.6",
3
+ "version": "5.0.7-alpha-20230220175948-cdba6d9f",
4
4
  "sideEffects": false,
5
5
  "peerDependencies": {
6
- "@envelop/core": "^3.0.6",
6
+ "@envelop/core": "3.0.7-alpha-20230220175948-cdba6d9f",
7
7
  "graphql": "^14.0.0 || ^15.0.0 || ^16.0.0"
8
8
  },
9
9
  "dependencies": {