Build a GraphQL Server CRUD API in Node.js and Express Using Express-GraphQL Library Full Project For Beginners

You are currently viewing Build a GraphQL Server CRUD API in Node.js and Express Using Express-GraphQL Library Full Project For Beginners




Welcome folks today in this blog post we will be making a graphql server in which we will handling crud operations in the form of api. We will be using node.js and express for this purpose. All the source code of the project will be given below.




Get Started




In order to get started we need to install the following dependencies


First of all we need to initialize the empty node.js project by executing the following commands


npm init -y


After that we need to install the following dependencies


npm i express


npm i nodemon


npm i graphql


npm i express-graphql


So here we are installing first of all express library for constructing our http server inside the node.js application

And after that we will be installing nodemon library to automatically restart the application for us.

And then we are installing the graphql library for making the graphql api

And now we are installing the express-graphql library for using graphql with express.

After installing all these dependencies you will find the package.json file and all the dependencies will be contained inside the file.

Now you need to make a index.js file and copy paste the following code







Here you can see we have imported the express library and also express-graphql. Here we have initialized a simple express app at port 5000. And also we have initialized a special middleware function to the route /graphql and we have passed a option graphiql to true.


And now if you execute the application you will see a error of schema like this





As you can see graphql middleware must contain a schema. Schema is actually the data which the graphql server will be returning when you make a request to it.


Now we need to define our data inside our application or schema to be precise.




So you can see that we have two arrays namely websites and owners. For Simplicity we are taking static arrays you can also take database as well.


And now we define the fields which are there inside the arrays first we will tackle website array as you can see there are three fields out there id and name and ownerID

Inside the index.js file copy paste the following code





So here in the above code we have defined a variable called as WebsiteType which stores the actual object type of Website Arrays and here we are using name and description, and fields. Inside fields we have defined id, name and ownerID


For this we need to import GraphQLINT , GraphQLString and GraphQLNonNull from the graphql library as shown below.




Similarly we will define Owners Array Object type called as OwnerType as shown below





Now we will define the queries that we will make to the GraphQL Server as shown below




So these two queries will return the list of websites and owners


Now we will be importing the GraphQLList from graphql




So now lastly we will define the schema inside our application like this




So we will import the GraphQLSchema from graphql




Now if you run the node application by running the following command


node index.js


And now if you go to this route localhost:5000/graphql





So you can see we have executed the below query inside the graphql server




So you can also tweak this query by adding more fields like this to this query







As we query owners you will see the following image




Now we will query a single website and single owner like this




Now if you execute the app and now to retrieve a single website or owner







Mutations in GraphQL Server



Now guys we will look at the very important concept of mutations which is  crud operations which is creation, deletion and updation


For this you need to add this code





So now after this we need to add a special attribute called as mutation inside schema variable





Now if you execute this query to add a owner like this







Now if you check the no of owners like this





Now in order to update the website we will write the query like this







Now to Delete the website we will write the query












Leave a Reply