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, see this complete live example in Apollo Launchpad..
Using GraphQL with HTTP
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
- 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-toolsenables 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.
- 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.
- 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.