GraphQL: Getting Started

To create a new project and install GraphQL.js in your current directory:

npm init
npm install graphql --save

Writing Code

 For an API that just returns “Hello world!”, we can put this code in a file named server.js:

var { graphql, 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!';

// Run the GraphQL query '{ hello }' and print out the response
graphql(schema, '{ hello }', root).then((response) => {

If you run this with:

node server.js

You should see the GraphQL response printed out:

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

Leave a Reply

Your email address will not be published. Required fields are marked *