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
+95-10Lines changed: 95 additions & 10 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -41,11 +41,11 @@ __Table of Contents__
41
41
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.
42
42
43
43
## 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).
45
45
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.
47
47
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.
49
49
50
50
```js
51
51
importGraphQLRestRouterfrom'graphql-rest-router';
@@ -78,7 +78,7 @@ api.listen(3000);
78
78
See [Usage with Express](/#) and read [Getting Started](/#) to see all available options.
79
79
80
80
## 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 contentlevel 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).
82
82
83
83
One line of GraphQL Rest Router code allows you to take
GraphQL Rest Router is available via NPM as `graphql-rest-router` (`npm install graphql-rest-router`)
102
102
103
103
## Getting Started
104
+
GRR is still in alpha so the DSL may be subject to changes.
104
105
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`.
106
107
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
+
queryGetFirstUser {
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:
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
108
162
109
163
### GET / POST
110
164
111
165
## Caching
112
166
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`
113
167
114
-
### InMemory Cache
168
+
### In Memory Cache
115
169
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.
As of the time of this writing, a REDIS interface for GQL Rest Router is not yet available. Feel free to submit a PR.
134
189
135
190
## Swagger / Open API
136
191
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);
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)`.
0 commit comments