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
User | Most Recent Commit | # Commits |
---|
Other Committers
User | Most Recent Commit | # Commits |
---|
This package provides a few useful ways to create a GraphQL schema:
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.
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, issues and feature requests are very welcome. If you are using this package and fixed a bug for yourself, please consider submitting a PR!