grafast 0.1.1-beta.2 → 0.1.1-beta.21

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.
Files changed (157) hide show
  1. package/README.md +2 -3
  2. package/dist/args.d.ts +4 -7
  3. package/dist/args.js +53 -38
  4. package/dist/assert.js +2 -3
  5. package/dist/bucket.d.ts +1 -89
  6. package/dist/config.d.ts +0 -17
  7. package/dist/config.js +1 -95
  8. package/dist/constraints.d.ts +15 -1
  9. package/dist/constraints.js +41 -3
  10. package/dist/deferred.js +1 -2
  11. package/dist/dev.d.ts +1 -4
  12. package/dist/dev.js +18 -3
  13. package/dist/engine/LayerPlan.d.ts +21 -111
  14. package/dist/engine/LayerPlan.js +196 -78
  15. package/dist/engine/OperationPlan.d.ts +30 -104
  16. package/dist/engine/OperationPlan.js +763 -471
  17. package/dist/engine/OutputPlan.d.ts +9 -20
  18. package/dist/engine/OutputPlan.js +432 -444
  19. package/dist/engine/StepTracker.d.ts +1 -95
  20. package/dist/engine/StepTracker.js +94 -20
  21. package/dist/engine/executeBucket.d.ts +5 -7
  22. package/dist/engine/executeBucket.js +646 -244
  23. package/dist/engine/executeOutputPlan.d.ts +0 -53
  24. package/dist/engine/executeOutputPlan.js +7 -2
  25. package/dist/engine/lib/defaultPlanResolver.d.ts +6 -0
  26. package/dist/engine/lib/defaultPlanResolver.js +11 -0
  27. package/dist/engine/lib/withGlobalLayerPlan.d.ts +2 -0
  28. package/dist/engine/lib/withGlobalLayerPlan.js +7 -4
  29. package/dist/engine/lock.d.ts +4 -0
  30. package/dist/engine/lock.js +82 -0
  31. package/dist/envelop.js +5 -4
  32. package/dist/error.d.ts +18 -49
  33. package/dist/error.js +50 -39
  34. package/dist/establishOperationPlan.js +57 -42
  35. package/dist/execute.d.ts +5 -4
  36. package/dist/execute.js +31 -15
  37. package/dist/exportAs.d.ts +1 -18
  38. package/dist/exportAs.js +2 -3
  39. package/dist/global.js +2 -3
  40. package/dist/grafastGraphql.d.ts +6 -1
  41. package/dist/grafastGraphql.js +35 -16
  42. package/dist/grafastPrint.d.ts +3 -0
  43. package/dist/grafastPrint.js +67 -6
  44. package/dist/graphqlCollectFields.d.ts +7 -34
  45. package/dist/graphqlCollectFields.js +24 -13
  46. package/dist/graphqlMergeSelectionSets.js +1 -2
  47. package/dist/index.d.ts +44 -29
  48. package/dist/index.js +91 -18
  49. package/dist/input.d.ts +1 -9
  50. package/dist/input.js +75 -28
  51. package/dist/inspect.d.ts +9 -3
  52. package/dist/inspect.js +3 -3
  53. package/dist/interfaces.d.ts +238 -93
  54. package/dist/interfaces.js +20 -3
  55. package/dist/makeGrafastSchema.d.ts +21 -9
  56. package/dist/makeGrafastSchema.js +353 -133
  57. package/dist/mermaid.d.ts +0 -17
  58. package/dist/mermaid.js +131 -19
  59. package/dist/middleware.d.ts +7 -0
  60. package/dist/middleware.js +24 -0
  61. package/dist/multistep.d.ts +19 -0
  62. package/dist/multistep.js +54 -0
  63. package/dist/operationPlan-input.d.ts +5 -4
  64. package/dist/operationPlan-input.js +140 -368
  65. package/dist/planJSONInterfaces.d.ts +12 -2
  66. package/dist/polymorphic.js +4 -5
  67. package/dist/prepare.d.ts +1 -7
  68. package/dist/prepare.js +100 -74
  69. package/dist/step.d.ts +118 -172
  70. package/dist/step.js +262 -183
  71. package/dist/steps/__flag.d.ts +65 -0
  72. package/dist/steps/__flag.js +261 -0
  73. package/dist/steps/__inputDefault.d.ts +14 -0
  74. package/dist/steps/__inputDefault.js +48 -0
  75. package/dist/steps/__inputDynamicScalar.d.ts +4 -6
  76. package/dist/steps/__inputDynamicScalar.js +4 -2
  77. package/dist/steps/__inputList.d.ts +5 -9
  78. package/dist/steps/__inputList.js +4 -1
  79. package/dist/steps/__inputObject.d.ts +7 -11
  80. package/dist/steps/__inputObject.js +49 -3
  81. package/dist/steps/__inputStaticLeaf.d.ts +2 -4
  82. package/dist/steps/__inputStaticLeaf.js +3 -1
  83. package/dist/steps/__item.d.ts +10 -12
  84. package/dist/steps/__item.js +8 -2
  85. package/dist/steps/__trackedValue.d.ts +7 -55
  86. package/dist/steps/__trackedValue.js +69 -8
  87. package/dist/steps/__value.d.ts +5 -4
  88. package/dist/steps/__value.js +5 -4
  89. package/dist/steps/access.d.ts +10 -10
  90. package/dist/steps/access.js +50 -17
  91. package/dist/steps/applyInput.d.ts +44 -0
  92. package/dist/steps/applyInput.js +206 -0
  93. package/dist/steps/applyTransforms.d.ts +2 -25
  94. package/dist/steps/applyTransforms.js +53 -31
  95. package/dist/steps/bakedInput.d.ts +23 -0
  96. package/dist/steps/bakedInput.js +90 -0
  97. package/dist/steps/condition.d.ts +31 -0
  98. package/dist/steps/condition.js +94 -0
  99. package/dist/steps/connection.d.ts +56 -42
  100. package/dist/steps/connection.js +97 -44
  101. package/dist/steps/constant.d.ts +7 -7
  102. package/dist/steps/constant.js +86 -7
  103. package/dist/steps/each.d.ts +3 -3
  104. package/dist/steps/each.js +2 -3
  105. package/dist/steps/error.d.ts +4 -4
  106. package/dist/steps/error.js +7 -6
  107. package/dist/steps/filter.d.ts +3 -2
  108. package/dist/steps/filter.js +1 -2
  109. package/dist/steps/first.d.ts +10 -9
  110. package/dist/steps/first.js +9 -13
  111. package/dist/steps/graphqlResolver.d.ts +3 -76
  112. package/dist/steps/graphqlResolver.js +89 -72
  113. package/dist/steps/groupBy.d.ts +3 -2
  114. package/dist/steps/groupBy.js +1 -2
  115. package/dist/steps/index.d.ts +12 -3
  116. package/dist/steps/index.js +48 -6
  117. package/dist/steps/lambda.d.ts +9 -9
  118. package/dist/steps/lambda.js +18 -9
  119. package/dist/steps/last.d.ts +9 -8
  120. package/dist/steps/last.js +7 -6
  121. package/dist/steps/list.d.ts +20 -9
  122. package/dist/steps/list.js +56 -13
  123. package/dist/steps/listTransform.d.ts +18 -22
  124. package/dist/steps/listTransform.js +58 -27
  125. package/dist/steps/listen.d.ts +6 -11
  126. package/dist/steps/listen.js +14 -16
  127. package/dist/steps/load.d.ts +42 -33
  128. package/dist/steps/load.js +60 -27
  129. package/dist/steps/node.d.ts +17 -10
  130. package/dist/steps/node.js +18 -6
  131. package/dist/steps/object.d.ts +33 -27
  132. package/dist/steps/object.js +53 -32
  133. package/dist/steps/partitionByIndex.d.ts +3 -2
  134. package/dist/steps/partitionByIndex.js +1 -2
  135. package/dist/steps/polymorphicBranch.d.ts +27 -0
  136. package/dist/steps/polymorphicBranch.js +62 -0
  137. package/dist/steps/proxy.d.ts +11 -12
  138. package/dist/steps/proxy.js +8 -5
  139. package/dist/steps/remapKeys.d.ts +10 -10
  140. package/dist/steps/remapKeys.js +14 -7
  141. package/dist/steps/reverse.d.ts +8 -8
  142. package/dist/steps/reverse.js +10 -7
  143. package/dist/steps/setter.d.ts +9 -18
  144. package/dist/steps/setter.js +24 -14
  145. package/dist/steps/sideEffect.d.ts +28 -0
  146. package/dist/steps/sideEffect.js +44 -0
  147. package/dist/stringifyPayload.js +1 -2
  148. package/dist/stripAnsi.js +2 -2
  149. package/dist/subscribe.d.ts +7 -1
  150. package/dist/subscribe.js +24 -9
  151. package/dist/thereCanBeOnlyOne.js +1 -1
  152. package/dist/timeSource.d.ts +0 -1
  153. package/dist/utils.d.ts +56 -55
  154. package/dist/utils.js +179 -59
  155. package/dist/version.d.ts +1 -1
  156. package/dist/version.js +1 -1
  157. package/package.json +5 -8
@@ -1,58 +1,9 @@
1
- import type { GraphQLError } from "graphql";
2
1
  import type { Bucket, RequestTools } from "../bucket.js";
3
- import type { JSONValue } from "../interfaces.js";
4
2
  import type { OutputPlan } from "./OutputPlan.js";
5
3
  export type OutputPath = Array<string | number>;
6
4
  export interface OutputStream {
7
5
  asyncIterable: AsyncIterableIterator<any>;
8
6
  }
9
- /**
10
- * PayloadRoot handles the root-level of a payload, it's ultimately responsible
11
- * for sharing the `data`/`errors` with the user. But it also handles the
12
- * intermediate payloads for stream/defer.
13
- *
14
- * A standard GraphQL query/mutation request (one that doesn't use
15
- * `@stream`/`@defer`) will have just one payload. A standard GraphQL
16
- * subscription request gets one payload per subscription event.
17
- *
18
- * When we mix `@stream` and `@defer` into this, we will require (often quite a
19
- * lot) more payloads.
20
- *
21
- * @internal
22
- */
23
- export interface PayloadRoot {
24
- /**
25
- * Serialization works differently if we're running inside GraphQL. (Namely:
26
- * we don't serialize - that's GraphQL's job.)
27
- */
28
- insideGraphQL: false;
29
- /**
30
- * The errors that have occurred; these are proper GraphQLErrors and will be
31
- * returned directly to clients so they must be complete.
32
- */
33
- errors: GraphQLError[];
34
- /**
35
- * Defer queue - we don't start executing these until after the main
36
- * payload is completed (to allow for a non-null boundary to abort
37
- * execution).
38
- */
39
- queue: Array<SubsequentPayloadSpec>;
40
- /**
41
- * Stream queue - we don't start executing these until after the main
42
- * payload is completed (to allow for a non-null boundary to abort
43
- * execution).
44
- */
45
- streams: Array<SubsequentStreamSpec>;
46
- /**
47
- * VERY DANGEROUS. This is _only_ to pass variables through to introspection
48
- * selections, it shouldn't be used for anything else.
49
- *
50
- * @internal
51
- * */
52
- variables: {
53
- [key: string]: any;
54
- };
55
- }
56
7
  export interface OutputPlanContext {
57
8
  requestContext: RequestTools;
58
9
  root: PayloadRoot;
@@ -76,8 +27,4 @@ export interface SubsequentStreamSpec {
76
27
  stream: AsyncIterator<any> | Iterator<any>;
77
28
  startIndex: number;
78
29
  }
79
- /**
80
- * @internal
81
- */
82
- export declare function executeOutputPlan(ctx: OutputPlanContext, outputPlan: OutputPlan, bucket: Bucket, bucketIndex: number, outputDataAsString: boolean): JSONValue;
83
30
  //# sourceMappingURL=executeOutputPlan.d.ts.map
@@ -1,13 +1,19 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.executeOutputPlan = void 0;
3
+ exports.executeOutputPlan = executeOutputPlan;
4
4
  const tslib_1 = require("tslib");
5
+ const debug_1 = tslib_1.__importDefault(require("debug"));
5
6
  const assert = tslib_1.__importStar(require("../assert.js"));
6
7
  const dev_js_1 = require("../dev.js");
8
+ const debug = (0, debug_1.default)("grafast:OutputPlan");
9
+ const debugVerbose = debug.extend("verbose");
7
10
  /**
8
11
  * @internal
9
12
  */
10
13
  function executeOutputPlan(ctx, outputPlan, bucket, bucketIndex, outputDataAsString) {
14
+ if (debugVerbose.enabled) {
15
+ debugVerbose("Executing %c with data:\n%c", outputPlan, bucket);
16
+ }
11
17
  if (dev_js_1.isDev) {
12
18
  assert.strictEqual(bucket.isComplete, true, "Can only process an output plan for a completed bucket");
13
19
  }
@@ -17,5 +23,4 @@ function executeOutputPlan(ctx, outputPlan, bucket, bucketIndex, outputDataAsStr
17
23
  ? outputPlan.executeString(ctx.root, mutablePath, bucket, bucketIndex)
18
24
  : outputPlan.execute(ctx.root, mutablePath, bucket, bucketIndex);
19
25
  }
20
- exports.executeOutputPlan = executeOutputPlan;
21
26
  //# sourceMappingURL=executeOutputPlan.js.map
@@ -0,0 +1,6 @@
1
+ import type { FieldPlanResolver } from "../../interfaces.js";
2
+ import type { Step } from "../../step.js";
3
+ export declare const defaultPlanResolver: FieldPlanResolver<any, Step & {
4
+ get?: (field: string) => Step;
5
+ }, any>;
6
+ //# sourceMappingURL=defaultPlanResolver.d.ts.map
@@ -0,0 +1,11 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.defaultPlanResolver = void 0;
4
+ const access_js_1 = require("../../steps/access.js");
5
+ const defaultPlanResolver = ($step, _, { fieldName }) => {
6
+ return typeof $step.get === "function"
7
+ ? $step.get(fieldName)
8
+ : (0, access_js_1.access)($step, [fieldName]);
9
+ };
10
+ exports.defaultPlanResolver = defaultPlanResolver;
11
+ //# sourceMappingURL=defaultPlanResolver.js.map
@@ -1,5 +1,7 @@
1
+ import type { Step } from "../..";
1
2
  import type { LayerPlan } from "../LayerPlan";
2
3
  export declare function withGlobalLayerPlan<T, TThis = any, TArgs extends [...args: any[]] = [...args: any[]]>(layerPlan: LayerPlan, polymorphicPaths: ReadonlySet<string> | null, callback: (this: TThis, ...args: TArgs) => T, callbackThis?: TThis, ...callbackArgs: TArgs): T;
3
4
  export declare function currentLayerPlan(): LayerPlan;
4
5
  export declare function currentPolymorphicPaths(): ReadonlySet<string> | null;
6
+ export declare function isUnaryStep($step: Step): boolean;
5
7
  //# sourceMappingURL=withGlobalLayerPlan.d.ts.map
@@ -1,6 +1,9 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.currentPolymorphicPaths = exports.currentLayerPlan = exports.withGlobalLayerPlan = void 0;
3
+ exports.withGlobalLayerPlan = withGlobalLayerPlan;
4
+ exports.currentLayerPlan = currentLayerPlan;
5
+ exports.currentPolymorphicPaths = currentPolymorphicPaths;
6
+ exports.isUnaryStep = isUnaryStep;
4
7
  let globalData_layerPlan = undefined;
5
8
  let globalData_polymorphicPaths = undefined;
6
9
  function withGlobalLayerPlan(layerPlan, polymorphicPaths, callback, callbackThis, ...callbackArgs) {
@@ -16,7 +19,6 @@ function withGlobalLayerPlan(layerPlan, polymorphicPaths, callback, callbackThis
16
19
  globalData_polymorphicPaths = oldPolymorphicPaths;
17
20
  }
18
21
  }
19
- exports.withGlobalLayerPlan = withGlobalLayerPlan;
20
22
  function currentLayerPlan() {
21
23
  if (globalData_layerPlan === undefined) {
22
24
  throw new Error(
@@ -25,12 +27,13 @@ function currentLayerPlan() {
25
27
  }
26
28
  return globalData_layerPlan;
27
29
  }
28
- exports.currentLayerPlan = currentLayerPlan;
29
30
  function currentPolymorphicPaths() {
30
31
  if (globalData_polymorphicPaths === undefined) {
31
32
  throw new Error("GrafastInternalError<b0b05743-8b21-42c6-9b53-925013d88bd1>: currentPolymorphicPaths called out of turn; must only called within a withGlobalLayerPlan callback");
32
33
  }
33
34
  return globalData_polymorphicPaths;
34
35
  }
35
- exports.currentPolymorphicPaths = currentPolymorphicPaths;
36
+ function isUnaryStep($step) {
37
+ return $step._isUnary;
38
+ }
36
39
  //# sourceMappingURL=withGlobalLayerPlan.js.map
@@ -0,0 +1,4 @@
1
+ import type { Step } from "..";
2
+ export declare function lock($step: Step): void;
3
+ export declare function unlock($step: Step): boolean;
4
+ //# sourceMappingURL=lock.d.ts.map
@@ -0,0 +1,82 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.$$unlock = void 0;
4
+ exports.lock = lock;
5
+ exports.unlock = unlock;
6
+ /**
7
+ * Used internally to prevent steps using other steps' optimize/finalize/etc
8
+ * methods.
9
+ *
10
+ * @internal
11
+ */
12
+ exports.$$unlock = Symbol("unlock");
13
+ function isLocked($step) {
14
+ return $step[exports.$$unlock] !== undefined;
15
+ }
16
+ function lock($step) {
17
+ if (!isLocked($step)) {
18
+ const { optimize, finalize, deduplicate, deduplicatedWith } = $step;
19
+ if (optimize) {
20
+ $step.optimize = optimizeLocked;
21
+ }
22
+ if (typeof finalize === "function") {
23
+ $step.finalize = finalizeLocked;
24
+ }
25
+ if (deduplicate) {
26
+ $step.deduplicate = deduplicateLocked;
27
+ }
28
+ if (deduplicatedWith) {
29
+ $step.deduplicatedWith = deduplicatedWithLocked;
30
+ }
31
+ $step[exports.$$unlock] = () => {
32
+ $step[exports.$$unlock] = undefined;
33
+ if (optimize && $step.optimize !== optimizeLocked) {
34
+ console.warn(`Warning: ${$step}'s optimize method changed whilst locked`);
35
+ }
36
+ else {
37
+ $step.optimize = optimize;
38
+ }
39
+ if (typeof finalize === "function" && $step.finalize !== finalizeLocked) {
40
+ console.warn(`Warning: ${$step}'s finalize method changed whilst locked`);
41
+ }
42
+ else {
43
+ $step.finalize = finalize;
44
+ }
45
+ if (deduplicate && $step.deduplicate !== deduplicateLocked) {
46
+ console.warn(`Warning: ${$step}'s deduplicate method changed whilst locked`);
47
+ }
48
+ else {
49
+ $step.deduplicate = deduplicate;
50
+ }
51
+ if (deduplicatedWith &&
52
+ $step.deduplicatedWith !== deduplicatedWithLocked) {
53
+ console.warn(`Warning: ${$step}'s deduplicatedWith method changed whilst locked`);
54
+ }
55
+ else {
56
+ $step.deduplicatedWith = deduplicatedWith;
57
+ }
58
+ };
59
+ }
60
+ }
61
+ function unlock($step) {
62
+ if (isLocked($step)) {
63
+ $step[exports.$$unlock]();
64
+ return true;
65
+ }
66
+ else {
67
+ return false;
68
+ }
69
+ }
70
+ function optimizeLocked() {
71
+ throw new Error(`Only Grafast may call a step's optimize method; rather than calling optimize on the steps your class depends on, consider opting in to multiple optimization passes via \`allowMultipleOptimizations\`.`);
72
+ }
73
+ function finalizeLocked() {
74
+ throw new Error(`Only Grafast may call a step's finalize method; steps should not attempt to interact with other steps during finalize.`);
75
+ }
76
+ function deduplicateLocked() {
77
+ throw new Error(`Only Grafast may call a step's deduplicate method.`);
78
+ }
79
+ function deduplicatedWithLocked() {
80
+ throw new Error(`Only Grafast may call a step's deduplicatedWith method.`);
81
+ }
82
+ //# sourceMappingURL=lock.js.map
package/dist/envelop.js CHANGED
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.useMoreDetailedErrors = exports.useGrafast = void 0;
4
+ const graphile_config_1 = require("graphile-config");
4
5
  const execute_js_1 = require("./execute.js");
5
6
  const stripAnsi_js_1 = require("./stripAnsi.js");
6
7
  const subscribe_js_1 = require("./subscribe.js");
@@ -33,9 +34,9 @@ const useGrafast = (options = {}) => {
33
34
  async onExecute(opts) {
34
35
  const explainHeaders = opts.args.contextValue?.req?.headers["x-graphql-explain"];
35
36
  const explain = processExplain(explainAllowed, explainHeaders);
36
- opts.setExecuteFn((args) => (0, execute_js_1.execute)(args, {
37
+ opts.setExecuteFn((args) => (0, execute_js_1.execute)(args, (0, graphile_config_1.resolvePreset)({
37
38
  grafast: { explain },
38
- }));
39
+ })));
39
40
  },
40
41
  async onSubscribe(opts) {
41
42
  const ctx = opts.args.contextValue;
@@ -43,9 +44,9 @@ const useGrafast = (options = {}) => {
43
44
  ctx?.request?.headers ||
44
45
  ctx?.connectionParams)?.["x-graphql-explain"];
45
46
  const explain = processExplain(explainAllowed, explainHeaders);
46
- opts.setSubscribeFn(async (args) => (0, subscribe_js_1.subscribe)(args, {
47
+ opts.setSubscribeFn(async (args) => (0, subscribe_js_1.subscribe)(args, (0, graphile_config_1.resolvePreset)({
47
48
  grafast: { explain },
48
- }));
49
+ })));
49
50
  },
50
51
  };
51
52
  };
package/dist/error.d.ts CHANGED
@@ -1,62 +1,31 @@
1
- /// <reference types="node" />
2
- import type { $$extensions } from "./interfaces.js";
1
+ import type { ExecutionEntryFlags } from "./interfaces.js";
3
2
  import { $$safeError } from "./interfaces.js";
3
+ declare const $$flagged: unique symbol;
4
4
  /**
5
- * Internally we wrap errors that occur in a GrafastError; this allows us to do
6
- * simple `instanceof` checks to see if a value is an actual value or an error.
7
- * Users should never use this class; it's for internal usage only.
8
- *
9
- * @internal
5
+ * Wrapper for errors to return (rather than throw or reject) from user code.
10
6
  */
11
- export interface GrafastError extends Error {
12
- originalError: Error;
13
- [$$extensions]?: Record<string, any>;
7
+ export interface FlaggedValue<TValue = any> {
8
+ [$$flagged]: true;
9
+ flags: ExecutionEntryFlags;
10
+ value: TValue;
11
+ planId: number | null;
12
+ toString(): string;
14
13
  }
15
- export declare const $$error: unique symbol;
14
+ export declare const $$inhibit: FlaggedValue<null>;
16
15
  /**
17
- * When an error occurs during plan execution we wrap it in a GrafastError so
18
- * that we can pass it around as a value. It gets unwrapped and thrown in the
19
- * grafast resolver.
20
- *
21
- * @internal
16
+ * Used to wrap error values to have Grafast treat them as if they were
17
+ * thrown/rejected (rather than just regular values).
22
18
  */
23
- export declare const _GrafastError: {
24
- new (originalError: Error, planId: number | null): {
25
- readonly originalError: Error;
26
- extensions: Record<string, any>;
27
- [$$error]: boolean;
28
- name: string;
29
- message: string;
30
- stack?: string | undefined;
31
- cause?: unknown;
32
- };
33
- captureStackTrace(targetObject: object, constructorOpt?: Function | undefined): void;
34
- prepareStackTrace?: ((err: Error, stackTraces: NodeJS.CallSite[]) => any) | undefined;
35
- stackTraceLimit: number;
36
- };
37
- /**
38
- * DO NOT ALLOW CONSTRUCTION OF ERRORS OUTSIDE OF THIS MODULE!
39
- *
40
- * @internal
41
- */
42
- export declare function newGrafastError(error: Error, planId: number | null): {
43
- readonly originalError: Error;
44
- extensions: Record<string, any>;
45
- [$$error]: boolean;
46
- name: string;
47
- message: string;
48
- stack?: string | undefined;
49
- cause?: unknown;
50
- };
51
- /**
52
- * Is the given value a GrafastError? This is the only public API that people
53
- * should use for looking at GrafastErrors.
54
- */
55
- export declare function isGrafastError(value: any): value is GrafastError;
19
+ export declare function flagError<TError extends Error = Error>(value: TError, planId?: number | null): FlaggedValue<TError>;
56
20
  export declare class SafeError<TExtensions extends Record<string, any> | undefined = Record<string, any> | undefined> extends Error {
57
21
  extensions: TExtensions;
22
+ static $$export: {
23
+ moduleName: string;
24
+ exportName: string;
25
+ };
58
26
  [$$safeError]: boolean;
59
27
  constructor(message: string, extensions?: TExtensions, errorOptions?: ErrorOptions);
60
28
  }
61
29
  export declare function isSafeError(error: Error): error is SafeError;
30
+ export {};
62
31
  //# sourceMappingURL=error.d.ts.map
package/dist/error.js CHANGED
@@ -1,56 +1,68 @@
1
1
  "use strict";
2
- var _a, _b;
2
+ var _a;
3
3
  Object.defineProperty(exports, "__esModule", { value: true });
4
- exports.isSafeError = exports.SafeError = exports.isGrafastError = exports.newGrafastError = exports._GrafastError = exports.$$error = void 0;
4
+ exports.SafeError = exports.$$inhibit = void 0;
5
+ exports.flagError = flagError;
6
+ exports.isFlaggedValue = isFlaggedValue;
7
+ exports.isSafeError = isSafeError;
8
+ const dev_js_1 = require("./dev.js");
9
+ const inspect_js_1 = require("./inspect.js");
5
10
  const interfaces_js_1 = require("./interfaces.js");
6
- exports.$$error = Symbol("isGrafastError");
7
- // IMPORTANT: this WILL NOT WORK when compiled down to ES5. It requires ES6+
8
- // native class support.
9
- /**
10
- * When an error occurs during plan execution we wrap it in a GrafastError so
11
- * that we can pass it around as a value. It gets unwrapped and thrown in the
12
- * grafast resolver.
13
- *
14
- * @internal
15
- */
16
- const _GrafastError = class GrafastError extends Error {
17
- static { _a = exports.$$error; }
18
- constructor(originalError, planId) {
19
- if (originalError instanceof exports._GrafastError) {
20
- throw new Error("GrafastInternalError<62505509-8b21-4ef7-80f5-d0f99873174b>: attempted to wrap a GrafastError with a GrafastError.");
11
+ const $$flagged = Symbol("grafastFlaggedValue");
12
+ function flaggedValueToString() {
13
+ if (this.flags & interfaces_js_1.FLAG_ERROR && this.value instanceof Error) {
14
+ return String(this.value);
15
+ }
16
+ else if (this.flags & interfaces_js_1.FLAG_INHIBITED && this.value === null) {
17
+ return "INHIBIT";
18
+ }
19
+ else {
20
+ return `${this.flags}/${(0, inspect_js_1.inspect)(this.value)}`;
21
+ }
22
+ }
23
+ function flaggedValue(flags, value, planId) {
24
+ if (dev_js_1.isDev) {
25
+ if (value === null && !(flags & interfaces_js_1.FLAG_NULL)) {
26
+ throw new Error(`flaggedValue called with null, but not flagged as null.`);
27
+ }
28
+ if (value === null && !(flags & interfaces_js_1.FLAG_INHIBITED)) {
29
+ throw new Error(`flaggedValue called with null, but not flagged as inhibited.`);
21
30
  }
22
- const message = originalError?.message;
23
- super(message);
24
- this[_a] = true;
25
- Object.setPrototypeOf(this, GrafastError.prototype);
26
- this.originalError = originalError;
27
- this.extensions = { grafast: { planId } };
28
31
  }
29
- };
30
- exports._GrafastError = _GrafastError;
32
+ return {
33
+ [$$flagged]: true,
34
+ flags,
35
+ value,
36
+ planId,
37
+ toString: flaggedValueToString,
38
+ };
39
+ }
40
+ exports.$$inhibit = flaggedValue(interfaces_js_1.FLAG_NULL | interfaces_js_1.FLAG_INHIBITED, null, null);
31
41
  /**
32
- * DO NOT ALLOW CONSTRUCTION OF ERRORS OUTSIDE OF THIS MODULE!
33
- *
34
- * @internal
42
+ * Used to wrap error values to have Grafast treat them as if they were
43
+ * thrown/rejected (rather than just regular values).
35
44
  */
36
- function newGrafastError(error, planId) {
37
- return new exports._GrafastError(error, planId);
45
+ function flagError(value, planId = null) {
46
+ return flaggedValue(interfaces_js_1.FLAG_ERROR, value, planId);
38
47
  }
39
- exports.newGrafastError = newGrafastError;
40
48
  /**
41
- * Is the given value a GrafastError? This is the only public API that people
42
- * should use for looking at GrafastErrors.
49
+ * Is this a flagged value?
50
+ *
51
+ * @internal
43
52
  */
44
- function isGrafastError(value) {
45
- return typeof value === "object" && value !== null && exports.$$error in value;
53
+ function isFlaggedValue(value) {
54
+ return Object.hasOwn(value, $$flagged);
46
55
  }
47
- exports.isGrafastError = isGrafastError;
48
56
  class SafeError extends Error {
49
- static { _b = interfaces_js_1.$$safeError; }
57
+ static { _a = interfaces_js_1.$$safeError; }
58
+ static { this.$$export = {
59
+ moduleName: "grafast",
60
+ exportName: "SafeError",
61
+ }; }
50
62
  constructor(message, extensions = undefined, errorOptions) {
51
63
  super(message, errorOptions);
52
64
  this.extensions = extensions;
53
- this[_b] = true;
65
+ this[_a] = true;
54
66
  Object.setPrototypeOf(this, SafeError.prototype);
55
67
  }
56
68
  }
@@ -58,5 +70,4 @@ exports.SafeError = SafeError;
58
70
  function isSafeError(error) {
59
71
  return error[interfaces_js_1.$$safeError];
60
72
  }
61
- exports.isSafeError = isSafeError;
62
73
  //# sourceMappingURL=error.js.map
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.establishOperationPlan = void 0;
3
+ exports.establishOperationPlan = establishOperationPlan;
4
4
  const tslib_1 = require("tslib");
5
5
  const lru_1 = tslib_1.__importDefault(require("@graphile/lru"));
6
6
  const debug_1 = tslib_1.__importDefault(require("debug"));
@@ -14,7 +14,7 @@ const debug = (0, debug_1.default)("grafast:establishOperationPlan");
14
14
  const TIMEOUT_TIMEOUT = (typeof process !== "undefined" &&
15
15
  typeof process.env.GRAFAST_TIMEOUT_VALIDITY_MS === "string"
16
16
  ? parseInt(process.env.GRAFAST_TIMEOUT_VALIDITY_MS, 10)
17
- : null) || 60000;
17
+ : null) || 60_000;
18
18
  // OPTIMIZE: we should consider increasing the timeout once the process has been
19
19
  // running a while (since the JIT should have kicked in) - we could even use
20
20
  // `setTimeout` to trigger it after certain amount of time elapsed.
@@ -50,7 +50,7 @@ const assertFragmentsMatch = !dev_js_1.isDev ? dev_js_1.noop : reallyAssertFragm
50
50
  * @remarks Due to the optimisation in `establishOperationPlan`, the schema, document
51
51
  * and operationName checks have already been performed.
52
52
  */
53
- function isOperationPlanCompatible(operationPlan, variableValues, context, rootValue) {
53
+ function isOperationPlanResultCompatible(operationPlan, variableValues, context, rootValue) {
54
54
  const { variableValuesConstraints, contextConstraints, rootValueConstraints, } = operationPlan;
55
55
  if (!(0, constraints_js_1.matchesConstraints)(variableValuesConstraints, variableValues)) {
56
56
  return false;
@@ -84,49 +84,54 @@ function establishOperationPlan(schema, operation, fragments, variableValues, co
84
84
  let linkedItem = cache.possibleOperationPlans;
85
85
  while (linkedItem) {
86
86
  const value = linkedItem.value;
87
- if (value instanceof index_js_1.SafeError) {
88
- if (value.extensions?.[interfaces_js_1.$$timeout] != null) {
89
- if (value.extensions[interfaces_js_1.$$ts] < timeSource_js_1.timeSource.now() - TIMEOUT_TIMEOUT) {
90
- // Remove this out of date timeout
91
- linkedItem = linkedItem.next;
92
- if (previousItem !== null) {
93
- previousItem.next = linkedItem;
87
+ if (isOperationPlanResultCompatible(value, variableValues, context, rootValue)) {
88
+ const { error, operationPlan } = value;
89
+ if (error != null) {
90
+ if (error instanceof index_js_1.SafeError) {
91
+ if (error.extensions?.[interfaces_js_1.$$timeout] != null) {
92
+ if (error.extensions[interfaces_js_1.$$ts] < timeSource_js_1.timeSource.now() - TIMEOUT_TIMEOUT) {
93
+ // Remove this out of date timeout
94
+ linkedItem = linkedItem.next;
95
+ if (previousItem !== null) {
96
+ previousItem.next = linkedItem;
97
+ }
98
+ else {
99
+ cache.possibleOperationPlans = linkedItem;
100
+ }
101
+ continue;
102
+ }
103
+ if (planningTimeout !== null &&
104
+ error.extensions[interfaces_js_1.$$timeout] >= planningTimeout) {
105
+ // It was a timeout error - do not retry
106
+ throw error;
107
+ }
108
+ else {
109
+ // That's Not My Timeout, let's try again.
110
+ }
94
111
  }
95
112
  else {
96
- cache.possibleOperationPlans = linkedItem;
113
+ // Not a timeout error - this will always fail in the same way?
114
+ throw error;
97
115
  }
98
- continue;
99
- }
100
- if (planningTimeout !== null &&
101
- value.extensions[interfaces_js_1.$$timeout] >= planningTimeout) {
102
- // It was a timeout error - do not retry
103
- throw value;
104
116
  }
105
117
  else {
106
- // That's Not My Timeout, let's try again.
118
+ // Not a timeout error - this will always fail in the same way?
119
+ throw error;
107
120
  }
108
121
  }
109
122
  else {
110
- // Not a timeout error - this will always fail in the same way?
111
- throw value;
112
- }
113
- }
114
- else if (value instanceof Error) {
115
- // Not a timeout error - this will always fail in the same way?
116
- throw value;
117
- }
118
- else if (isOperationPlanCompatible(value, variableValues, context, rootValue)) {
119
- // Hoist to top of linked list
120
- if (previousItem !== null) {
121
- // Remove linkedItem from existing chain
122
- previousItem.next = linkedItem.next;
123
- // Add rest of chain after linkedItem
124
- linkedItem.next = cache.possibleOperationPlans;
125
- // linkedItem is now head of chain
126
- cache.possibleOperationPlans = linkedItem;
123
+ // Hoist to top of linked list
124
+ if (previousItem !== null) {
125
+ // Remove linkedItem from existing chain
126
+ previousItem.next = linkedItem.next;
127
+ // Add rest of chain after linkedItem
128
+ linkedItem.next = cache.possibleOperationPlans;
129
+ // linkedItem is now head of chain
130
+ cache.possibleOperationPlans = linkedItem;
131
+ }
132
+ // We found a suitable OperationPlan - use that!
133
+ return operationPlan;
127
134
  }
128
- // We found a suitable OperationPlan - use that!
129
- return value;
130
135
  }
131
136
  count++;
132
137
  lastButOneItem = previousItem;
@@ -137,8 +142,11 @@ function establishOperationPlan(schema, operation, fragments, variableValues, co
137
142
  // No suitable OperationPlan found, time to make one.
138
143
  let operationPlan;
139
144
  let error;
145
+ const variableValuesConstraints = [];
146
+ const contextConstraints = [];
147
+ const rootValueConstraints = [];
140
148
  try {
141
- operationPlan = new index_js_1.OperationPlan(schema, operation, fragments, variableValues, context, rootValue, planningTimeout);
149
+ operationPlan = new index_js_1.OperationPlan(schema, operation, fragments, variableValuesConstraints, variableValues, contextConstraints, context, rootValueConstraints, rootValue, planningTimeout);
142
150
  }
143
151
  catch (e) {
144
152
  error = e;
@@ -153,11 +161,19 @@ function establishOperationPlan(schema, operation, fragments, variableValues, co
153
161
  });
154
162
  schema.extensions.grafast[interfaces_js_1.$$cacheByOperation] = cacheByOperation;
155
163
  }
156
- const operationPlanOrError = operationPlan ?? error;
164
+ const establishOperationPlanResult = {
165
+ variableValuesConstraints,
166
+ contextConstraints,
167
+ rootValueConstraints,
168
+ ...(operationPlan ? { operationPlan } : { error: error }),
169
+ };
157
170
  if (!cache) {
158
171
  cache = {
159
172
  fragments,
160
- possibleOperationPlans: { value: operationPlanOrError, next: null },
173
+ possibleOperationPlans: {
174
+ value: establishOperationPlanResult,
175
+ next: null,
176
+ },
161
177
  };
162
178
  cacheByOperation.set(operation, cache);
163
179
  }
@@ -171,7 +187,7 @@ function establishOperationPlan(schema, operation, fragments, variableValues, co
171
187
  }
172
188
  // Add new operationPlan to top of the linked list.
173
189
  cache.possibleOperationPlans = {
174
- value: operationPlanOrError,
190
+ value: establishOperationPlanResult,
175
191
  next: cache.possibleOperationPlans,
176
192
  };
177
193
  }
@@ -182,5 +198,4 @@ function establishOperationPlan(schema, operation, fragments, variableValues, co
182
198
  return operationPlan;
183
199
  }
184
200
  }
185
- exports.establishOperationPlan = establishOperationPlan;
186
201
  //# sourceMappingURL=establishOperationPlan.js.map
package/dist/execute.d.ts CHANGED
@@ -1,13 +1,14 @@
1
1
  import type { AsyncExecutionResult, ExecutionArgs, ExecutionResult } from "graphql";
2
2
  import type { PromiseOrValue } from "graphql/jsutils/PromiseOrValue";
3
+ import type { GrafastExecutionArgs } from "./interfaces.js";
3
4
  /**
4
- * Used by `execute` and `subscribe`.
5
- * @internal
5
+ * @deprecated Second and third parameters should be passed as part of args,
6
+ * specifically `resolvedPreset` and `outputDataAsString`.
6
7
  */
7
- export declare function withGrafastArgs(args: ExecutionArgs, resolvedPreset: GraphileConfig.ResolvedPreset, outputDataAsString: boolean): PromiseOrValue<ExecutionResult | AsyncGenerator<AsyncExecutionResult, void, void>>;
8
+ export declare function execute(args: ExecutionArgs, resolvedPreset: GraphileConfig.ResolvedPreset | undefined, outputDataAsString?: boolean): PromiseOrValue<ExecutionResult | AsyncGenerator<AsyncExecutionResult, void, undefined>>;
8
9
  /**
9
10
  * Use this instead of GraphQL.js' execute method and we'll automatically
10
11
  * run grafastPrepare for you and handle the result.
11
12
  */
12
- export declare function execute(args: ExecutionArgs, resolvedPreset?: GraphileConfig.ResolvedPreset, outputDataAsString?: boolean): PromiseOrValue<ExecutionResult | AsyncGenerator<AsyncExecutionResult, void, undefined>>;
13
+ export declare function execute(args: GrafastExecutionArgs): PromiseOrValue<ExecutionResult | AsyncGenerator<AsyncExecutionResult, void, undefined>>;
13
14
  //# sourceMappingURL=execute.d.ts.map