How to Build a GraphQL server with NodeJS and Express Last Updated : 24 Jul, 2024 Comments Improve Suggest changes Like Article Like Report GraphQL is the open-supply question-based language that is used for querying the data. The main task of GraphQL is to execute the given question return the appropriate facts and represent it to the person. GrapghQL is the advancement of conventional REST API architecture presenting greater features. In this newsletter, we are able to see the ste[s to create the Simple GraphQL server. We can even execute the query and based on that query the result might be fetched. Prerequisites:VSCodeNodeJSExpressTable of Content Steps to create a GraphQL Server with Node & ExpressHow to test GraphQL Server?Steps to create a GraphQL Server with Node & ExpressStep 1: Create a New Directory by running the following command on vs code.mkdir simple-graphql-servercd simple-graphql-serverStep 2: Initialize the Project using the following command.npm init -yStep 3: Install the required dependencies for the projectnpm install express express-graphql graphqlFolder Structure:Folder StructureThe updated dependencies in package.json file will look like: "dependencies": { "express": "^4.18.2", "express-graphql": "^0.12.0", "graphql": "^15.8.0"}Example: Create GraphQL schema in schema.js file and setup it in app.js file. JavaScript // schema.js const { GraphQLObjectType, GraphQLString, GraphQLSchema } = require('graphql'); // Defining a simple GraphQL type for articles const ArticleType = new GraphQLObjectType({ name: 'Article', fields: { id: { type: GraphQLString }, title: { type: GraphQLString }, content: { type: GraphQLString }, }, }); // Defining the root query const RootQuery = new GraphQLObjectType({ name: 'RootQueryType', fields: { article: { type: ArticleType, args: { id: { type: GraphQLString } }, resolve(parent, args) { const articleData = { id: args.id, title: 'Introduction to GraphQL', content: 'This is a sample article about GraphQL on GeeksforGeeks.', }; return articleData; }, }, }, }); // Creating the GraphQL schema module.exports = new GraphQLSchema({ query: RootQuery, }); JavaScript // app.js const express = require('express'); const { graphqlHTTP } = require('express-graphql'); const schema = require('./schema'); const app = express(); // Setting up the GraphQL endpoint app.use( '/graphql', graphqlHTTP({ schema: schema, graphiql: true, }) ); const PORT = process.env.PORT || 3000; app.listen(PORT, () => { console.log(`Server is running on port ${PORT}`); }); Step to run the application: Run the Server using the following command.node server.jsOutput:How to test GraphQL Server?To test the server we will try the following query in the GraphiQL interfacequery { article(id: "1") { id title content }}This query has fetched the details for us which are specified in the schema. Output:Output Comment More infoAdvertise with us Next Article How to Build a GraphQL server with NodeJS and Express A anjalibo6rb0 Follow Improve Article Tags : Web Technologies Node.js Geeks Premier League Express.js Geeks Premier League 2023 +1 More Similar Reads How to Build a Simple Web Server with Node.js ? Node.js is an open-source and cross-platform runtime environment for executing JavaScript code outside a browser. You need to remember that NodeJS is not a framework, and itâs not a programming language. Node.js is mostly used in server-side programming. In this article, we will discuss how to make 3 min read How to use TypeScript to build Node.js API with Express ? TypeScript is a powerful version of JavaScript that incorporates static typing and other features, making it easy to build and maintain large applications. Combined with Node.js and Express, TypeScript can enhance your development experience by providing better type safety and tools. This guide will 4 min read Build a Simple Web App with Express & Angular Building a simple web app using Express and Angular is a great way to understand the fundamentals of full-stack development. Express, a minimalist web framework for Node.js, handles the backend, while Angular, a powerful front-end framework, provides the structure for the client-side application.In 5 min read How to Build a Node.js Proxy Server ? A proxy server acts as an intermediary between a client and other servers. It can be used for various purposes, including load balancing, security, and caching. In the context of web development, a proxy server forwards requests from clients to other servers, fetches responses, and sends them back t 4 min read How to Implement Search and Filtering in a REST API with Node.js and Express.js ? Search and filtering are very basic features that an API must possess to serve data to the client application efficiently. By handling these operations on the server-side, we can reduce the amount of processing that has to be done on the client application, thereby increasing its performance.In this 5 min read How to run ExpressJS server from browser ? ExpressJS is a minimal and flexible Node.js web application framework that provides a robust set of features for web and mobile applications. Typically, ExpressJS servers are run on a Node.js environment on the backend. However, for development, testing, or educational purposes, you might want to ru 2 min read How to Make a search function using Node Express and MYSQL In this article, we will learn how to create a search function using Node with Express framework to search terms inside MYSQL tables. Prerequisites:MySQLNode JSExpress JSWe will discuss the following methods to search: Table of Content Searching term with partial matchSearching term with exact match 5 min read How To Build Node.js Authentication System With MySQL? Node.js is an open-source server-side JavaScript runtime environment established to develop server-side applications. The first task can be an implementation of an authentication system, this is one of the most frequently used processes in web development. In this article, we are going to learn how 4 min read How to Run Node.js with Express on Mobile Devices ? Node.js is a cross-platform open-source Javascript runtime environment and library for running web applications outside the browser. It is based on Chrome's V8 engine. Node.js is used to create server-side and command-line applications. Express.js is one of the most popular node frameworks. It is a 2 min read Building GraphQL APIs with PostgreSQL GraphQL and PostgreSQL are powerful technologies that play important roles in modern web development. GraphQL a query language for APIs, revolutionizes how clients interact with servers by allowing them to request specific data. On the other hand, PostgreSQL, an advanced relational database manageme 6 min read Like