Skip to content

Instantly share code, notes, and snippets.

@guncha
Created March 23, 2016 16:39
Show Gist options
  • Save guncha/ea3d0e04ceeea65dd317 to your computer and use it in GitHub Desktop.
Save guncha/ea3d0e04ceeea65dd317 to your computer and use it in GitHub Desktop.
GraphQL schema example using CoffeeScript
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 }")
@anaslaham
Copy link

What if a want the query to be template string in order to pass the args dynamically?

@guncha
Copy link
Author

guncha commented Sep 15, 2022

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment