Giter Site home page Giter Site logo

graphql-rest-wrapper's Introduction

GraphQL - REST Wrapper


Introduction

graphql-rest-wrapper let you use GraphQL on top of your existing REST API very easily. using express-graphql.

See code example

Code Example

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 = new gqlRestWrapper('http://localhost:9090/restapi', {
    name: 'MyRestAPI',
    generateSchema: true,
    saveSchema: true,
    graphiql: true
})

app.use('/graphql', wrapper.expressMiddleware())

How does it work

  1. When instantiating 'gqlRestWrapper' class, a GraphQL schema will be made for you from the REST API response. (you can provide a schema yourself)
  2. GraphQL wrapper will expose the express middleware function to a chosen route.
  3. 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.

Installation

Install the npm package

npm i graphql-rest-wrapper

import

var gqlRestWrapper = require(graphql-rest-wrapper)

Instantiate:

/**
 *  GraphQL rest wrapper takes all express-graphql options and some additional options.
 *
 *  Options object:
 *
 *  - express-graphql: (Main ones)
 *  schema: GraphQLSchema    -> You can specify a schema or let gqlRestWrapper to generate one with [generateSchema: true]
 *  graphiql: Boolean        -> Render GraphQL query client
 *
 *  - gqlRestWrapper:
 *  name: String            -> Will name the main query, and file if asked for
 *  generateSchema: Boolean -> Generate schema from the API response
 *  saveSchema: Boolean     -> Save the generated schema to a javascript file
 *
 */

new gqlRestWrapper([ENDPOINT], [OPTIONS])

attach middleware to a route

app.use([ROUTE], wrapper.expressMiddleware())

Sending a query: Simplest way to send a query is using GraphiQL, by setting it to 'true' and just navgating to GraphQL route.

{
  MyRestAPI {
    id
    name
    email
    last_name
    active
    friends
    password {
      hash
      value
    }
  }
}

Or making an HTTP GET/POST request:

fetch("http://localhost:9090/graphql",
    {
        headers: {
            'Accept': 'application/json',
            'Content-Type': 'application/json'
        },
        method: "POST",
        body: "{'query': 'query { MyRestAPI { id, name } }'}"
    })
    .then(function (res) {
        console.log(res);
    })

Schema builder

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: 9,
 email : "[email protected]",
 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:

new gqlRestWrapper([ENDPOINT], {
    name: 'MyRestAPI',
    //generateSchema: true,
    //saveSchema: true,
    schema: require(./schema-file)
    graphiql: true
})

Tests

Contributors

License

MIT

graphql-rest-wrapper's People

Contributors

aarmand avatar alonp99 avatar

Watchers

James Cloos avatar d0wHc3r avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.