Introduction
GraphQL Tools is a set of npm packages(@graphql-tools/*
) and an opinionated structure for how tobuild a GraphQL schema and resolvers in JavaScript, following the GraphQL-first developmentworkflow.
Functions in the@graphql-tools/*
packages are not only useful for building servers. They can alsobe 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 ifyou don’t follow our structure; they work with any GraphQL-JS schema, and each tool can be useful onits own.
Using GraphQL with HTTP
If you want to bind your JavaScript GraphQL schema to an HTTP server, you can useGraphQL Yoga .
You can develop your JavaScript-based GraphQL API withGraphQL Tools andGraphQL Yogatogether: 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 schemais the first thing you design, and acts as the contract between your frontend and backend. It’s notnecessarily for everyone, but it can be a great way to get a server up and running with a very clearseparation of concerns. These concerns are aligned with Facebook’s direction about the best way touse GraphQL, and our own findings after thinking about the best way to architect a JavaScriptGraphQL API codebase.
- Use the GraphQL schema language. Theofficial GraphQL documentation explains schema concepts usinga concise and easy-to-read language. Thegetting 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 the features of GraphQL including resolvers,interfaces, custom scalars, and more so that you can have a seamless flow from design to mockingto implementation. For a more complete overview of the benefits, check out Nick Nance’s talk,Managing GraphQL Development at Scale . - 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 actualbusiness logic, permissions, and other concerns should not be part of your GraphQL schema. Forlarge 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.
- Use standard libraries for auth and other special concerns. There’s no need to reinvent thelogin 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 beingserved through a GraphQL endpoint, and it is okay to have non-GraphQL endpoints on your serverwhen it’s the most practical solution.