GraphQL Tutorial Beginner – Learn GraphQL in NodeJS / ExpressJS
If you are interested in learning GraphQL, this tutorial is a great starting point for beginners. We will guide you through the necessary steps to set up GraphQL in a NodeJS/ExpressJS environment.
What is GraphQL?
GraphQL is a query language for APIs and a runtime for executing those queries with the existing data. It provides a more efficient and flexible alternative to traditional RESTful APIs. With GraphQL, you can precisely request the data you need, reducing unnecessary round-trips to the server.
Setting up NodeJS and ExpressJS
Before diving into GraphQL, you need to have NodeJS and ExpressJS installed on your computer. If you haven’t installed them yet, follow these steps:
- Visit the official NodeJS website (https://nodejs.org) and download the latest stable version for your operating system.
- Once the installation is complete, open your terminal or command prompt and run the command
node -v
to verify that NodeJS has been successfully installed. - Next, you need to install ExpressJS. Run the command
npm install express
to install Express globally on your system. - Use the command
express --version
to confirm that ExpressJS has been installed.
Install necessary packages
Now that you have NodeJS and ExpressJS set up, you need to install the required packages for GraphQL integration. Run the following command in your terminal:
npm install express-graphql graphql
Creating a GraphQL server
In your project folder, create a new file called server.js
. This will be the entry point for your GraphQL server.
Copy the following code into server.js
:
const express = require('express');
const { graphqlHTTP } = require('express-graphql');
const { buildSchema } = require('graphql');
// Construct a schema
const schema = buildSchema(`
type Query {
hello: String
}
`);
// The root provides a resolver function for each API endpoint
const root = {
hello: () => {
return 'Hello, GraphQL!';
},
};
const app = express();
app.use('/graphql', graphqlHTTP({
schema: schema,
rootValue: root,
graphiql: true,
}));
app.listen(4000);
console.log('GraphQL server running at http://localhost:4000/graphql');
Save the file and open your terminal. Navigate to the project folder and run the command:
node server.js
Congratulations! You just created your first GraphQL server using NodeJS and ExpressJS.
Testing the GraphQL API
Open your web browser and visit http://localhost:4000/graphql. You will see the GraphiQL interface, which allows you to interact with the GraphQL API.
On the left side, you will find a JSON editor where you can write GraphQL queries. Try executing the following query:
query {
hello
}
Click on the “Play” button or press Ctrl + Enter to execute the query. On the right side, you will see the response:
{
"data": {
"hello": "Hello, GraphQL!"
}
}
Congratulations! You have successfully executed your first GraphQL query.
Conclusion
In this tutorial, you learned how to set up GraphQL in a NodeJS/ExpressJS environment. You created a basic GraphQL server and tested it using the GraphiQL interface. Now you have a solid foundation to explore further and build more complex GraphQL APIs.
Continue experimenting with GraphQL and discover its powerful features for creating efficient and flexible APIs.
Great tutorial.All working except of reading json file. Getting "getAllUsers": null. I tried just to read json : const data = JSON.parse(userData) and log it to console and gettiing: undefined:1
[object Object],[object Object],[object Object],[
Unfortunately "graphql-express" package is deprecated.
Great video! Thank you bro
At what context i should use native graphql library as you have explained very well
over other library like Apollo client ?
I have been watching many tutorials, but the point was confused me! you taught graphQL with express like drinking a cup of tea, thank you body 😘
Amazing tutorial my friend! Brazillian teachers are always the best ones lol
Really useful, thank you
localhost:6969 (sus) 😳⚫ the way you teach is really amazing. Keep it up
Why can't everyone teach like this. Why?
Hats off man hats off. You explained it in a way that I finally understand.
Cant freaking believe i learn graphql in less that 30 mins. Thanks man
Only a man of culture would choose port 6969.
has been useful to me.
very good, videos, thanks for help some programmers
awesome teaching! ✌✌✌
Great tutorial!! Thank you Pedro
Good stuff. Any tips on other ways to store the user data?
thank you for this teaching❤
How or what would you need to do in regards to setting the port on a live api once it is hosted on a live server?
Solid primer, thx!
The best part of the video was that he ran the server on PORT 6969