@graphql-tools/executor-envelop 2.0.2 → 2.0.3

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
@@ -1,7 +1,10 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.useExecutor = void 0;
4
- function useExecutor(executor) {
4
+ const utils_1 = require("@graphql-tools/utils");
5
+ const wrap_1 = require("@graphql-tools/wrap");
6
+ function useExecutor(executor, opts) {
7
+ const EMPTY_ARRAY = Object.freeze([]);
5
8
  function executorToExecuteFn(executionArgs) {
6
9
  return executor({
7
10
  document: executionArgs.document,
@@ -11,13 +14,99 @@ function useExecutor(executor) {
11
14
  operationName: executionArgs.operationName,
12
15
  });
13
16
  }
17
+ const pluginCtx = {
18
+ schema$: undefined,
19
+ schema: undefined,
20
+ schemaSetPromise$: undefined,
21
+ skipIntrospection: false,
22
+ };
23
+ if (opts?.polling) {
24
+ setInterval(() => {
25
+ pluginCtx.schema$ = undefined;
26
+ pluginCtx.schema = undefined;
27
+ }, opts.polling);
28
+ }
29
+ function ensureSchema(ctx) {
30
+ if (pluginCtx.skipIntrospection) {
31
+ return;
32
+ }
33
+ try {
34
+ if (!pluginCtx.schema && !pluginCtx.schemaSetPromise$) {
35
+ pluginCtx.schema$ ||= (0, wrap_1.schemaFromExecutor)(executor, ctx, opts);
36
+ if ((0, utils_1.isPromise)(pluginCtx.schema$)) {
37
+ pluginCtx.schemaSetPromise$ = pluginCtx.schema$.then(newSchema => {
38
+ pluginCtx.schema = newSchema;
39
+ }).catch?.(err => {
40
+ console.warn(`Introspection failed, skipping introspection due to the following errors;\n`, err);
41
+ pluginCtx.skipIntrospection = true;
42
+ });
43
+ }
44
+ else {
45
+ pluginCtx.schema = pluginCtx.schema$;
46
+ }
47
+ }
48
+ }
49
+ catch (err) {
50
+ pluginCtx.skipIntrospection = true;
51
+ console.warn(`Introspection failed, skipping introspection due to the following errors;\n`, err);
52
+ }
53
+ }
14
54
  return {
15
- onExecute({ setExecuteFn }) {
55
+ onEnveloped({ context, setSchema }) {
56
+ ensureSchema(context);
57
+ if (pluginCtx.schema) {
58
+ setSchema(pluginCtx.schema);
59
+ }
60
+ },
61
+ onContextBuilding() {
62
+ ensureSchema();
63
+ if (pluginCtx.schemaSetPromise$) {
64
+ return pluginCtx.schemaSetPromise$;
65
+ }
66
+ },
67
+ onExecute({ args, setExecuteFn }) {
68
+ if (args.schema) {
69
+ pluginCtx.schema = args.schema;
70
+ pluginCtx.schema$ = pluginCtx.schema;
71
+ }
72
+ ensureSchema(args.contextValue);
73
+ if ((0, utils_1.isPromise)(pluginCtx.schemaSetPromise$)) {
74
+ return pluginCtx.schemaSetPromise$.then(() => {
75
+ setExecuteFn(executorToExecuteFn);
76
+ });
77
+ }
16
78
  setExecuteFn(executorToExecuteFn);
17
79
  },
18
- onSubscribe({ setSubscribeFn }) {
80
+ onSubscribe({ args, setSubscribeFn }) {
81
+ if (args.schema) {
82
+ pluginCtx.schema = args.schema;
83
+ pluginCtx.schema$ = pluginCtx.schema;
84
+ }
85
+ ensureSchema(args.contextValue);
86
+ if ((0, utils_1.isPromise)(pluginCtx.schemaSetPromise$)) {
87
+ return pluginCtx.schemaSetPromise$.then(() => {
88
+ setSubscribeFn(executorToExecuteFn);
89
+ });
90
+ }
19
91
  setSubscribeFn(executorToExecuteFn);
20
92
  },
93
+ onValidate({ params, context, setResult }) {
94
+ if (params.schema) {
95
+ pluginCtx.schema = params.schema;
96
+ pluginCtx.schema$ = pluginCtx.schema;
97
+ }
98
+ ensureSchema(context);
99
+ if (pluginCtx.schema?.[Symbol.toStringTag] !== 'GraphQLSchema') {
100
+ setResult(EMPTY_ARRAY);
101
+ }
102
+ },
103
+ pluginCtx,
104
+ ensureSchema,
105
+ invalidateSupergraph() {
106
+ pluginCtx.schema$ = undefined;
107
+ pluginCtx.schema = undefined;
108
+ pluginCtx.skipIntrospection = false;
109
+ },
21
110
  };
22
111
  }
23
112
  exports.useExecutor = useExecutor;
package/esm/index.js CHANGED
@@ -1,4 +1,7 @@
1
- export function useExecutor(executor) {
1
+ import { isPromise } from '@graphql-tools/utils';
2
+ import { schemaFromExecutor } from '@graphql-tools/wrap';
3
+ export function useExecutor(executor, opts) {
4
+ const EMPTY_ARRAY = Object.freeze([]);
2
5
  function executorToExecuteFn(executionArgs) {
3
6
  return executor({
4
7
  document: executionArgs.document,
@@ -8,12 +11,98 @@ export function useExecutor(executor) {
8
11
  operationName: executionArgs.operationName,
9
12
  });
10
13
  }
14
+ const pluginCtx = {
15
+ schema$: undefined,
16
+ schema: undefined,
17
+ schemaSetPromise$: undefined,
18
+ skipIntrospection: false,
19
+ };
20
+ if (opts?.polling) {
21
+ setInterval(() => {
22
+ pluginCtx.schema$ = undefined;
23
+ pluginCtx.schema = undefined;
24
+ }, opts.polling);
25
+ }
26
+ function ensureSchema(ctx) {
27
+ if (pluginCtx.skipIntrospection) {
28
+ return;
29
+ }
30
+ try {
31
+ if (!pluginCtx.schema && !pluginCtx.schemaSetPromise$) {
32
+ pluginCtx.schema$ ||= schemaFromExecutor(executor, ctx, opts);
33
+ if (isPromise(pluginCtx.schema$)) {
34
+ pluginCtx.schemaSetPromise$ = pluginCtx.schema$.then(newSchema => {
35
+ pluginCtx.schema = newSchema;
36
+ }).catch?.(err => {
37
+ console.warn(`Introspection failed, skipping introspection due to the following errors;\n`, err);
38
+ pluginCtx.skipIntrospection = true;
39
+ });
40
+ }
41
+ else {
42
+ pluginCtx.schema = pluginCtx.schema$;
43
+ }
44
+ }
45
+ }
46
+ catch (err) {
47
+ pluginCtx.skipIntrospection = true;
48
+ console.warn(`Introspection failed, skipping introspection due to the following errors;\n`, err);
49
+ }
50
+ }
11
51
  return {
12
- onExecute({ setExecuteFn }) {
52
+ onEnveloped({ context, setSchema }) {
53
+ ensureSchema(context);
54
+ if (pluginCtx.schema) {
55
+ setSchema(pluginCtx.schema);
56
+ }
57
+ },
58
+ onContextBuilding() {
59
+ ensureSchema();
60
+ if (pluginCtx.schemaSetPromise$) {
61
+ return pluginCtx.schemaSetPromise$;
62
+ }
63
+ },
64
+ onExecute({ args, setExecuteFn }) {
65
+ if (args.schema) {
66
+ pluginCtx.schema = args.schema;
67
+ pluginCtx.schema$ = pluginCtx.schema;
68
+ }
69
+ ensureSchema(args.contextValue);
70
+ if (isPromise(pluginCtx.schemaSetPromise$)) {
71
+ return pluginCtx.schemaSetPromise$.then(() => {
72
+ setExecuteFn(executorToExecuteFn);
73
+ });
74
+ }
13
75
  setExecuteFn(executorToExecuteFn);
14
76
  },
15
- onSubscribe({ setSubscribeFn }) {
77
+ onSubscribe({ args, setSubscribeFn }) {
78
+ if (args.schema) {
79
+ pluginCtx.schema = args.schema;
80
+ pluginCtx.schema$ = pluginCtx.schema;
81
+ }
82
+ ensureSchema(args.contextValue);
83
+ if (isPromise(pluginCtx.schemaSetPromise$)) {
84
+ return pluginCtx.schemaSetPromise$.then(() => {
85
+ setSubscribeFn(executorToExecuteFn);
86
+ });
87
+ }
16
88
  setSubscribeFn(executorToExecuteFn);
17
89
  },
90
+ onValidate({ params, context, setResult }) {
91
+ if (params.schema) {
92
+ pluginCtx.schema = params.schema;
93
+ pluginCtx.schema$ = pluginCtx.schema;
94
+ }
95
+ ensureSchema(context);
96
+ if (pluginCtx.schema?.[Symbol.toStringTag] !== 'GraphQLSchema') {
97
+ setResult(EMPTY_ARRAY);
98
+ }
99
+ },
100
+ pluginCtx,
101
+ ensureSchema,
102
+ invalidateSupergraph() {
103
+ pluginCtx.schema$ = undefined;
104
+ pluginCtx.schema = undefined;
105
+ pluginCtx.skipIntrospection = false;
106
+ },
18
107
  };
19
108
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@graphql-tools/executor-envelop",
3
- "version": "2.0.2",
3
+ "version": "2.0.3",
4
4
  "description": "A set of utils for faster development of GraphQL tools",
5
5
  "sideEffects": false,
6
6
  "peerDependencies": {
@@ -8,6 +8,7 @@
8
8
  },
9
9
  "dependencies": {
10
10
  "@graphql-tools/utils": "^10.0.0",
11
+ "@graphql-tools/wrap": "^10.0.0",
11
12
  "tslib": "^2.3.1"
12
13
  },
13
14
  "repository": {
@@ -1,3 +1,19 @@
1
1
  import { Plugin } from '@envelop/core';
2
- import { Executor } from '@graphql-tools/utils';
3
- export declare function useExecutor(executor: Executor): Plugin;
2
+ import { Executor, MaybePromise } from '@graphql-tools/utils';
3
+ import { schemaFromExecutor } from '@graphql-tools/wrap';
4
+ type GraphQLSchema = any;
5
+ export interface ExecutorPluginContext {
6
+ schema$?: MaybePromise<GraphQLSchema>;
7
+ schema?: GraphQLSchema;
8
+ schemaSetPromise$?: PromiseLike<void>;
9
+ skipIntrospection: boolean;
10
+ }
11
+ export type ExecutorPluginOpts = Parameters<typeof schemaFromExecutor>[2] & {
12
+ polling?: number;
13
+ };
14
+ export declare function useExecutor(executor: Executor, opts?: ExecutorPluginOpts): Plugin & {
15
+ invalidateSupergraph: () => void;
16
+ pluginCtx: ExecutorPluginContext;
17
+ ensureSchema(ctx?: any): void;
18
+ };
19
+ export {};
@@ -1,3 +1,19 @@
1
1
  import { Plugin } from '@envelop/core';
2
- import { Executor } from '@graphql-tools/utils';
3
- export declare function useExecutor(executor: Executor): Plugin;
2
+ import { Executor, MaybePromise } from '@graphql-tools/utils';
3
+ import { schemaFromExecutor } from '@graphql-tools/wrap';
4
+ type GraphQLSchema = any;
5
+ export interface ExecutorPluginContext {
6
+ schema$?: MaybePromise<GraphQLSchema>;
7
+ schema?: GraphQLSchema;
8
+ schemaSetPromise$?: PromiseLike<void>;
9
+ skipIntrospection: boolean;
10
+ }
11
+ export type ExecutorPluginOpts = Parameters<typeof schemaFromExecutor>[2] & {
12
+ polling?: number;
13
+ };
14
+ export declare function useExecutor(executor: Executor, opts?: ExecutorPluginOpts): Plugin & {
15
+ invalidateSupergraph: () => void;
16
+ pluginCtx: ExecutorPluginContext;
17
+ ensureSchema(ctx?: any): void;
18
+ };
19
+ export {};