Awesome Open Source
Awesome Open Source

waterline-to-graphql

##Stability status: alpha Waterline to graphql adapter. This library converts waterline models to graphql types. You can execute graphql query which in turns resolves into waterline queries behind the scenes.

build status npm version npm downloads

##Basic Usage: (See waterline-example in examples folder)

import { getGraphQLSchemaFrom } from 'waterline-to-graphql'; 
.....
  • Pass in intialized models aka waterline collections.
  • If you are using standalone waterline models need to be patched with associations array. See here . See waterline-example in examples folder
  • If you are passing in sails.models, you dont need the above patch
let schema = getGraphQLSchemaFrom(models);
  • Execute graphql query
var query = '{ users{firstName,lastName posts{text,comments{text}}} }';
    graphql(schema, query).then(result => {
      console.log(JSON.stringify(result, null, 2));
    });
  • Example transformation Waterline
module.exports = {
  identity: 'user',
  attributes: {
    firstName: {
      type: 'string',
      required: true
    },
    lastName: {
      type: 'string',
      required: true
    },
    email: {
      type: 'email',
      required: true
    },
    phone: 'string',
    posts: {
      collection: 'post',
      via: 'from'
    },
    comments: {
      collection: 'comment',
      via: 'from'
    }
  }
};

GraphQL

// User
let UserType = new GraphQLObjectType({
  name: 'user',
  fields: () => ({
    firstName: {
      type: GraphQLString
    },
    lastName: {
      type: GraphQLString
    },
    email: {
      type: GraphQLString
    },
    phone: GraphQLString,
    posts: {
      type: new GraphQLList(PostType)
    },
    comments: {
      type: new GraphQLList(CommentType)
    }
  }),
  interfaces:[Node]
});

##Using with sails,express,relay: If you are using with express/sails , you can define graphql middleware as below.

  • npm i waterline-to-graphql
  • Add the following in config/http.js.
    graphql: function(req, res, next) {
      console.log('executing graphql query');
      if (req.url === '/graphql') {
        var schema = getGraphQLSchemaFrom(sails.models);
        require('express-graphql')({
            schema: schema,
            pretty: true
          })(req, res);
      } else {
        return next();
      }
    }

####See (react-relay-graphql-sails-example)

##TODO Add mutations Add frontend code for react-relay-graphql-sails-example

Alternatives To Waterline To Graphql
Select To Compare


Alternative Project Comparisons
Related Awesome Lists
Top Programming Languages

Get A Weekly Email With Trending Projects For These Topics
No Spam. Unsubscribe easily at any time.
Javascript (1,155,559
Graphql (16,378
Adapter (11,120
Relay (4,314
Sails (1,138
Graphql Query (217