Skip to content

Instantly share code, notes, and snippets.

@mxlje
Last active October 15, 2015 19:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mxlje/80545ec28a7841ce2411 to your computer and use it in GitHub Desktop.
Save mxlje/80545ec28a7841ce2411 to your computer and use it in GitHub Desktop.
Code samples for “Understanding GraphQL Server” https://keywordbrain.com/blog/understanding-graphql-server/
let arguments = { someId: "1234" }
graphql(schema, query, null, arguments)
// named
query Foo { foo }
// unnamed
{ foo }
query Foo($someId: String) {
foo(id: $someId) {
bar
}
}
let BlogAuthor = new GraphQLObjectType({
name: 'Author',
fields: () => ({
id: { type: GraphQLString },
name: { type: GraphQLString }
})
})
let BlogArticle = new GraphQLObjectType({
name: 'Article',
fields: {
id: { type: new GraphQLNonNull(GraphQLString) },
isPublished: { type: GraphQLBoolean },
author: { type: BlogAuthor },
title: { type: GraphQLString },
body: { type: GraphQLString },
keywords: { type: new GraphQLList(GraphQLString) }
}
})
let RootQuery = new GraphQLObjectType({
name: 'Query',
fields: {
article: {
type: BlogArticle,
args: { id: { type: GraphQLID } },
// fetches an article from the database, including the author
resolve: (_, {id}) => article(id)
}
}
})
let schema = new GraphQLSchema({
query: RootQuery
});
graphql(schema, query).then((result) => {
// do something with the result here
});
let RootQuery = new GraphQLObjectType({
name: 'Query',
fields: …
})
let schema = new GraphQLSchema({
query: RootQuery,
mutation: RootMutation
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment