How to retrieve data using Express GraphQL from mongoDB?

What is GraphQL?

GraphQL is a query language for API’s. It is used to simplify the API specific data processing in which it will fulfill the exact needs and nothing more as like REST API. Data retrieval structure and expectation is decided by client rather than the server. GraphQL isn’t tied to any specific database or storage engine and is instead backed by your existing code and data.

Why GraphQL?

REST API is popular way to consume data in which end point returns all the data which is not fulfilling the data requirement with respect to response data and size in some scenarios. GraphQL will work like query language to fetch the data available in database. Instead of database, GraphQL helps to query the API.

In this post, we are going to see how to retrieve data from mongoDB using express-graphql.

Pre-requisite to install the graphql dependencies;

>npm install express express-graphql graphql mongoose –save

Please refer the previous post to create an express based project in the same blog;

Create a graph.js based on the below code snippet;

How to declare and call the graphQL endpoint in the base-file of project;

Refer all the above code snippet in the below Git Repo;

https://github.com/gopekanna/express

Tried to retrieve the complete list of data based on the below query output;

Tried to retrieve the list of data specific to single parameter based on the below query output;

Thank you!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s