Skip to content

Commit 18a0ba2

Browse files
Update README.md
1 parent d4727b5 commit 18a0ba2

File tree

1 file changed

+95
-4
lines changed

1 file changed

+95
-4
lines changed

README.md

Lines changed: 95 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -141,11 +141,104 @@ const endpoint = 'http://mygraphqlserver.com:9000';
141141
const api = new GraphQLRestRouter(endpoint, schema);
142142
```
143143

144+
### Creating Endpoints
145+
Once GraphQL Rest Router has been configured setting up endpoints to proxy queries through is simple. Make sure that the schema you've provided is utilizing [Operation Names](https://graphql.org/learn/queries/#operation-name) and `mount(OperationName)` to have GQL Rest Router automatically scan your schema for the desired operation and create a RESTful endpoint for it. If you attempt to mount a non-named query or a query that does not exist within your provided schema, GraphQL Rest Router will throw an exception.
146+
147+
```js
148+
const api = new GraphQLRestRouter(endpoint, schema);
149+
150+
api.mount('OperationName'); // Mounts "query OperationName" as "GET /OperationName"
151+
```
152+
153+
#### GET / POST
154+
By default, mounted queries are GET requests. If you'd like to change that you may specify any http method using `.as()` on a route.
155+
156+
Example:
157+
```
158+
const api = new GraphQLRestRouter(endpoint, schema);
159+
160+
api.mount('GetUserById'); // GET /GetUserById
161+
api.mount('UpdateUser').as('put'); // PUT /UpdateUser
162+
api.mount('CreateUser').as('post'); // POST /CreateUser
163+
```
164+
165+
#### Variables
166+
GraphQL Rest Router will read your provided schema to determine which variables are required and optional. If you are unsure how to create a named operation with variables, the [official GraphQL documentation](https://graphql.org/learn/queries/#variables) has examples. When mounted as a GET endpoint, the variables will be expected as query parameters, while all other methods will check the body for the required variables.
167+
168+
In order to reduce unnecessary load on the GraphQL server, GQL Rest Router validates the variables you've provided before sending a request to the GraphQL server.
169+
170+
Example Schema:
171+
```gql
172+
# If GetUserById is mounted:
173+
#
174+
# - A GET request to /GetUserById will require you to pass in a query parameter of id or it will error.
175+
#
176+
# Example:
177+
# URL: /GetUserById?id=1
178+
# Method: GET
179+
#
180+
# - A POST request to /GetUserByID will require you to pass in a body that conatins a JSON object with the key id.
181+
#
182+
# Example:
183+
# Url: /GetUserById
184+
# Method: POST
185+
# Headers: { Content-Type: application/json }
186+
# Body: { "id": 1 }
187+
#
188+
query GetUserById($id: Int!) {
189+
getUserById(id: $id) {
190+
firstName
191+
lastName
192+
}
193+
}
194+
195+
# If SearchUsers is mounted:
196+
#
197+
# - A GET request to /SearchUsers will require you to pass in a query parameter of searchTerm or it will error. Optionally you # may pass in page and resultsPerPage as well (/SearchUsers?searchTerm=pesto&page=1&resultsPerPage=10)
198+
#
199+
# Example:
200+
# URL: /SearchUsers?id=1
201+
# Method: GET
202+
#
203+
# - A POST request to /SearchUsers will require you to pass in a body that conatins a JSON object with the key searchTerm and # the optional parameters of page and resultsPerPage.
204+
#
205+
# Example:
206+
# Url: /GetUserById
207+
# Method: POST
208+
# Headers: { Content-Type: application/json }
209+
# Body: { "searchTerm": "pesto", page: 1 }
210+
#
211+
query SearchUsers($page: Int = 1, $resultsPerPage: Int, $searchTerm: String!) {
212+
searchUsers(resultsPerPage: $resultsPerPage, page: $page, query: $searchTerm) {
213+
email
214+
firstName
215+
lastName
216+
}
217+
}
218+
`;
219+
```
220+
221+
#### Custom Paths
222+
If no path is provided to a mounted route, it will be made available exactly as it is type in the operation name:
223+
```js
224+
api.mount('GetUserById'); // Made available at /GetUserById
225+
```
226+
227+
It is possible to change/customize this mounting path by using `.at(pathname)` on a route.
228+
```js
229+
api.mount('GetUserById').at('/user'); // A call to '/user?id=42' will execute a 'GetUserById' operation on your GQL Server with an id of 42
230+
```
231+
232+
It is also possible to describe a required variable in the path using a syntax similar to that of express routes
233+
```js
234+
api.mount('GetUserById').at('/user/:id'); // A call to /user/42 will execute a 'GetUserById'operation on your GQL server with an id of 42
235+
```
236+
144237
### 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.
238+
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. See [Advanced Configuration of GraphQL Rest Router](#Advanced-Configuration-of-GraphQL-Rest-Router) for implementation
146239

147240
### 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
241+
GraphQL Rest Router takes an optional third parameter during initialization that allows you to control default cache, headers, authentication and proxies.
149242

150243
```js
151244
const options = {
@@ -169,8 +262,6 @@ A list of options and their default values is below:
169262
| proxy | [AxiosProxyConfig](https://github.com/axios/axios/blob/master/index.d.ts#L14-L22) | null | If a proxy is required to communicate with your GraphQL server from the server that GQL Rest Router is running on, provide it here. |
170263
| cacheEngine | ICacheEngine | null | Either a cache engine that [ships default](#Caching) with GQL Rest Router or adheres to the [ICacheEngine interface](#Custom-Cache-Engine) |
171264

172-
### Creating Endpoints
173-
174265
## Caching
175266
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`
176267

0 commit comments

Comments
 (0)