Created
March 23, 2016 16:39
-
-
Save guncha/ea3d0e04ceeea65dd317 to your computer and use it in GitHub Desktop.
GraphQL schema example using CoffeeScript
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 = require("graphql") | |
GraphQLRelay = require("graphql-relay") | |
GraphQLUtilities = require("graphql/utilities") | |
__GREETING = "Hello World!" | |
Root = new GraphQL.GraphQLObjectType | |
name: "Root" | |
fields: | |
hello: | |
type: GraphQL.GraphQLString | |
resolve: -> __GREETING | |
Mutation = new GraphQL.GraphQLObjectType | |
name: "Mutation" | |
fields: | |
set_greeting: GraphQLRelay.mutationWithClientMutationId | |
name: "SetGreeting" | |
inputFields: | |
greeting: | |
type: new GraphQL.GraphQLNonNull(GraphQL.GraphQLString) | |
outputFields: | |
what_we_thought_of_your_mutation: | |
type: GraphQL.GraphQLString | |
resolve: ({thought}) -> "It was #{thought}!" | |
mutateAndGetPayload: ({greeting}) -> | |
__GREETING = greeting | |
return {thought: "horrendous"} | |
schema = new GraphQL.GraphQLSchema | |
query: Root | |
mutation: Mutation | |
# console.log("Schema:\n\n" + GraphQLUtilities.printSchema(schema)) | |
fetchQuery = (query) -> | |
GraphQL.graphql(schema, query).then (result) -> | |
console.log("Query result:\n\n" + JSON.stringify(result, null, 2)) | |
# query = require("fs").readFileSync("schema/introspection_query.graphql") | |
query = """ | |
mutation { | |
set_greeting(input: {clientMutationId: "123", greeting: "Hola!"}) { | |
clientMutationId | |
what_we_thought_of_your_mutation | |
} | |
} | |
""" | |
fetchQuery("{ hello }").then -> | |
fetchQuery(query).then -> | |
fetchQuery("{ hello }") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is pretty old so I'm not sure about the exact API, but there certainly is a way to do that. I'd dig through the
graphql
package documentation.