In this section, we will walk through the steps to set up a basic GraphQL server. By the end of this lesson, you will have a working GraphQL server that you can query and extend as you learn more about GraphQL.

Prerequisites

Before we start, ensure you have the following installed:

  • Node.js (v12 or higher)
  • npm (Node Package Manager)

Step 1: Initialize a Node.js Project

First, create a new directory for your project and navigate into it:

mkdir graphql-server
cd graphql-server

Initialize a new Node.js project:

npm init -y

This command will create a package.json file with default settings.

Step 2: Install Required Packages

We need to install the following packages:

  • express: A web framework for Node.js.
  • express-graphql: A middleware to connect Express and GraphQL.
  • graphql: The core GraphQL library.

Run the following command to install these packages:

npm install express express-graphql graphql

Step 3: Create the Server

Create a new file named server.js in the root of your project directory. This file will contain the code to set up and run your GraphQL server.

Basic Server Setup

First, let's set up a basic Express server:

const express = require('express');
const { graphqlHTTP } = require('express-graphql');
const { buildSchema } = require('graphql');

const app = express();
const port = 4000;

// Define a simple schema
const schema = buildSchema(`
  type Query {
    hello: String
  }
`);

// Define a resolver for the schema
const root = {
  hello: () => {
    return 'Hello, world!';
  },
};

// Set up the GraphQL endpoint
app.use('/graphql', graphqlHTTP({
  schema: schema,
  rootValue: root,
  graphiql: true, // Enable GraphiQL interface
}));

app.listen(port, () => {
  console.log(`GraphQL server running at http://localhost:${port}/graphql`);
});

Explanation

  • Express Setup: We create an Express application and set it to listen on port 4000.
  • GraphQL Schema: We define a simple schema with a single query hello that returns a string.
  • Resolver: We define a resolver for the hello query that returns the string "Hello, world!".
  • GraphQL Endpoint: We set up the /graphql endpoint using express-graphql middleware and enable the GraphiQL interface for testing.

Step 4: Run the Server

To start the server, run the following command in your terminal:

node server.js

You should see the following output:

GraphQL server running at http://localhost:4000/graphql

Step 5: Test the Server

Open your browser and navigate to http://localhost:4000/graphql. You should see the GraphiQL interface. In the left pane, enter the following query:

{
  hello
}

Click the "Execute Query" button (the play button). You should see the following response in the right pane:

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

Summary

In this lesson, we set up a basic GraphQL server using Node.js, Express, and the express-graphql middleware. We defined a simple schema and resolver, and tested our server using the GraphiQL interface. This setup provides a foundation that you can build upon as you learn more about GraphQL.

Next, we will dive into the core concepts of GraphQL, starting with queries.

© Copyright 2024. All rights reserved