@envelop/rate-limiter 3.4.0-alpha-26c4ae2.0 → 3.4.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/README.md +62 -0
- package/package.json +2 -2
package/README.md
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
## `@envelop/rate-limiter`
|
|
2
|
+
|
|
3
|
+
This plugins uses [`graphql-rate-limit`](https://github.com/teamplanes/graphql-rate-limit#readme) in order to limit the rate of calling queries and mutations.
|
|
4
|
+
|
|
5
|
+
## Getting Started
|
|
6
|
+
|
|
7
|
+
```
|
|
8
|
+
yarn add @envelop/rate-limiter
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage Example
|
|
12
|
+
|
|
13
|
+
```ts
|
|
14
|
+
import { envelop } from '@envelop/core';
|
|
15
|
+
import { useRateLimiter, IdentifyFn } from '@envelop/rate-limiter';
|
|
16
|
+
|
|
17
|
+
const identifyFn: IdentifyFn = async context => {
|
|
18
|
+
return context.request.ip;
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
const getEnveloped = envelop({
|
|
22
|
+
plugins: [
|
|
23
|
+
// ... other plugins ...
|
|
24
|
+
useRateLimiter({
|
|
25
|
+
identifyFn,
|
|
26
|
+
}),
|
|
27
|
+
],
|
|
28
|
+
});
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
> By default, we assume that you have the GraphQL directive definition as part of your GraphQL schema (`directive @rateLimit(max: Int, window: String, message: String) on FIELD_DEFINITION`).
|
|
32
|
+
|
|
33
|
+
Then, in your GraphQL schema SDL, you can add `@rateLimit` directive to your fields, and the limiter will get called only while resolving that specific field:
|
|
34
|
+
|
|
35
|
+
```graphql
|
|
36
|
+
type Query {
|
|
37
|
+
posts: [Post]! @rateLimit(
|
|
38
|
+
window: "5s", // time interval window for request limit quota
|
|
39
|
+
max: 10, // maximum requests allowed in time window
|
|
40
|
+
message: "Too many calls!" // quota reached error message
|
|
41
|
+
)
|
|
42
|
+
# unlimitedField: String
|
|
43
|
+
}
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
> You can apply that directive to any GraphQL `field` definition, not only to root fields.
|
|
47
|
+
|
|
48
|
+
### Error message interpolation
|
|
49
|
+
|
|
50
|
+
The `message` argument of the `@rateLimit` directive can be dynamic. You `{{var}}` or `{{ var }}` syntax to interpolate variables.
|
|
51
|
+
|
|
52
|
+
```graphql
|
|
53
|
+
type Query {
|
|
54
|
+
posts: [Post]! @rateLimit(window: "5s", max: 10, message: "Too many calls made by {{ id }}")
|
|
55
|
+
}
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
> The only available variable so far is `id`.
|
|
59
|
+
|
|
60
|
+
## Notes
|
|
61
|
+
|
|
62
|
+
You can find more details here: hhttps://github.com/teamplanes/graphql-rate-limit#readme
|
package/package.json
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@envelop/rate-limiter",
|
|
3
|
-
"version": "3.4.0
|
|
3
|
+
"version": "3.4.0",
|
|
4
4
|
"sideEffects": false,
|
|
5
5
|
"peerDependencies": {
|
|
6
|
-
"@envelop/core": "2.4.0
|
|
6
|
+
"@envelop/core": "^2.4.0",
|
|
7
7
|
"graphql": "^14.0.0 || ^15.0.0 || ^16.0.0"
|
|
8
8
|
},
|
|
9
9
|
"dependencies": {
|