@graphql-mesh/plugin-rate-limit 0.0.1-alpha-d8211f7b2.0 → 0.0.1-alpha-ce3779bf5.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.
Files changed (3) hide show
  1. package/index.js +31 -24
  2. package/index.mjs +29 -24
  3. package/package.json +4 -2
package/index.js CHANGED
@@ -1,49 +1,48 @@
1
1
  'use strict';
2
2
 
3
+ function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
4
+
3
5
  const stringInterpolation = require('@graphql-mesh/string-interpolation');
4
- const graphql = require('graphql');
5
6
  const crossHelpers = require('@graphql-mesh/cross-helpers');
7
+ const utils = require('@graphql-tools/utils');
8
+ const minimatch = _interopDefault(require('minimatch'));
6
9
 
7
10
  function useMeshRateLimit(options) {
8
- const pathRateLimitDef = new Map();
9
11
  const tokenMap = new Map();
10
12
  const timeouts = new Set();
11
- if (options.config) {
12
- options.config.forEach(config => {
13
- pathRateLimitDef.set(`${config.type}.${config.field}`, config);
14
- });
15
- }
16
13
  if (options.pubsub) {
17
14
  const id = options.pubsub.subscribe('destroy', () => {
18
15
  options.pubsub.unsubscribe(id);
19
16
  timeouts.forEach(timeout => clearTimeout(timeout));
20
17
  });
21
18
  }
22
- return {
23
- onValidate(onValidateParams) {
24
- onValidateParams.addValidationRule(validationContext => ({
25
- Field: () => {
26
- const parentType = validationContext.getParentType();
19
+ const validationRuleFactories = options.config.map(config => {
20
+ const typeMatcher = new minimatch.Minimatch(config.type);
21
+ const fieldMatcher = new minimatch.Minimatch(config.field);
22
+ return (context) => (validationContext) => ({
23
+ Field: () => {
24
+ const parentType = validationContext.getParentType();
25
+ if (typeMatcher.match(parentType.name)) {
27
26
  const fieldDef = validationContext.getFieldDef();
28
- const path = `${parentType.name}.${fieldDef.name}`;
29
- const rateLimitConfig = pathRateLimitDef.get(path);
30
- if (rateLimitConfig) {
31
- const identifier = stringInterpolation.stringInterpolator.parse(rateLimitConfig.identifier, {
27
+ if (fieldMatcher.match(fieldDef.name)) {
28
+ const identifier = stringInterpolation.stringInterpolator.parse(config.identifier, {
32
29
  env: crossHelpers.process.env,
33
- context: onValidateParams.context,
30
+ context,
34
31
  });
35
- const mapKey = `${identifier}-${path}`;
32
+ const mapKey = `${identifier}-${parentType.name}.${fieldDef.name}`;
36
33
  let remainingTokens = tokenMap.get(mapKey);
37
34
  if (remainingTokens == null) {
38
- remainingTokens = rateLimitConfig.max;
35
+ remainingTokens = config.max;
39
36
  const timeout = setTimeout(() => {
40
37
  tokenMap.delete(mapKey);
41
38
  timeouts.delete(timeout);
42
- }, rateLimitConfig.ttl);
39
+ }, config.ttl);
43
40
  timeouts.add(timeout);
44
41
  }
45
42
  if (remainingTokens === 0) {
46
- validationContext.reportError(new graphql.GraphQLError(`Rate limit of "${path}" exceeded for "${identifier}"`));
43
+ validationContext.reportError(utils.createGraphQLError(`Rate limit of "${parentType.name}.${fieldDef.name}" exceeded for "${identifier}"`, {
44
+ path: [fieldDef.name],
45
+ }));
47
46
  // Remove this field from the selection set
48
47
  return null;
49
48
  }
@@ -51,9 +50,17 @@ function useMeshRateLimit(options) {
51
50
  tokenMap.set(mapKey, remainingTokens - 1);
52
51
  }
53
52
  }
54
- return false;
55
- },
56
- }));
53
+ }
54
+ return false;
55
+ },
56
+ });
57
+ });
58
+ return {
59
+ onValidate(onValidateParams) {
60
+ validationRuleFactories.forEach(validationRuleFactory => {
61
+ const validationRule = validationRuleFactory(onValidateParams.context);
62
+ onValidateParams.addValidationRule(validationRule);
63
+ });
57
64
  },
58
65
  };
59
66
  }
package/index.mjs CHANGED
@@ -1,47 +1,44 @@
1
1
  import { stringInterpolator } from '@graphql-mesh/string-interpolation';
2
- import { GraphQLError } from 'graphql';
3
2
  import { process } from '@graphql-mesh/cross-helpers';
3
+ import { createGraphQLError } from '@graphql-tools/utils';
4
+ import minimatch from 'minimatch';
4
5
 
5
6
  function useMeshRateLimit(options) {
6
- const pathRateLimitDef = new Map();
7
7
  const tokenMap = new Map();
8
8
  const timeouts = new Set();
9
- if (options.config) {
10
- options.config.forEach(config => {
11
- pathRateLimitDef.set(`${config.type}.${config.field}`, config);
12
- });
13
- }
14
9
  if (options.pubsub) {
15
10
  const id = options.pubsub.subscribe('destroy', () => {
16
11
  options.pubsub.unsubscribe(id);
17
12
  timeouts.forEach(timeout => clearTimeout(timeout));
18
13
  });
19
14
  }
20
- return {
21
- onValidate(onValidateParams) {
22
- onValidateParams.addValidationRule(validationContext => ({
23
- Field: () => {
24
- const parentType = validationContext.getParentType();
15
+ const validationRuleFactories = options.config.map(config => {
16
+ const typeMatcher = new minimatch.Minimatch(config.type);
17
+ const fieldMatcher = new minimatch.Minimatch(config.field);
18
+ return (context) => (validationContext) => ({
19
+ Field: () => {
20
+ const parentType = validationContext.getParentType();
21
+ if (typeMatcher.match(parentType.name)) {
25
22
  const fieldDef = validationContext.getFieldDef();
26
- const path = `${parentType.name}.${fieldDef.name}`;
27
- const rateLimitConfig = pathRateLimitDef.get(path);
28
- if (rateLimitConfig) {
29
- const identifier = stringInterpolator.parse(rateLimitConfig.identifier, {
23
+ if (fieldMatcher.match(fieldDef.name)) {
24
+ const identifier = stringInterpolator.parse(config.identifier, {
30
25
  env: process.env,
31
- context: onValidateParams.context,
26
+ context,
32
27
  });
33
- const mapKey = `${identifier}-${path}`;
28
+ const mapKey = `${identifier}-${parentType.name}.${fieldDef.name}`;
34
29
  let remainingTokens = tokenMap.get(mapKey);
35
30
  if (remainingTokens == null) {
36
- remainingTokens = rateLimitConfig.max;
31
+ remainingTokens = config.max;
37
32
  const timeout = setTimeout(() => {
38
33
  tokenMap.delete(mapKey);
39
34
  timeouts.delete(timeout);
40
- }, rateLimitConfig.ttl);
35
+ }, config.ttl);
41
36
  timeouts.add(timeout);
42
37
  }
43
38
  if (remainingTokens === 0) {
44
- validationContext.reportError(new GraphQLError(`Rate limit of "${path}" exceeded for "${identifier}"`));
39
+ validationContext.reportError(createGraphQLError(`Rate limit of "${parentType.name}.${fieldDef.name}" exceeded for "${identifier}"`, {
40
+ path: [fieldDef.name],
41
+ }));
45
42
  // Remove this field from the selection set
46
43
  return null;
47
44
  }
@@ -49,9 +46,17 @@ function useMeshRateLimit(options) {
49
46
  tokenMap.set(mapKey, remainingTokens - 1);
50
47
  }
51
48
  }
52
- return false;
53
- },
54
- }));
49
+ }
50
+ return false;
51
+ },
52
+ });
53
+ });
54
+ return {
55
+ onValidate(onValidateParams) {
56
+ validationRuleFactories.forEach(validationRuleFactory => {
57
+ const validationRule = validationRuleFactory(onValidateParams.context);
58
+ onValidateParams.addValidationRule(validationRule);
59
+ });
55
60
  },
56
61
  };
57
62
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@graphql-mesh/plugin-rate-limit",
3
- "version": "0.0.1-alpha-d8211f7b2.0",
3
+ "version": "0.0.1-alpha-ce3779bf5.0",
4
4
  "sideEffects": false,
5
5
  "peerDependencies": {
6
6
  "graphql": "*"
@@ -9,7 +9,9 @@
9
9
  "@envelop/core": "^2.3.2",
10
10
  "@graphql-mesh/cross-helpers": "0.1.6",
11
11
  "@graphql-mesh/string-interpolation": "0.3.0",
12
- "@graphql-mesh/types": "0.77.0-alpha-d8211f7b2.0",
12
+ "@graphql-mesh/types": "0.77.0-alpha-ce3779bf5.0",
13
+ "@graphql-tools/utils": "8.8.0",
14
+ "minimatch": "5.1.0",
13
15
  "tslib": "^2.4.0"
14
16
  },
15
17
  "repository": {