Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

graphql-tools-builder

Package Overview
Dependencies
Maintainers
1
Versions
15
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

graphql-tools-builder

![Travis](https://api.travis-ci.org/mrphu3074/graphql-tools-builder.svg?branch=master) [![codecov](https://codecov.io/gh/mrphu3074/graphql-tools-builder/branch/master/graph/badge.svg)](https://codecov.io/gh/mrphu3074/graphql-tools-builder)

  • 1.0.14
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
16
increased by433.33%
Maintainers
1
Weekly downloads
 
Created
Source

Travis codecov


graphql-tools-builder

Create Graphql schema effortlessly

This library like a plugin for graphql-tools to organize your modules easier.

Graphl-tools style

const typeDefs = `
scalar Date

type User {
  _id: String
  username: String
  password: String
  createdAt: Date
  modifiedAt: Date
  createdBy: String
  modifiedBy: String
  creator: User
  modifier: User
}

input CreateUserForm {
  username: String!
  password: String!
}

type Query {
	#get all users
  getUsers: [User]
}

type Mutation {
  #Create new user
  createUser(data: CreateUserForm!): User
}
`;

const resolvers = {
  User: {
    creator(root, args, context) {
      if(root.createdBy) {
        return context.User.findOne({_id: root.createdBy});
      }
      return null;
    },
    modifier(root, args, context) {
      if(root.modifiedBy) {
        return context.User.findOne({_id: root.modifiedBy});
      }
      return null;
    }
  },
  Query: {
    getUsers(root, args, context) {
      return context.User.find({});
    }
  },
  Mutation: {
    createUser(root, args, context) {
      return context.User.insert(args.data);
    }
  }
}

import { makeExecutableSchema } from 'graphql-tools';

// Generarte main schema
const schema = makeExecutableSchema({
  typeDefs,
  resolvers
});

Graphl-tools-builder style

import { makeExecutableSchema } from 'graphql-tools';
import { Schema, Module } from 'graphql-tools-builder';
const GraphqlDate = require('graphql-date);

const schema = new Schema();
const commons = new Module('Commons');
const user = new Module('User');
schema.addModule(commons);
schema.addModule(user);

// set up commons type
commons.createScalar('Date').resolver(GraphqlDate);
// set up user module
user.createType('User')
  .field('_id', 'String')
  .field('username', 'String')
  .field('password', 'String')
  .field('createdAt', 'Date')
  .field('modifiedAt', 'Date')
  .field('createdBy', 'String')
  .field('modifiedBy', 'String')
  .field('creator', 'User')
  .field('modifier', 'User')
  .resolver({
    creator(root, args, context) {
      if(root.createdBy) {
        return context.User.findOne({_id: root.createdBy});
      }
      return null;
    },
    modifier(root, args, context) {
      if(root.modifiedBy) {
        return context.User.findOne({_id: root.modifiedBy});
      }
      return null;
    }
  });

user.createInput('CreateUserForm')
  .field('username', 'String', true)
  .field('password', 'String', true);

user.createQuery('getUsers')
  .output('[User]')
  .resolver((root, args, context) => {
    return context.User.find({});
  });

user.createMutation('createUser')
  .param('data', 'CreateUserForm', true)
  .output('User')
  .resolver((root, args, context) => ({
    return context.User.insert(args.data);
  }));

// Generarte main schema
const graphqlSchema = makeExecutableSchema({
  typeDefs: schema.getTypeDefs(),
  resolvers: schema.getResolvers()
});

FAQs

Package last updated on 23 Aug 2017

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc