|
1 | | -# GraphQL-Gate |
| 1 | +<div align="center"> |
| 2 | + <img width="50px" src="https://user-images.githubusercontent.com/89324687/182067950-54c00964-2be4-481a-976b-773d9112a4c0.png"/> |
| 3 | + <h1>GraphQLGate</h1> |
| 4 | + <a href="https://github.com/oslabs-beta/GraphQL-Gate"><img src="https://img.shields.io/badge/license-MIT-blue"/></a> <a href="https://github.com/oslabs- beta/GraphQL-Gate/stargazers"><img alt="GitHub stars" src="https://img.shields.io/github/stars/oslabs-beta/GraphQL-Gate"></a> <a href="https://github.com/oslabs-beta/GraphQL-Gate/issues"><img alt="GitHub issues" src="https://img.shields.io/github/issues/oslabs-beta/GraphQL-Gate"></a> <img alt="GitHub last commit" src="https://img.shields.io/github/last-commit/oslabs-beta/GraphQL-Gate"> |
2 | 5 |
|
3 | | -A GraphQL rate limiting library using query complexity analysis. |
| 6 | + <h3 align="center"> <strong>A GraphQL rate-limiting library with query complextiy analysis for Node.js and Express</strong></h3> |
| 7 | + </div> |
| 8 | + |
| 9 | + |
| 10 | + |
| 11 | +## Table of Contents |
| 12 | + |
| 13 | +- [Getting Started](#getting-started) |
| 14 | +- [Configuration](#configuration) |
| 15 | +- [How It Works](#how-it-works) |
| 16 | +- [Future Development](#future-development) |
| 17 | +- [Contributions](#contributions) |
| 18 | +- [Developers](#developers) |
| 19 | +- [License](#license) |
| 20 | + |
| 21 | +## <a name="getting-started"></a> Getting Started |
| 22 | + |
| 23 | +Install the package |
| 24 | + |
| 25 | +``` |
| 26 | +npm i graphqlgate |
| 27 | +``` |
| 28 | + |
| 29 | +Import the package and add the rate-limiting middlleware to the Express middleware chain before the GraphQL server. |
| 30 | + |
| 31 | +NOTE: a Redis server instance will need to be started in order for the limiter to cache data. |
| 32 | + |
| 33 | +```javascript |
| 34 | +// import package |
| 35 | +import expressGraphQLRateLimiter from 'graphqlgate'; |
| 36 | + |
| 37 | +/** |
| 38 | + * Import other dependencies |
| 39 | + * */ |
| 40 | + |
| 41 | +//Add the middleware into your GraphQL middleware chain |
| 42 | +app.use( |
| 43 | + 'gql', |
| 44 | + expressGraphQLRateLimiter(schemaObject, { |
| 45 | + rateLimiter: { |
| 46 | + type: 'TOKEN_BUCKET', |
| 47 | + refillRate: 10, |
| 48 | + capacity: 100, |
| 49 | + }, |
| 50 | + }) /** add GraphQL server here */ |
| 51 | +); |
| 52 | +``` |
| 53 | + |
| 54 | +## <a name="configuration"></a> Configuration |
| 55 | + |
| 56 | +1. #### `schema: GraphQLSchema` | required |
| 57 | + |
| 58 | +2. #### `config: ExpressMiddlewareConfig` | required |
| 59 | + |
| 60 | + - `rateLimiter: RateLimiterOptions` | required |
| 61 | + |
| 62 | + - `type: 'TOKEN_BUCKET' | 'FIXED_WINDOW' | 'SLIDING_WINDOW_LOG' | 'SLIDING_WINDOW_COUTER'` |
| 63 | + - `capacity: number` |
| 64 | + - `refillRate: number` | bucket algorithms only |
| 65 | + - `windowSize: number` | (in ms) window algorithms only |
| 66 | + |
| 67 | + - `redis: RedisConfig` |
| 68 | + |
| 69 | + - `options: RedisOptions` | [ioredis configuration options](https://github.com/luin/ioredis) | defaults to standard ioredis connection options (`localhost:6379`) |
| 70 | + - `keyExpiry: number` (ms) | custom expiry of keys in redis cache | defaults to 24 hours |
| 71 | + |
| 72 | + - `typeWeights: TypeWeightObject` |
| 73 | + |
| 74 | + - `mutation: number` | assigned weight to mutations | defaults to 10 |
| 75 | + - `query: number` | assigned weight of a query | defaults to 1 |
| 76 | + - `object: number` | assigned weight of GraphQL object, interface and union types | defaults to `1` |
| 77 | + - `scalar: number` | assigned weight of GraphQL scalar and enum types | defaults to `0` |
| 78 | + |
| 79 | + - `depthLimit: number` | throttle queies by the depth of the nested stucture | defaults to `Infinity` (ie. no limit) |
| 80 | + - `enforceBoundedLists: boolean` | if true, an error will be thrown if any lists types are not bound by slicing arguments [`first`, `last`, `limit`] or directives | defaults to `false` |
| 81 | + - `dark: boolean` | if true, the package will calculate complexity, depth and tokens but not throttle any queries. Use this to dark launch the package and monitor what would happen if rate limiting was added to yaur application |
| 82 | + |
| 83 | + All configuration options |
| 84 | + |
| 85 | + ```javascript |
| 86 | + expressGraphQLRateLimiter(schemaObject, { |
| 87 | + rateLimiter: { |
| 88 | + type: 'TOKEN_BUCKET', // rate-limiter selection |
| 89 | + refillRate: 10, |
| 90 | + capacity: 100, |
| 91 | + }, |
| 92 | + redis: { |
| 93 | + keyExpiry: 14400000 // 4 hours, defaults to 86400000 (24 hours) |
| 94 | + options: { |
| 95 | + host: 'localhost' // ioredis connection options |
| 96 | + port: 6379, |
| 97 | + } |
| 98 | + }, |
| 99 | + typeWeights: { // weights of GraphQL types |
| 100 | + mutation: 10, |
| 101 | + query: 1, |
| 102 | + object: 1, |
| 103 | + scalar: 0, |
| 104 | + }, |
| 105 | + enforceBoundedLists: false, // defaults to false |
| 106 | + dark: false, // defaults to false |
| 107 | + depthLimit: 7 // defaults to Infinity (ie. no depth limiting) |
| 108 | + }); |
| 109 | + ``` |
| 110 | + |
| 111 | +## <a name="how-it-works"></a> How It Works |
| 112 | + |
| 113 | +how are things weighted examples |
| 114 | + |
| 115 | +## <a name="future-development"></a> Future Development |
| 116 | + |
| 117 | +- configure rate-limiting cache with other caching libraries |
| 118 | +- resolve complexity analysis for queries |
| 119 | +- leaky bucket rate-limiting algorithm |
| 120 | +- experimint with performance improvments |
| 121 | + - caching optimizations |
| 122 | + |
| 123 | +## <a name="contributions"></a> Contributions |
| 124 | + |
| 125 | +Contributions to the code, examples, documentation, etc. are very much appreciated. |
| 126 | + |
| 127 | +- Please report issues and bugs directly in this [GitHub project](https://github.com/oslabs-beta/GraphQL-Gate/issues). |
| 128 | + |
| 129 | +## <a name="developers"></a> Developers |
| 130 | + |
| 131 | +- [Evan McNeely](https://github.com/evanmcneely) |
| 132 | +- [Stephan Halarewicz](https://github.com/shalarewicz) |
| 133 | +- [Flora Yufei Wu](https://github.com/feiw101) |
| 134 | +- [Jon Dewey](https://github.com/donjewey) |
| 135 | +- [Milos Popovic](https://github.com/milos381) |
| 136 | + |
| 137 | +## <a name="license"></a> License |
| 138 | + |
| 139 | +This product is licensed under the MIT License - see the LICENSE.md file for details. |
| 140 | + |
| 141 | +This is an open source product. |
| 142 | + |
| 143 | +This product is accelerated by OS Labs. |
0 commit comments