HomeToolsAbout a20k

Template

Installation

npm install graphql --save

Template to run as CLI

var { graphql, buildSchema } = require("graphql") // Construct a schema, using GraphQL schema language var schema = buildSchema(` type Query { hello: String } `) // The rootValue provides a resolver function for each API endpoint var rootValue = { hello() { return "Hello world!" } } // Run the GraphQL query '{ hello }' and print out the response graphql({ schema, source: "{ hello }", rootValue }).then(response => { console.log(response) })

Return Value

{ "data": { "hello": "Hello world!" } }

Template to run as server

var express = require("express") var { createHandler } = require("graphql-http/lib/use/express") var { buildSchema } = require("graphql") // Construct a schema, using GraphQL schema language var schema = buildSchema(` type Query { hello: String } `) // The root provides a resolver function for each API endpoint var root = { hello() { return "Hello world!" }, } var app = express() // Create and use the GraphQL handler. app.all( "/graphql", createHandler({ schema: schema, rootValue: root, }) ) // Start the server at port app.listen(4000) console.log("Running a GraphQL API server at http://localhost:4000/graphql")
© VincentVanKoh