Open In App

Installation in GraphQL

Last Updated : 30 May, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

GraphQL is a powerful query language that enables clients to request the data they need from an API. Setting up a GraphQL server involves installing the necessary dependencies and configuring the server to handle GraphQL queries. In this article, we will go through the steps to install and set up a basic GraphQL server using Node.js and Express.js.

Prerequisites

Before we start with the installation of GraphQL, ensure you have the following prerequisites:

  • Node. js and npm: To clarify, GraphQL itself runs on node.js, so you need to have Node.js and npm which is Node Package Manager installed on your computer. You can collect them from the Node js official website.
  • Basic Knowledge of JavaScript: Understanding JavaScript and its operations is important because all your GraphQL server and the queries using the APIs will be written in JavaScript.

Installation in GraphQL

  • GraphQL is versatile and can be installed and set up in several methods besides the server framework.
  • In this section we will use Express.js, a minimal and flexible Node.js web application framework along with Express-GraphQL, a library that helps integrate GraphQL with Express.

Step 1: Install Node. js and npm

First proceed to download the Node package and confirm the installation by running:

node -v
npm -v

Output:

Install-Node-js-and-npm

Step 2: Initialize a Node. js project

Form a new working folder for this project and move into it. Next, create a new node and perform the following operations on that Node.js project:

mkdir graphql-server
cd graphql-server
npm init -y

Output:

Initialize-a-Node-js-project

Step 3: Install dependencies

Install Express and its associated extension Express-GraphQL in addition to the GraphQL package itself:

npm install express express-graphql graphql

Example of Installation in GraphQL

Let's Set up a server:

Create an index.js file and set up a basic Express server integrated with GraphQL.

const express = require('express');
const { graphqlHTTP } = require('express-graphql');
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();
app.use('/graphql', graphqlHTTP({
schema: schema,
rootValue: root,
graphiql: true,
}));

app.listen(4000, () => console.log('Now browse to localhost:4000/graphql'));

Explanation:

  • Express Server Setup: An Express server is configured to use the GraphQL middleware at the /graphql endpoint.
  • GraphQL Schema Definition: A GraphQL schema is defined with a single query type hello that returns a string.
  • Resolver Function: A resolver function for the hello query is provided, which returns the string "Hello world!".
  • GraphiQL Interface: The GraphiQL interface is enabled to test GraphQL queries easily.
  • Server Listening: The server listens on port 4000 and we can access the GraphiQL interface by navigating to http://localhost:4000/graphql.

2. Now Run the server

Start your server by running:

node index.js

3. Access GraphiQL

Open your browser and navigate to http://localhost:4000/graphql. You should see the GraphiQL interface, which is an in-browser tool for writing, validating and testing GraphQL queries.


Output:

Access-GraphiQL

4. Let's Test the GraphQL query

In the GraphiQL interface, enter the following query:

{
hello
}

Output:

Test-your-GraphQL-query

Explanation: Click on the "Execute Query" button (the play button) and we should see the following output:

Conclusion

In conclusion, GraphQL is a powerful tool for building efficient APIs that allow clients to request only the data they need. Setting up a GraphQL server involves installing the necessary dependencies and configuring the server to handle GraphQL queries. In this article, we have go through the steps to install and set up a basic GraphQL server using Node.js and Express.js. By following these steps, you can start building your GraphQL API and using its flexibility and efficiency for data fetching.


Next Article
Article Tags :

Similar Reads