Installation
npm install graphql --save
Vanilla Template
const { graphql, buildSchema } = require("graphql")
// Construct a schema, using GraphQL schema language
const schema = buildSchema(`
type Query {
hello: String
}
`)
// The rootValue provides a resolver function for each API endpoint
const 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!"
}
}
Express Server Template
const express = require("express");
const { createHandler } = require("graphql-http/lib/use/express");
const { buildSchema } = require("graphql");
// Construct a schema, using GraphQL schema language
const schema = buildSchema(`
type Query {
hello: String
}
`)
// The root provides a resolver function for each API endpoint
const root = {
hello() {
return "Hello world!"
},
}
const 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")