Last active
October 15, 2015 19:22
-
-
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/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let arguments = { someId: "1234" } | |
graphql(schema, query, null, arguments) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// named | |
query Foo { foo } | |
// unnamed | |
{ foo } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
query Foo($someId: String) { | |
foo(id: $someId) { | |
bar | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
graphql(schema, query).then((result) => { | |
// do something with the result here | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let RootQuery = new GraphQLObjectType({ | |
name: 'Query', | |
fields: … | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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