@@ -12,6 +12,8 @@ GraphQL Rest Router allows you to expose an internal GraphQL API as a REST API.
1212_ Example Usage_
1313``` js
1414import GraphQLRestRouter from ' graphql-rest-router' ;
15+ import * as OpenApi from ' graphql-rest-router' ;
16+
1517const Schema = `
1618 query UserById($id: Int!) {
1719 getUserById(id: $id) {
@@ -31,6 +33,22 @@ const Schema = `
3133 }
3234` ;
3335
36+ const swaggerDoc = new OpenApi.V2 ({
37+ title: ' Example API' ,
38+ version: ' 1.0.0' ,
39+
40+ host: ' http://127.0.0.1' ,
41+ basePath: ' /api' ,
42+ });
43+
44+ const openApiDoc = new OpenApi.V3 ({
45+ title: ' Example API' ,
46+ version: ' 1.0.0' ,
47+
48+ host: ' http://127.0.0.1' ,
49+ basePath: ' /api' ,
50+ });
51+
3452const api = new GraphQLRestRouter (url, Schema, {
3553 cacheEngine: GraphQLRestRouter .InMemoryCache ,
3654
@@ -43,8 +61,7 @@ const api = new GraphQLRestRouter(url, Schema, {
4361 },
4462});
4563
46- api .mount ({
47- operationName: ' UserByEmail'
64+ api .mount (' UserByEmail' , {
4865 path: ' /user/:email' ,
4966
5067 method: ' POST' ,
@@ -63,16 +80,12 @@ api.mount({
6380api .mount (' UserById' ); // creates GET /UserById?id=:id
6481
6582api .mount (' UserById' ).at (' /:id' ) // creates GET /:id
66- .setCacheTimeInMs (200 );
6783
6884api .mount (' UserById' ).at (' /:id' ).as (' POST' )
6985 .disableCache ()
70- .modifyRequest ( request => {
71- request . addHeader ( ' x-jwt' , ' test ' ) ;
86+ .transformRequest ( headers => {
87+ headers[ ' x-context- jwt' ] = ' 1234 ' ;
7288 })
73- .modifyResponse (response => {
74- response .setStatus (200 );
75- });
7689
7790api .mount (' UserById' ).withOptions ({
7891 path: ' /user/0' ,
@@ -83,8 +96,9 @@ api.mount('UserById').withOptions({
8396 }
8497});
8598
99+ api .mount (swaggerDoc).at (' /docs/swagger' );
100+ api .mount (openApiDoc).at (' /docs/openapi' );
101+
86102// Export Options
87- api .asExpressRouter ();
88- api .asKoaRouter ();
89- api .listen (3000 , () => console .log (' Callback' ));
103+ module .exports = api .asExpressRouter ();
90104```
0 commit comments