graphql-tools

Build, mock, and stitch a GraphQL schema using the schema language

README

toolkit
npm version CI Discord Chat code style: prettier [![renovate-app badge][renovate-badge]][renovate-app]

[renovate-badge]: https://img.shields.io/badge/renovate-app-blue.svg
[renovate-app]: https://renovateapp.com/

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



Binding to HTTP


If you want to bind your JavaScript GraphQL schema to an HTTP server, you can use [GraphQL Yoga](https://www.graphql-yoga.com) .

You can develop your JavaScript based GraphQL API with graphql-tools and GraphQL Yoga together: One to write the schema and resolver code, and the other to connect it to a web server.

Example


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

  1. ``` js
  2. const typeDefs = /* GraphQL */ `
  3.   type Author {
  4.     id: ID! # the ! means that every author object _must_ have an id
  5.     firstName: String
  6.     lastName: String
  7.     """
  8.     the list of Posts by this author
  9.     """
  10.     posts: [Post]
  11.   }

  12.   type Post {
  13.     id: ID!
  14.     title: String
  15.     author: Author
  16.     votes: Int
  17.   }

  18.   # the schema allows the following query:
  19.   type Query {
  20.     posts: [Post]
  21.   }

  22.   # this schema allows the following mutation:
  23.   type Mutation {
  24.     upvotePost(postId: ID!): Post
  25.   }

  26.   # we need to tell the server which types represent the root query
  27.   # and root mutation types. We call them RootQuery and RootMutation by convention.
  28.   schema {
  29.     query: Query
  30.     mutation: Mutation
  31.   }
  32. `

  33. export default typeDefs
  34. ```

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

  1. ``` js
  2. const resolvers = {
  3.   Query: {
  4.     posts() {
  5.       return posts
  6.     }
  7.   },
  8.   Mutation: {
  9.     upvotePost(_, { postId }) {
  10.       const post = find(posts, { id: postId })
  11.       if (!post) {
  12.         throw new Error(`Couldn't find post with id ${postId}`)
  13.       }
  14.       post.votes += 1
  15.       return post
  16.     }
  17.   },
  18.   Author: {
  19.     posts(author) {
  20.       return filter(posts, { authorId: author.id })
  21.     }
  22.   },
  23.   Post: {
  24.     author(post) {
  25.       return find(authors, { id: post.authorId })
  26.     }
  27.   }
  28. }

  29. export default resolvers
  30. ```

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

  1. ``` js
  2. import { makeExecutableSchema } from '@graphql-tools/schema'

  3. const executableSchema = makeExecutableSchema({
  4.   typeDefs,
  5.   resolvers
  6. })
  7. ```

GraphQL-Tools schema can be consumed by frameworks like GraphQL Yoga, Apollo GraphQL or express-graphql
For example in Node.js;

  1. ``` js
  2. const { createServer } = require('@graphql-yoga/node')

  3. const server = createServer({
  4.   schema: executableSchema
  5. })

  6. server.start()
  7. ```

You can check GraphQL Yoga for other JavaScript platforms and frameworks besides vanilla Node.js HTTP.

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 type definitions and merging resolvers 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!

And if this is your first time contributing to this project, please do read our Contributor Workflow Guide before you get started off.

Code of Conduct


Help us keep GraphQL Tools open and inclusive. Please read and follow our Code of Conduct as adopted from Contributor Covenant

Maintainers