Git Product home page Git Product logo

graffiti's Introduction

graffiti

npm version Codeship Status for RisingStack/graffiti bitHound Overall Score Known Vulnerabilities

Currently the consumption of HTTP REST APIs dominate the client-side world, GraphQL aims to change this. This transition can be time-consuming - this is where graffiti comes into the picture.

We don't want to rewrite our application - no one wants that. graffiti provides an Express middleware, a Hapi plugin and a Koa middleware to convert your existing models into a GraphQL schema and exposes it over HTTP.

What is GraphQL?

GraphQL is a query language created by Facebook in 2012 which provides a common interface between the client and the server for data fetching and manipulations.

The client asks for various data from the GraphQL server via queries. The response format is described in the query and defined by the client instead of the server: they are called client‐specified queries.

For more info check out RisingStack's GraphQL tutorial.

Example server and queries

For a running example server and executable queries, check out our example repository and play with your GraphQL queries: graffiti-example

Adapters

Supported servers

Install

npm install @risingstack/graffiti --save

Usage

  1. run MongoDB
  2. register the middleware
  3. provide a schema (returned by an adapters getSchema method or your own GraphQLSchema instance)
  4. the GraphQL endpoint is available on /graphql

Express

import express from 'express';
import { json } from 'body-parser';
import graffiti from '@risingstack/graffiti';
import { getSchema } from '@risingstack/graffiti-mongoose';

import Cat from './models/Cat';
import User from './models/User';

const app = express();

// parse body as json
app.use(json());

app.use(graffiti.express({
  schema: getSchema([User, Cat]),
  context: {} // custom context
}));

app.listen(3000);

Hapi

import { Server } from 'hapi';
import graffiti from '@risingstack/graffiti';
import { getSchema } from '@risingstack/graffiti-mongoose';

const server = new Server();
server.connection({ port: 3000 });

server.register({
  register: graffiti.hapi,
  options: {
    schema: getSchema([User, Cat]),
    context: {}, // custom context 
    config: {} // config parameter for hapi graphql route
  }
}, function (err) {
  if (err) {
    console.error('Failed to load plugin:', err);
  }

  server.start(function () {
      console.log('Server running at:', server.info.uri);
  });
});

Koa

import koa from 'koa';
import parser from 'koa-bodyparser';
import graffiti from '@risingstack/graffiti';
import { getSchema } from '@risingstack/graffiti-mongoose';

import Cat from './models/Cat';
import User from './models/User';

const app = koa();

app.use(parser());

app.use(graffiti.koa({
  schema: getSchema([User, Cat]),
  context: {} // custom context
}));

app.listen(3000);

Options

  • schema: a GraphQLSchema instance. You can use an adapters getSchema method, or provide your own schema. (required)
  • graphiql: may present GraphiQL when loaded directly from a browser. (default: true)
  • context: custom GraphQL context object. (default: {})

Test

npm test

graffiti's People

Contributors

gergelyke avatar tothandras avatar developer-rakeshpaul avatar

Watchers

James Cloos avatar  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.