Skip to content

Commit d226f13

Browse files
Update README.md
1 parent 38f0f4d commit d226f13

File tree

1 file changed

+95
-10
lines changed

1 file changed

+95
-10
lines changed

README.md

Lines changed: 95 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,11 @@ __Table of Contents__
4141
GraphQL has gained addoption as a replacement to the conventional REST API and for good reason. Development Time/Time to Market are signficantly shortened when no longer requiring every application to build and maintain its own API.
4242

4343
## Internal GraphQL API
44-
While GraphQL has gained traction recently, the majority of GraphQL endpoints are used internally and not distributed or endorsed for public use. Because of this, exposing your GraphQL server to the public internet exposes you to risk (e.g. DDOS by allowing unknown users to create their own non-performant queries, accidental exposure of sensitive data, etc).
44+
While GraphQL has gained traction recently, the majority of GraphQL endpoints are used internally and not distributed or endorsed for public use. Items such as authentication, permissions, priveleges and sometimes even performance on certain keys are not seen as primary concerns as the API is deemed "internal". Because of this, any exposure of your GraphQL server to the public internet exposes you to risk (e.g. DDOS by allowing unknown users to create their own non-performant queries, accidental exposure of sensitive data, etc).
4545

46-
GraphQL Rest Router attempts to solve these problems by allowing you to leverage GraphQL upstream for all of your data resolution, but exposing predefined queries to the downstream to the public in the form of a cacheable REST url.
46+
GraphQL Rest Router attempts to solve these problems by allowing you to leverage GraphQL upstream for all of your data resolution, but exposes predefined queries downstream to the public in the form of cacheable RESTful urls.
4747

48-
Instead of exposing your server by having your web interface interact with api calls directly to `http://yourgraphqlserver.com/?query={ getUserById(1) { firstName, lastName }` that can be altered to `http://yourgraphqlserver.com/?query={ getUserById(1) { firstName, lastName, socialSecurityNumber }`, GraphQL Rest Router allows you to predefine a query and expose it as `http://yourserver/api/users/1` and always execute a safe query with minimal code:
48+
Instead of exposing your server by having your client or web application (e.g. React, Angular, etc...) perform api calls directly to `http://yourgraphqlserver.com/?query={ getUserById(1) { firstName, lastName }` that could then be altered to `http://yourgraphqlserver.com/?query={ getUserById(1) { firstName, lastName, socialSecurityNumber }`, GraphQL Rest Router allows you to predefine a query and expose it as a restful route, such as `http://yourserver/api/users/1`. This ensures end users are only able to execute safe, performant, and tested queries.
4949

5050
```js
5151
import GraphQLRestRouter from 'graphql-rest-router';
@@ -78,7 +78,7 @@ api.listen(3000);
7878
See [Usage with Express](/#) and read [Getting Started](/#) to see all available options.
7979

8080
## External GraphQL API
81-
When dealing with a publicly exposed GraphQL layer, the main benefit GraphQL Rest Client provides is caching. While implementing individual caches at a content level with dynamic expiration in the GraphQL layer is optimal, actually building these systems out are laborous and aren't always included in MVP products. GraphQL Rest Client allows you to expose a GraphQL query as a REST endpoing with built in cache management that is compatible with all CDNs and cache management layers (e.g. CloudFlare, Akamai, Varnish, etc).
81+
When dealing with a publicly exposed GraphQL server that implements users and priveleges, the main benefit GraphQL Rest Client provides is caching. While implementing individual caches at a content-level with push-expiration in the GraphQL server is optimal, building these systems is laborous and isn't always prioritized in an MVP product. GraphQL Rest Client allows you to expose a GraphQL query as a REST endpoing with built in cache management that is compatible with all CDNs and cache management layers (e.g. CloudFlare, Akamai, Varnish, etc).
8282

8383
One line of GraphQL Rest Router code allows you to take
8484
```gql
@@ -101,17 +101,71 @@ api.mount('UserById').at('/user/:id');
101101
GraphQL Rest Router is available via NPM as `graphql-rest-router` (`npm install graphql-rest-router`)
102102

103103
## Getting Started
104+
GRR is still in alpha so the DSL may be subject to changes.
104105

105-
### Configuring GraphQL Rest Router
106+
Get started by installing GraphQL Rest Router as a production dependency in your application with: `npm install --save graphql-rest-router`.
106107

107-
### Mounting Queries
108+
To instantiate a bare bones GraphQL Rest Router instance you'll need both the location of your GraphQL Server and the client schema you'll be using. It is advised that you create one `.gql` file per application that holds all of the application's respective queries.
109+
110+
GQL Rest Router leverages [Operation Names](https://graphql.org/learn/queries/#operation-name) and [Variables](https://graphql.org/learn/queries/#variables) as a way to transform your provided schema into a REST endpoint. **Make sure that your queries and mutations are all utilizing operation names or they will not be mountable.**
111+
112+
For Example:
113+
```gql
114+
# THIS
115+
query GetFirstUser {
116+
getUser(id: 1) {
117+
id
118+
firstName
119+
lastName
120+
}
121+
}
122+
123+
# NOT THIS
124+
{
125+
getUser(id: 1) {
126+
id
127+
firstName
128+
lastName
129+
}
130+
}
131+
```
132+
133+
Once you have your schema and your endpoint, instantiation is straight-forward:
134+
135+
```js
136+
import GraphQLRestRouter from 'graphql-rest-router';
137+
138+
const schema = fs.readFile(`${__dirname}/schema.gql`);
139+
const endpoint = 'http://mygraphqlserver.com:9000';
140+
141+
const api = new GraphQLRestRouter(endpoint, schema);
142+
```
143+
144+
### Proxies and Authentication
145+
If the server that you are running GraphQL Rest Router on requires a proxy to connect to the GraphQL server or credentials to connect, you may pass them directly into GQL Rest Router during instantiation or on a per route basis to limit them to specific routes.
146+
147+
### Advanced Configuration of GraphQL Rest Router
148+
GraphQL Rest Router takes an optional third parameter during initialization that allows you to control default cache, headers, authentication and proxies
149+
150+
```
151+
const options = {
152+
defaultCacheTimeInMs: 3000,
153+
};
154+
155+
new GraphQLRestRouter(endpoint, schema, options);
156+
```
157+
158+
A list of options and their default values is below:
159+
**TODO TABLE HERE**
160+
161+
### Creating Endpoints
108162

109163
### GET / POST
110164

111165
## Caching
112166
GraphQL Rest Router ships with two cache interfaces stock and supports any number of custom or third party caching interfaces as long as they adhere to `ICacheInterface`
113167

114-
### InMemory Cache
168+
### In Memory Cache
115169
InMemoryCache stores your cached route data on your server in memory. This can be used in development or with low TTLs in order to prevent a [thundering herd](https://en.wikipedia.org/wiki/Thundering_herd_problem) however it is strongly discouraged to use this in production. In Memory caches have the ability to deplete your system's resources and take down your instance of GraphQLRestRouter.
116170

117171
```js
@@ -131,6 +185,7 @@ api.mount('GetUser', { cacheTimeInMs: 500 });
131185
```
132186

133187
### Redis Cache
188+
As of the time of this writing, a REDIS interface for GQL Rest Router is not yet available. Feel free to submit a PR.
134189

135190
## Swagger / Open API
136191
As GraphQL Rest Router exposes your API with new routes that aren't covered by GraphQL's internal documentation or introspection queries, GraphQL Rest Router ships with support for Swagger (Open Api V2), Open API (V3) and API Blueprint (planned). When mounting a documentation on GraphQL Rest Router, it will automatically inspect all queries in the schema you provided and run an introspection query on your GraphQL server to dynamically assemble and document the types / endpoints.
@@ -169,11 +224,41 @@ const api = new GraphQLRestRouter('http://yourgraphqlendpoint', schema);
169224
api.mount(swaggerDocumentation).at('/docs/swagger');
170225
```
171226

227+
### API Blueprint
228+
Planned for V1 but not available in Alpha
172229

173230
## Custom Routes
174231

175-
## Usage with Express
232+
## Usage with Web Frameworks
233+
Currently GQL Rest Router only supports Express out of the box. Please submit a PR or an Issue if you would like to see GQL Rest Router support additional frameworks.
234+
235+
### Usage with Express
236+
It is common to leverage GraphQL Rest Client on a server that is already delivering a website as opposed to standing up a net new server. To integrate with an existing express server, simply export GraphQL Rest Router as express using `.asExpressRouter()` instead of starting up a new server using `.listen(port)`.
237+
238+
For Example:
239+
```js
240+
// api.js
241+
const api = new GraphQLRestRouter(endpoint, schema);
242+
243+
api.mount('GetUserById').at('/users/:id');
244+
245+
export default api.asExpressRouter();
246+
247+
// server.js
248+
import express from 'express';
249+
import api from './api';
250+
251+
const app = express();
252+
253+
app.get('/status', (req, res) => ...);
254+
app.get('/', (req, res) => ...);
255+
app.use('/api', api); // MOUNTS GQL REST ROUTER ON :3000/api/* (e.g. :3000/api/users/4)
256+
257+
app.listen(3000);
258+
```
176259

177-
## Usage with KOA
260+
### Usage with KOA
261+
As of the time of this writing, a KOA extension for GQL Rest Router is not yet available. Feel free to submit a PR.
178262

179-
## Examples
263+
### Code Examples
264+
See the [examples folder](/examples) in this repo for code examples.

0 commit comments

Comments
 (0)