You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+86-3Lines changed: 86 additions & 3 deletions
Original file line number
Diff line number
Diff line change
@@ -7,6 +7,9 @@
7
7
</div>
8
8
9
9
10
+
## Summary
11
+
12
+
Developed under tech-accelerator [OSLabs](https://opensourcelabs.io/), GraphQLGate strives for a principled approach to complexity analysis and rate-limiting for GraphQL queries by accurately estimating an upper-bound of the response size of the query. Within a loosely opinionated framework with lots of configuration options, you can reliably throttle GraphQL queries by complexity and depth to protect your GraphQL API. Our solution is inspired by [this paper](https://github.com/Alan-Cha/fse20/blob/master/submissions/functional/FSE-24/graphql-paper.pdf) from IBM research teams.
10
13
11
14
## Table of Contents
12
15
@@ -16,6 +19,7 @@
16
19
-[How It Works](#how-it-works)
17
20
-[Response](#response)
18
21
-[Error Handling](#error-handling)
22
+
-[Internals](#internals)
19
23
-[Future Development](#future-development)
20
24
-[Contributions](#contributions)
21
25
-[Developers](#developers)
@@ -35,7 +39,7 @@ NOTE: a Redis server instance will need to be started in order for the limiter t
Requests are rate-limited based on the IP address associated with the request.
137
141
138
-
On startup, the GraphQL (GQL) schema is parsed to build an object that maps GQL types/fields to their corresponding weights. Type weights can be provided during <a href="typeWeights">initial configuration</a>. When a request is received, this object is used to cross reference the fields queried by the user and compute the complexity of each field. The total complexity of the request is the sum of these values.
142
+
On startup, the GraphQL (GQL) schema is parsed to build an object that maps GQL types/fields to their corresponding weights. Type weights can be provided during <a href="#typeWeights">initial configuration</a>. When a request is received, this object is used to cross reference the fields queried by the user and compute the complexity of each field. The total complexity of the request is the sum of these values.
139
143
140
144
Complexity is determined, statically (before any resolvers are called) to estimate the upper bound of the response size - a proxy for the work done by the server to build the response. The total complexity is then used to allow/block the request based on popular rate-limiting algorithms.
141
145
@@ -176,7 +180,7 @@ query {
176
180
177
181
```javascript
178
182
{
179
-
graphglGate: {
183
+
graphqlGate: {
180
184
success: boolean, // true when successful
181
185
tokens: number, // tokens available after request
182
186
compexity: number, // complexity of the query
@@ -191,6 +195,85 @@ query {
191
195
- Incoming queries are validated against the GraphQL schema. If the query is invalid, a response with status code `400` is returned along with an array of GraphQL Errors that were found.
192
196
- To avoid disrupting server activity, errors thrown during the analysis and rate-limiting of the query are logged and the request is passed onto the next piece of middleware in the chain.
193
197
198
+
## <a name="internals"></a> Internals
199
+
200
+
This package exposes 3 additional functionalities which comprise the internals of the package. This is a breif documentaion on them.
201
+
202
+
### Complexity Analysis
203
+
204
+
1. #### `typeWeightsFromSchema` | function to create the type weight object from the schema for complexity analysis
205
+
206
+
- `schema: GraphQLSchema` | GraphQL schema object
207
+
- `typeWeightsConfig: TypeWeightConfig = defaultTypeWeightsConfig` | type weight configuration
208
+
- `enforceBoundedLists = false`
209
+
- returns: `TypeWeightObject`
210
+
- usage:
211
+
212
+
```ts
213
+
import { typeWeightsFromSchema } from 'graphql-limiter';
214
+
import { GraphQLSchema } from 'graphql/type/schema';
- returns:`{ success: boolean, tokens: number, retryAfter?: number }`| where `tokens` is tokens available, `retryAfter` is time to wait in seconds before the request would be successful and `success` is falseif the request is blocked
0 commit comments