Getting started with graphql – Server Installation & Implementation

GraphQL.js

GraphQL.js is a JavaScript reference implementation for GraphQL. You can install it via npm:

  • Initialize npm in your project if you have not done so already: npm init
  • Install GraphQL.js from npm: npm install --save graphql

Example Server

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) => {
  console.log(response);
});

Server Middleware Alternatives

if you want to reproduce, please indicate the source:
Getting started with graphql – Server Installation & Implementation - CodeDay