graphql-rest-wrapper let you use GraphQL on top of your existing REST API very easily. using express-graphql.
The following code will fetch the REST API response, make a GraphQL schema for you, and expose the data from a GraphQL server.
const wrapper = ''name: 'MyRestAPI'generateSchema: truesaveSchema: truegraphiql: trueapp
- When instantiating 'gqlRestWrapper' class, a GraphQL schema will be made for you from the REST API response. (you can provide a schema yourself)
- GraphQL wrapper will expose the express middleware function to a chosen route.
- When you will send an http request to the route with GraphQL query, GraphQL server will fetch the response from the REST API and send you only the data you asked for.
Install the npm package
npm i graphql-rest-wrapper
var gqlRestWrapper =
attach middleware to a route
Sending a query: Simplest way to send a query is using GraphiQL, by setting it to 'true' and just navgating to GraphQL route.
Or making an HTTP GET/POST request:
The GraphQL schema is being built with a modified version of Aweary/json-to-graphql package it will work for simple data structures like I use in the example code:
name : "john_s"first_name : "John"last_name : "Smith"display_name : "Johnny"id: 9email : "email@example.com"password :value : "my_password"hash : "0a0655l2hg32hbr2d23f239"friends: 'Mike' 'Jen' 'Chris' 'Tom'active : true
But can fall short on more complex data structures, In those cases it needs some help. Just use [saveSchema: true] and tweak it the way you like. then import the file and pass it to the wrapper:
ENDPOINTname: 'MyRestAPI'//generateSchema: true,//saveSchema: true,schema:graphiql: true