voxmedia/graphql-tools

Name: graphql-tools

Owner: Vox Media

Description: :wrench: Build, mock, and stitch a GraphQL schema using the schema language

Created: 2018-04-30 16:59:38.0

Updated: 2018-05-15 21:17:44.0

Pushed: 2018-05-16 22:14:51.0

Homepage: https://www.apollographql.com/docs/graphql-tools/

Size: 1770

Language: TypeScript

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

GraphQL-tools: generate and mock GraphQL.js schemas

npm version Build Status Coverage Status Get on Slack

This package provides a few useful ways to create a GraphQL schema:

  1. Use the GraphQL schema language to generate a schema with full support for resolvers, interfaces, unions, and custom scalars. The schema produced is completely compatible with GraphQL.js.
  2. Mock your GraphQL API with fine-grained per-type mocking
  3. Automatically stitch multiple schemas together into one larger API
Documentation

Read the docs.

Binding to HTTP

If you want to bind your JavaScript GraphQL schema to an HTTP server, we recommend using Apollo Server, which supports every popular Node HTTP server library including Express, Koa, Hapi, and more.

JavaScript GraphQL servers are often developed with graphql-tools and apollo-server-express together: One to write the schema and resolver code, and the other to connect it to a web server.

Example

See and edit the live example on Launchpad.

When using graphql-tools, you describe the schema as a GraphQL type language string:

t typeDefs = `
 Author {
: ID! # the ! means that every author object _must_ have an id
rstName: String
stName: String
"
e list of Posts by this author
"
sts: [Post]


 Post {
: ID!
tle: String
thor: Author
tes: Int


e schema allows the following query:
 Query {
sts: [Post]


is schema allows the following mutation:
 Mutation {
votePost (
postId: ID!
 Post


 need to tell the server which types represent the root query
d root mutation types. We call them RootQuery and RootMutation by convention.
ma {
ery: Query
tation: Mutation



rt default typeDefs;

Then you define resolvers as a nested object that maps type and field names to resolver functions:

t resolvers = {
ery: {
posts() {
  return posts;
},

tation: {
upvotePost(_, { postId }) {
  const post = find(posts, { id: postId });
  if (!post) {
    throw new Error(`Couldn't find post with id ${postId}`);
  }
  post.votes += 1;
  return post;
},

thor: {
posts(author) {
  return filter(posts, { authorId: author.id });
},

st: {
author(post) {
  return find(authors, { id: post.authorId });
},



rt default resolvers;

At the end, the schema and resolvers are combined using makeExecutableSchema:

rt { makeExecutableSchema } from 'graphql-tools';

t executableSchema = makeExecutableSchema({
peDefs,
solvers,

This example has the entire type definition in one string and all resolvers in one file, but you can combine types and resolvers from multiple files and objects, as documented in the modularizing the schema section of the docs.

Contributions

Contributions, issues and feature requests are very welcome. If you are using this package and fixed a bug for yourself, please consider submitting a PR!


This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.