@envelop/graphql-jit 4.3.0-alpha-c0cc559.0 → 5.0.0-alpha-d0d0776.0

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/index.d.ts CHANGED
@@ -1,11 +1,14 @@
1
- import { Plugin, BasicCache } from '@envelop/core';
1
+ import { Plugin } from '@envelop/core';
2
2
  import { ExecutionArgs, ExecutionResult } from 'graphql';
3
3
  import { CompilerOptions, CompiledQuery } from 'graphql-jit';
4
4
  declare type JITCacheEntry = {
5
5
  query: CompiledQuery['query'];
6
6
  subscribe?: CompiledQuery['subscribe'];
7
7
  };
8
- export declare type JITCache = BasicCache<JITCacheEntry>;
8
+ export interface JITCache {
9
+ get(key: string): JITCacheEntry | undefined;
10
+ set(key: string, value: JITCacheEntry): void;
11
+ }
9
12
  export declare const useGraphQlJit: (compilerOptions?: Partial<CompilerOptions>, pluginOptions?: {
10
13
  /**
11
14
  * A helper function that helps to conditionally enable JIT based on incoming request
package/index.js CHANGED
@@ -5,22 +5,28 @@ Object.defineProperty(exports, '__esModule', { value: true });
5
5
  const core = require('@envelop/core');
6
6
  const graphql = require('graphql');
7
7
  const graphqlJit = require('graphql-jit');
8
+ const lru = require('tiny-lru');
9
+
10
+ function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
11
+
12
+ const lru__default = /*#__PURE__*/_interopDefaultLegacy(lru);
8
13
 
9
14
  /* eslint-disable no-console */
15
+ const DEFAULT_MAX = 1000;
16
+ const DEFAULT_TTL = 3600000;
10
17
  const useGraphQlJit = (compilerOptions = {}, pluginOptions = {}) => {
11
18
  const documentSourceMap = new WeakMap();
12
- const jitCache = pluginOptions.cache != null ? pluginOptions.cache : core.getInMemoryLRUCache();
19
+ const jitCache = typeof pluginOptions.cache !== 'undefined' ? pluginOptions.cache : lru__default["default"](DEFAULT_MAX, DEFAULT_TTL);
13
20
  function getCacheEntry(args) {
14
- var _a;
15
21
  let cacheEntry;
16
22
  const documentSource = documentSourceMap.get(args.document);
17
23
  if (documentSource) {
18
24
  cacheEntry = jitCache.get(documentSource);
19
25
  }
20
26
  if (!cacheEntry) {
21
- const compilationResult = graphqlJit.compileQuery(args.schema, args.document, (_a = args.operationName) !== null && _a !== void 0 ? _a : undefined, compilerOptions);
27
+ const compilationResult = graphqlJit.compileQuery(args.schema, args.document, args.operationName ?? undefined, compilerOptions);
22
28
  if (!graphqlJit.isCompiledQuery(compilationResult)) {
23
- if (pluginOptions === null || pluginOptions === void 0 ? void 0 : pluginOptions.onError) {
29
+ if (pluginOptions?.onError) {
24
30
  pluginOptions.onError(compilationResult);
25
31
  }
26
32
  else {
@@ -50,20 +56,20 @@ const useGraphQlJit = (compilerOptions = {}, pluginOptions = {}) => {
50
56
  },
51
57
  async onExecute({ args, setExecuteFn }) {
52
58
  if (!pluginOptions.enableIf || (pluginOptions.enableIf && (await pluginOptions.enableIf(args)))) {
53
- setExecuteFn(function jitExecutor() {
59
+ setExecuteFn(core.makeExecute(function jitExecutor(args) {
54
60
  const cacheEntry = getCacheEntry(args);
55
61
  return cacheEntry.query(args.rootValue, args.contextValue, args.variableValues);
56
- });
62
+ }));
57
63
  }
58
64
  },
59
65
  async onSubscribe({ args, setSubscribeFn }) {
60
66
  if (!pluginOptions.enableIf || (pluginOptions.enableIf && (await pluginOptions.enableIf(args)))) {
61
- setSubscribeFn(async function jitSubscriber() {
67
+ setSubscribeFn(core.makeSubscribe(async function jitSubscriber(args) {
62
68
  const cacheEntry = getCacheEntry(args);
63
69
  return cacheEntry.subscribe
64
70
  ? cacheEntry.subscribe(args.rootValue, args.contextValue, args.variableValues)
65
71
  : cacheEntry.query(args.rootValue, args.contextValue, args.variableValues);
66
- });
72
+ }));
67
73
  }
68
74
  },
69
75
  };
package/index.mjs CHANGED
@@ -1,22 +1,24 @@
1
- import { getInMemoryLRUCache } from '@envelop/core';
1
+ import { makeExecute, makeSubscribe } from '@envelop/core';
2
2
  import { Source } from 'graphql';
3
3
  import { compileQuery, isCompiledQuery } from 'graphql-jit';
4
+ import lru from 'tiny-lru';
4
5
 
5
6
  /* eslint-disable no-console */
7
+ const DEFAULT_MAX = 1000;
8
+ const DEFAULT_TTL = 3600000;
6
9
  const useGraphQlJit = (compilerOptions = {}, pluginOptions = {}) => {
7
10
  const documentSourceMap = new WeakMap();
8
- const jitCache = pluginOptions.cache != null ? pluginOptions.cache : getInMemoryLRUCache();
11
+ const jitCache = typeof pluginOptions.cache !== 'undefined' ? pluginOptions.cache : lru(DEFAULT_MAX, DEFAULT_TTL);
9
12
  function getCacheEntry(args) {
10
- var _a;
11
13
  let cacheEntry;
12
14
  const documentSource = documentSourceMap.get(args.document);
13
15
  if (documentSource) {
14
16
  cacheEntry = jitCache.get(documentSource);
15
17
  }
16
18
  if (!cacheEntry) {
17
- const compilationResult = compileQuery(args.schema, args.document, (_a = args.operationName) !== null && _a !== void 0 ? _a : undefined, compilerOptions);
19
+ const compilationResult = compileQuery(args.schema, args.document, args.operationName ?? undefined, compilerOptions);
18
20
  if (!isCompiledQuery(compilationResult)) {
19
- if (pluginOptions === null || pluginOptions === void 0 ? void 0 : pluginOptions.onError) {
21
+ if (pluginOptions?.onError) {
20
22
  pluginOptions.onError(compilationResult);
21
23
  }
22
24
  else {
@@ -46,20 +48,20 @@ const useGraphQlJit = (compilerOptions = {}, pluginOptions = {}) => {
46
48
  },
47
49
  async onExecute({ args, setExecuteFn }) {
48
50
  if (!pluginOptions.enableIf || (pluginOptions.enableIf && (await pluginOptions.enableIf(args)))) {
49
- setExecuteFn(function jitExecutor() {
51
+ setExecuteFn(makeExecute(function jitExecutor(args) {
50
52
  const cacheEntry = getCacheEntry(args);
51
53
  return cacheEntry.query(args.rootValue, args.contextValue, args.variableValues);
52
- });
54
+ }));
53
55
  }
54
56
  },
55
57
  async onSubscribe({ args, setSubscribeFn }) {
56
58
  if (!pluginOptions.enableIf || (pluginOptions.enableIf && (await pluginOptions.enableIf(args)))) {
57
- setSubscribeFn(async function jitSubscriber() {
59
+ setSubscribeFn(makeSubscribe(async function jitSubscriber(args) {
58
60
  const cacheEntry = getCacheEntry(args);
59
61
  return cacheEntry.subscribe
60
62
  ? cacheEntry.subscribe(args.rootValue, args.contextValue, args.variableValues)
61
63
  : cacheEntry.query(args.rootValue, args.contextValue, args.variableValues);
62
- });
64
+ }));
63
65
  }
64
66
  },
65
67
  };
package/package.json CHANGED
@@ -1,17 +1,18 @@
1
1
  {
2
2
  "name": "@envelop/graphql-jit",
3
- "version": "4.3.0-alpha-c0cc559.0",
3
+ "version": "5.0.0-alpha-d0d0776.0",
4
4
  "sideEffects": false,
5
5
  "peerDependencies": {
6
- "@envelop/core": "2.4.0-alpha-c0cc559.0",
6
+ "@envelop/core": "3.0.0-alpha-d0d0776.0",
7
7
  "graphql": "^14.0.0 || ^15.0.0 || ^16.0.0"
8
8
  },
9
9
  "dependencies": {
10
- "graphql-jit": "^0.7.0"
10
+ "graphql-jit": "^0.7.0",
11
+ "tiny-lru": "7.0.6"
11
12
  },
12
13
  "repository": {
13
14
  "type": "git",
14
- "url": "https://github.com/dotansimha/envelop.git",
15
+ "url": "https://github.com/n1ru4l/envelop.git",
15
16
  "directory": "packages/plugins/graphql-jit"
16
17
  },
17
18
  "author": "Dotan Simha <dotansimha@gmail.com>",
@@ -24,12 +25,24 @@
24
25
  },
25
26
  "exports": {
26
27
  ".": {
27
- "require": "./index.js",
28
- "import": "./index.mjs"
28
+ "require": {
29
+ "default": "./index.js",
30
+ "types": "./index.d.ts"
31
+ },
32
+ "import": {
33
+ "default": "./index.mjs",
34
+ "types": "./index.d.ts"
35
+ }
29
36
  },
30
37
  "./*": {
31
- "require": "./*.js",
32
- "import": "./*.mjs"
38
+ "require": {
39
+ "default": "./*.js",
40
+ "types": "./*.d.ts"
41
+ },
42
+ "import": {
43
+ "default": "./*.mjs",
44
+ "types": "./*.d.ts"
45
+ }
33
46
  },
34
47
  "./package.json": "./package.json"
35
48
  }