Creating A GraphQL Server With Node.js And Express

GraphQL is a language that enables you to provide a complete and understandable description of the data in your API. Furthermore it gives clients the power to ask for exactly what they need and nothing more.

Creating A Basic GraphQL Server With Express

let’s create the a first server implementation by inserting the following JS code in server.js:

var express = require(‘express’);

var express_graphql = require(‘express-graphql’);

var { buildSchema } = require(‘graphql’);

// GraphQL schema

var schema = buildSchema(`

    type Query {

        message: String

    }

`);

// Root resolver

var root = {

    message: () => ‘Hello World!’

};

// Create an express server and a GraphQL endpoint

var app = express();

app.use(‘/graphql’, express_graphql({

    schema: schema,

    rootValue: root,

    graphiql: true

}));

app.listen(4000, () => console.log(‘Express GraphQL Server Now Running On localhost:4000/graphql’));

At first we’re making sure that express, express-graphql and the buildSchema function from the graphql package are imported. Next we’re creating a simple GraphQL schema by using the buildSchema function.

To create the schema we’re calling the function and passing in a string that contains the IDL (GraphQL Interface Definition Language) code which is used to describe the schema. A GraphQL schema is used to describe the complete APIs type system. It includes the complete set of data and defines how a client can access that data. Each time the client makes an API call, the call is validated against the schema. Only if the validation is successful the action is executed. Otherwise an error is returned.

Next a root resolver is created. A resolver contains the mapping of actions to functions. In our example from above the root resolver contains only one action: message. To keep things easy the assigned functions just return the string Hello World!. Later on you’ll learn how to include multiple actions and assign different resolver functions.

Finally the Express server is created with a GraphQL endpoint: /graphql. To create the GraphQL endpoint first a new express instance is stored in the app. Next the app.use method is called and two parameters are provided:

First the URL endpoint as string

Second, the result of the express_graphql function is handed over. A configuration object is passed into the call of express_graphql containing three properties

The three configuration properties which are used for the Express GraphQL middleware are the following:

schema: The GraphQL schema which should be attached to the specific endpoint

rootValue: The root resolver object

graphiql: Must be set to true to enable the GraphiQL tool when accessing the endpoint in the browser. GraphiQL is a graphical interactive in-browser GraphQL IDE. By using this tool you can directly write your queries in the browser and try out the endpoint.

Finally app.listen is called to start the server process on port 4000.

In the query editor type in the following code:

{

    message

}

Next hit the Execute Query button and you should be able to see the following result:

{

“data”:{

“message”:”Hello World!”

}

Leave a comment

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