graphql-tools

A set of utilities to build your JavaScript GraphQL schema in a concise and powerful way.

GraphQL Tools is an npm package and an opinionated structure for how to build a GraphQL schema and resolvers in JavaScript, following the GraphQL-first development workflow.

1
npm install graphql-tools graphql

Functions in the graphql-tools package are not just useful for building servers. They can also be used in the browser, for example to mock a backend during development or testing.

Even though we recommend a specific way of building GraphQL servers, you can use these tools even if you don’t follow our structure; they work with any GraphQL-JS schema, and each tool can be useful on its own.

Hello world example

To get started with graphql-tools right away, run the example from the Apollo Server docs.

Using GraphQL with 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.

The GraphQL-first philosophy

This package enables a specific workflow for developing a GraphQL server, where the GraphQL schema is the first thing you design, and acts as the contract between your frontend and backend. It’s not necessarily for everyone, but it can be a great way to get a server up and running with a very clear separation of concerns. These concerns are aligned with Facebook’s direction about the best way to use GraphQL, and our own findings after thinking about the best way to architect a JavaScript GraphQL API codebase.

  1. Use the GraphQL schema language. The official GraphQL documentation explains schema concepts using a concise and easy to read language. The getting started guide for GraphQL.js now uses the schema to introduce new developers to GraphQL. graphql-tools enables you to use this language alongside with all of the features of GraphQL including resolvers, interfaces, custom scalars, and more, so that you can have a seamless flow from design to mocking to implementation. For a more complete overview of the benefits, check out Nick Nance’s talk, Managing GraphQL Development at Scale.
  2. Separate business logic from the schema. As Dan Schafer covered in his talk, GraphQL at Facebook, it’s a good idea to treat GraphQL as a thin API and routing layer. This means that your actual business logic, permissions, and other concerns should not be part of your GraphQL schema. For large apps, we suggest splitting your GraphQL server code into 4 components: Schema, Resolvers, Models, and Connectors, which each handle a specific part of the work. You can see this in action in the server part of our GitHunt example app.
  3. Use standard libraries for auth and other special concerns. There’s no need to reinvent the login process in GraphQL. Every server framework already has a wealth of technologies for auth, file uploads, and more. It’s prudent to use those standard solutions even if your data is being served through a GraphQL endpoint, and it is okay to have non-GraphQL endpoints on your server when it’s the most practical solution.

The GitHunt-API example

We have produced a complete example app that represents this model for constructing a small GraphQL API. Check it out at apollostack/GitHunt-API on GitHub.

Edit on GitHub