@graphql-yoga/plugin-persisted-operations 3.13.0 → 3.13.1

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 (3) hide show
  1. package/cjs/index.js +33 -26
  2. package/esm/index.js +33 -26
  3. package/package.json +4 -1
package/cjs/index.js CHANGED
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.defaultExtractPersistedOperationId = void 0;
4
4
  exports.usePersistedOperations = usePersistedOperations;
5
5
  const graphql_yoga_1 = require("graphql-yoga");
6
+ const promise_helpers_1 = require("@whatwg-node/promise-helpers");
6
7
  const defaultExtractPersistedOperationId = (params) => {
7
8
  if (params.extensions != null &&
8
9
  typeof params.extensions === 'object' &&
@@ -22,42 +23,48 @@ function usePersistedOperations({ allowArbitraryOperations = false, extractPersi
22
23
  const keyNotFoundErrorFactory = createErrorFactory('PersistedQueryKeyNotFound', customErrors?.keyNotFound);
23
24
  const persistentQueryOnlyErrorFactory = createErrorFactory('PersistedQueryOnly', customErrors?.persistedQueryOnly);
24
25
  return {
25
- async onParams(payload) {
26
+ onParams(payload) {
26
27
  const { request, params, setParams } = payload;
27
28
  if (params.query) {
28
- if ((typeof allowArbitraryOperations === 'boolean'
29
- ? allowArbitraryOperations
30
- : await allowArbitraryOperations(request)) === false) {
29
+ if (allowArbitraryOperations === false) {
31
30
  throw persistentQueryOnlyErrorFactory(payload);
32
31
  }
32
+ if (typeof allowArbitraryOperations === 'function') {
33
+ return (0, promise_helpers_1.handleMaybePromise)(() => allowArbitraryOperations(request), result => {
34
+ if (!result) {
35
+ throw persistentQueryOnlyErrorFactory(payload);
36
+ }
37
+ });
38
+ }
33
39
  return;
34
40
  }
35
41
  const persistedOperationKey = extractPersistedOperationId(params, request, payload.context);
36
42
  if (persistedOperationKey == null) {
37
43
  throw keyNotFoundErrorFactory(payload);
38
44
  }
39
- const persistedQuery = await getPersistedOperation(persistedOperationKey, request, payload.context);
40
- if (persistedQuery == null) {
41
- throw notFoundErrorFactory(payload);
42
- }
43
- if (typeof persistedQuery === 'object') {
44
- setParams({
45
- query: `__PERSISTED_OPERATION_${persistedOperationKey}__`,
46
- operationName: params.operationName,
47
- variables: params.variables,
48
- extensions: params.extensions,
49
- });
50
- operationASTByRequest.set(request, persistedQuery);
51
- }
52
- else {
53
- setParams({
54
- query: persistedQuery,
55
- operationName: params.operationName,
56
- variables: params.variables,
57
- extensions: params.extensions,
58
- });
59
- }
60
- persistedOperationRequest.add(request);
45
+ return (0, promise_helpers_1.handleMaybePromise)(() => getPersistedOperation(persistedOperationKey, request, payload.context), persistedQuery => {
46
+ if (persistedQuery == null) {
47
+ throw notFoundErrorFactory(payload);
48
+ }
49
+ if (typeof persistedQuery === 'object') {
50
+ setParams({
51
+ query: `__PERSISTED_OPERATION_${persistedOperationKey}__`,
52
+ operationName: params.operationName,
53
+ variables: params.variables,
54
+ extensions: params.extensions,
55
+ });
56
+ operationASTByRequest.set(request, persistedQuery);
57
+ }
58
+ else {
59
+ setParams({
60
+ query: persistedQuery,
61
+ operationName: params.operationName,
62
+ variables: params.variables,
63
+ extensions: params.extensions,
64
+ });
65
+ }
66
+ persistedOperationRequest.add(request);
67
+ });
61
68
  },
62
69
  onValidate({ setResult, context: { request } }) {
63
70
  if (skipDocumentValidation && persistedOperationRequest.has(request)) {
package/esm/index.js CHANGED
@@ -1,4 +1,5 @@
1
1
  import { createGraphQLError, } from 'graphql-yoga';
2
+ import { handleMaybePromise } from '@whatwg-node/promise-helpers';
2
3
  export const defaultExtractPersistedOperationId = (params) => {
3
4
  if (params.extensions != null &&
4
5
  typeof params.extensions === 'object' &&
@@ -17,42 +18,48 @@ export function usePersistedOperations({ allowArbitraryOperations = false, extra
17
18
  const keyNotFoundErrorFactory = createErrorFactory('PersistedQueryKeyNotFound', customErrors?.keyNotFound);
18
19
  const persistentQueryOnlyErrorFactory = createErrorFactory('PersistedQueryOnly', customErrors?.persistedQueryOnly);
19
20
  return {
20
- async onParams(payload) {
21
+ onParams(payload) {
21
22
  const { request, params, setParams } = payload;
22
23
  if (params.query) {
23
- if ((typeof allowArbitraryOperations === 'boolean'
24
- ? allowArbitraryOperations
25
- : await allowArbitraryOperations(request)) === false) {
24
+ if (allowArbitraryOperations === false) {
26
25
  throw persistentQueryOnlyErrorFactory(payload);
27
26
  }
27
+ if (typeof allowArbitraryOperations === 'function') {
28
+ return handleMaybePromise(() => allowArbitraryOperations(request), result => {
29
+ if (!result) {
30
+ throw persistentQueryOnlyErrorFactory(payload);
31
+ }
32
+ });
33
+ }
28
34
  return;
29
35
  }
30
36
  const persistedOperationKey = extractPersistedOperationId(params, request, payload.context);
31
37
  if (persistedOperationKey == null) {
32
38
  throw keyNotFoundErrorFactory(payload);
33
39
  }
34
- const persistedQuery = await getPersistedOperation(persistedOperationKey, request, payload.context);
35
- if (persistedQuery == null) {
36
- throw notFoundErrorFactory(payload);
37
- }
38
- if (typeof persistedQuery === 'object') {
39
- setParams({
40
- query: `__PERSISTED_OPERATION_${persistedOperationKey}__`,
41
- operationName: params.operationName,
42
- variables: params.variables,
43
- extensions: params.extensions,
44
- });
45
- operationASTByRequest.set(request, persistedQuery);
46
- }
47
- else {
48
- setParams({
49
- query: persistedQuery,
50
- operationName: params.operationName,
51
- variables: params.variables,
52
- extensions: params.extensions,
53
- });
54
- }
55
- persistedOperationRequest.add(request);
40
+ return handleMaybePromise(() => getPersistedOperation(persistedOperationKey, request, payload.context), persistedQuery => {
41
+ if (persistedQuery == null) {
42
+ throw notFoundErrorFactory(payload);
43
+ }
44
+ if (typeof persistedQuery === 'object') {
45
+ setParams({
46
+ query: `__PERSISTED_OPERATION_${persistedOperationKey}__`,
47
+ operationName: params.operationName,
48
+ variables: params.variables,
49
+ extensions: params.extensions,
50
+ });
51
+ operationASTByRequest.set(request, persistedQuery);
52
+ }
53
+ else {
54
+ setParams({
55
+ query: persistedQuery,
56
+ operationName: params.operationName,
57
+ variables: params.variables,
58
+ extensions: params.extensions,
59
+ });
60
+ }
61
+ persistedOperationRequest.add(request);
62
+ });
56
63
  },
57
64
  onValidate({ setResult, context: { request } }) {
58
65
  if (skipDocumentValidation && persistedOperationRequest.has(request)) {
package/package.json CHANGED
@@ -1,11 +1,14 @@
1
1
  {
2
2
  "name": "@graphql-yoga/plugin-persisted-operations",
3
- "version": "3.13.0",
3
+ "version": "3.13.1",
4
4
  "description": "Persisted Operations plugin for GraphQL Yoga.",
5
5
  "peerDependencies": {
6
6
  "graphql": "^15.2.0 || ^16.0.0",
7
7
  "graphql-yoga": "^5.13.0"
8
8
  },
9
+ "dependencies": {
10
+ "@whatwg-node/promise-helpers": "^1.2.4"
11
+ },
9
12
  "repository": {
10
13
  "type": "git",
11
14
  "url": "https://github.com/dotansimha/graphql-yoga.git",