@envelop/resource-limitations 0.3.0 → 0.4.2-alpha-33ac2d1.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,4 +1,4 @@
1
- import { Plugin } from '@envelop/types';
1
+ import { Plugin } from '@envelop/core';
2
2
  import { ExtendedValidationRule } from '@envelop/extended-validation';
3
3
  import { ExecutionArgs } from 'graphql';
4
4
  export declare const defaultNodeCostLimit = 500000;
package/index.js CHANGED
@@ -2,79 +2,11 @@
2
2
 
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
+ const core = require('@envelop/core');
6
+ const extendedValidation = require('@envelop/extended-validation');
5
7
  const graphql = require('graphql');
6
8
  const utils = require('@graphql-tools/utils');
7
9
 
8
- /**
9
- * Returns true if the provided object implements the AsyncIterator protocol via
10
- * implementing a `Symbol.asyncIterator` method.
11
- *
12
- * Source: https://github.com/graphql/graphql-js/blob/main/src/jsutils/isAsyncIterable.ts
13
- */
14
- function isAsyncIterable(maybeAsyncIterable) {
15
- return (maybeAsyncIterable != null &&
16
- typeof maybeAsyncIterable === 'object' &&
17
- typeof maybeAsyncIterable[Symbol.asyncIterator] === 'function');
18
- }
19
- /**
20
- * A utility function for hanlding `onExecuteDone` hook result, for simplifying the hanlding of AsyncIterable returned from `execute`.
21
- *
22
- * @param payload The payload send to `onExecuteDone` hook function
23
- * @param fn The handler to be executed on each result
24
- * @returns a subscription for streamed results, or undefined in case of an non-async
25
- */
26
- function handleStreamOrSingleExecutionResult(payload, fn) {
27
- if (isAsyncIterable(payload.result)) {
28
- return { onNext: fn };
29
- }
30
- else {
31
- fn({
32
- args: payload.args,
33
- result: payload.result,
34
- setResult: payload.setResult,
35
- });
36
- return undefined;
37
- }
38
- }
39
-
40
- const SYMBOL_EXTENDED_VALIDATION_RULES = Symbol('SYMBOL_EXTENDED_VALIDATION_RULES');
41
- const useExtendedValidation = (options) => {
42
- let schemaTypeInfo;
43
- return {
44
- onSchemaChange({ schema }) {
45
- schemaTypeInfo = new graphql.TypeInfo(schema);
46
- },
47
- onParse({ context, extendContext }) {
48
- var _a;
49
- const rules = (_a = context === null || context === void 0 ? void 0 : context[SYMBOL_EXTENDED_VALIDATION_RULES]) !== null && _a !== void 0 ? _a : [];
50
- rules.push(...options.rules);
51
- extendContext({
52
- [SYMBOL_EXTENDED_VALIDATION_RULES]: rules,
53
- });
54
- },
55
- onExecute({ args, setResultAndStopExecution }) {
56
- const rules = args.contextValue[SYMBOL_EXTENDED_VALIDATION_RULES];
57
- const errors = [];
58
- const typeInfo = schemaTypeInfo || new graphql.TypeInfo(args.schema);
59
- const validationContext = new graphql.ValidationContext(args.schema, args.document, typeInfo, e => {
60
- errors.push(e);
61
- });
62
- const visitor = graphql.visitInParallel(rules.map(rule => rule(validationContext, args)));
63
- graphql.visit(args.document, graphql.visitWithTypeInfo(typeInfo, visitor));
64
- if (errors.length > 0) {
65
- let result = {
66
- data: null,
67
- errors,
68
- };
69
- if (options.onValidationFailed) {
70
- options.onValidationFailed({ args, result, setResult: newResult => (result = newResult) });
71
- }
72
- return setResultAndStopExecution(result);
73
- }
74
- },
75
- };
76
- };
77
-
78
10
  const getWrappedType = (graphqlType) => {
79
11
  if (graphqlType instanceof graphql.GraphQLList || graphqlType instanceof graphql.GraphQLNonNull) {
80
12
  return getWrappedType(graphqlType.ofType);
@@ -220,7 +152,7 @@ const useResourceLimitations = (params) => {
220
152
  };
221
153
  return {
222
154
  onPluginInit({ addPlugin }) {
223
- addPlugin(useExtendedValidation({
155
+ addPlugin(extendedValidation.useExtendedValidation({
224
156
  rules: [
225
157
  ResourceLimitationValidationRule({
226
158
  nodeCostLimit,
@@ -240,7 +172,7 @@ const useResourceLimitations = (params) => {
240
172
  onExecute({ args }) {
241
173
  return {
242
174
  onExecuteDone(payload) {
243
- return handleStreamOrSingleExecutionResult(payload, ({ result }) => handleResult({ result, args }));
175
+ return core.handleStreamOrSingleExecutionResult(payload, ({ result }) => handleResult({ result, args }));
244
176
  },
245
177
  };
246
178
  },
package/index.mjs CHANGED
@@ -1,76 +1,8 @@
1
- import { TypeInfo, ValidationContext, visitInParallel, visit, visitWithTypeInfo, GraphQLObjectType, GraphQLError, GraphQLList, GraphQLNonNull, GraphQLInt, isScalarType } from 'graphql';
1
+ import { handleStreamOrSingleExecutionResult } from '@envelop/core';
2
+ import { useExtendedValidation } from '@envelop/extended-validation';
3
+ import { GraphQLObjectType, GraphQLError, GraphQLList, GraphQLNonNull, GraphQLInt, isScalarType } from 'graphql';
2
4
  import { getArgumentValues } from '@graphql-tools/utils';
3
5
 
4
- /**
5
- * Returns true if the provided object implements the AsyncIterator protocol via
6
- * implementing a `Symbol.asyncIterator` method.
7
- *
8
- * Source: https://github.com/graphql/graphql-js/blob/main/src/jsutils/isAsyncIterable.ts
9
- */
10
- function isAsyncIterable(maybeAsyncIterable) {
11
- return (maybeAsyncIterable != null &&
12
- typeof maybeAsyncIterable === 'object' &&
13
- typeof maybeAsyncIterable[Symbol.asyncIterator] === 'function');
14
- }
15
- /**
16
- * A utility function for hanlding `onExecuteDone` hook result, for simplifying the hanlding of AsyncIterable returned from `execute`.
17
- *
18
- * @param payload The payload send to `onExecuteDone` hook function
19
- * @param fn The handler to be executed on each result
20
- * @returns a subscription for streamed results, or undefined in case of an non-async
21
- */
22
- function handleStreamOrSingleExecutionResult(payload, fn) {
23
- if (isAsyncIterable(payload.result)) {
24
- return { onNext: fn };
25
- }
26
- else {
27
- fn({
28
- args: payload.args,
29
- result: payload.result,
30
- setResult: payload.setResult,
31
- });
32
- return undefined;
33
- }
34
- }
35
-
36
- const SYMBOL_EXTENDED_VALIDATION_RULES = Symbol('SYMBOL_EXTENDED_VALIDATION_RULES');
37
- const useExtendedValidation = (options) => {
38
- let schemaTypeInfo;
39
- return {
40
- onSchemaChange({ schema }) {
41
- schemaTypeInfo = new TypeInfo(schema);
42
- },
43
- onParse({ context, extendContext }) {
44
- var _a;
45
- const rules = (_a = context === null || context === void 0 ? void 0 : context[SYMBOL_EXTENDED_VALIDATION_RULES]) !== null && _a !== void 0 ? _a : [];
46
- rules.push(...options.rules);
47
- extendContext({
48
- [SYMBOL_EXTENDED_VALIDATION_RULES]: rules,
49
- });
50
- },
51
- onExecute({ args, setResultAndStopExecution }) {
52
- const rules = args.contextValue[SYMBOL_EXTENDED_VALIDATION_RULES];
53
- const errors = [];
54
- const typeInfo = schemaTypeInfo || new TypeInfo(args.schema);
55
- const validationContext = new ValidationContext(args.schema, args.document, typeInfo, e => {
56
- errors.push(e);
57
- });
58
- const visitor = visitInParallel(rules.map(rule => rule(validationContext, args)));
59
- visit(args.document, visitWithTypeInfo(typeInfo, visitor));
60
- if (errors.length > 0) {
61
- let result = {
62
- data: null,
63
- errors,
64
- };
65
- if (options.onValidationFailed) {
66
- options.onValidationFailed({ args, result, setResult: newResult => (result = newResult) });
67
- }
68
- return setResultAndStopExecution(result);
69
- }
70
- },
71
- };
72
- };
73
-
74
6
  const getWrappedType = (graphqlType) => {
75
7
  if (graphqlType instanceof GraphQLList || graphqlType instanceof GraphQLNonNull) {
76
8
  return getWrappedType(graphqlType.ofType);
package/package.json CHANGED
@@ -1,13 +1,15 @@
1
1
  {
2
2
  "name": "@envelop/resource-limitations",
3
- "version": "0.3.0",
3
+ "version": "0.4.2-alpha-33ac2d1.0",
4
4
  "description": "A rate-limit implementation based on resource limitations and static calculation of the score (similar to GitHub GraphQL API)",
5
5
  "sideEffects": false,
6
6
  "peerDependencies": {
7
+ "@envelop/core": "1.6.6-alpha-33ac2d1.0",
7
8
  "graphql": "^14.0.0 || ^15.0.0 || ^16.0.0"
8
9
  },
9
10
  "dependencies": {
10
- "@graphql-tools/utils": "8.5.1"
11
+ "@envelop/extended-validation": "1.3.2-alpha-33ac2d1.0",
12
+ "@graphql-tools/utils": "^8.5.3"
11
13
  },
12
14
  "repository": {
13
15
  "type": "git",
@@ -30,6 +32,7 @@
30
32
  "./*": {
31
33
  "require": "./*.js",
32
34
  "import": "./*.mjs"
33
- }
35
+ },
36
+ "./package.json": "./package.json"
34
37
  }
35
38
  }