DEV Community

Cover image for Creating an App with React, Apollo, and GraphQL: A Step-by-Step Guide to Fetching Data and Error Handling

Creating an App with React, Apollo, and GraphQL: A Step-by-Step Guide to Fetching Data and Error Handling

Beatrice Egumandi on February 18, 2023

Are you looking to build a modern web application with scalable, data-driven user interfaces? Then this article is for you. In this step-by-step gu...
Collapse
 
dev_geos profile image
Dev Geos

For django backend, please tell me what is the advantages for using graphql instead of his Orm ?

Collapse
 
joelbonetr profile image
JoelBonetR šŸ„‡ • Edited

Hi,

ORM

Or Object-Relational-Mapping is -usually [0]- meant to mimic DB models as a given programming language classes while providing methods to interact with the forementioned database on a straightforward and secure manner.

In other words, an ORM it's a piece of software (sometimes including a CLI) that helps you interact with a database. You can check SQLAlchemy (Python) or Sequelize (Node) as examples.

GraphQL

in the other hand, is a SDL (Schema Definition Language), it is meant to be a declarative way to interchange information between clients and servers, or between different servers; It falls into the Service layer.
It has some benefits in comparison with RESTful, like being able to define and ask explicitly for different fields from different models on a single request [1]. For this reason it's most of the time used as interface for clients, that sits in the gateway (then the gateway can resolve each requested data piece through API calls to specific microservices).

It has it's drawbacks too, like anything in this life, like being harder to manage the cache on a GraphQL service than in a REST one, though most of the drawbacks have been addressed through the years on a way or another and I'd currently can't find a reason not to provide a GraphQL API unless your use-case doesn't really require it (as always, analysis should be done before taking a stack decision).

Best regards


[0] Usually

There are some exceptions like HaskellDB, which can be considered an ORM as a concept but it uses a FP (Functional Programming) approach.

[1] Example:

You are coding an e-commerce, API-first using microservices architecture, one use-case can be to list the items of the last order of a group of clients to recommend some of those to the user in context.

RESTful scenario:

1- Request users.
2- You got full users (bigger payload) but you just needed the IDs.
3- Use this IDs to call (one time for each user) to a different endpoint to get the last Order of each.
4- Again, you just needed the ID of each order, instead you get the entire Order object, for each user.
5- Call to another endpoint to get the product details on each accumulated orderID to retrieve the product data.
6- Finally use the product data to do whatever you needed to do in the first place.

As you can see, those are a bunch of calls, a lot of unnecessary network data transfer and useless payloads.

GraphQL scenario:

1- Define in the Schema which information you need in this given request.
2- Do the request.
3- Use the data to do whatever you need to do.

Foot Notes

GraphQL and ORM are completely different things, not exclusive between each other.
You will probably keep using an ORM to interact with your DB in a GraphQL service, while GraphQL will be the API exposed to the clients (or other services) to interact with.

Collapse
 
icyy_bee profile image
Beatrice Egumandi

Thank you for your contribution!!

Thread Thread
 
joelbonetr profile image
JoelBonetR šŸ„‡

Anytime šŸ˜

Collapse
 
icyy_bee profile image
Beatrice Egumandi • Edited

Thanks for your comment! I'm sorry that I'm not into backend development, so I won't be of much help to you in this case. However, I would recommend that you do your own research to find the best solution.

Collapse
 
yeezywest profile image
Yusuf olatunji adeshina

nicee!!!!!!!!!!!!!!! thank you for the step by step guidešŸ™ŒšŸ™ŒšŸš€šŸš€šŸ˜ŽšŸ˜Ž

Collapse
 
icyy_bee profile image
Beatrice Egumandi

Youā€™re welcome

Collapse
 
thekelvinperez profile image
TheKelvinPerez

Amazing Beatrice Thank you for sharing ā¤ļø

Collapse
 
icyy_bee profile image
Beatrice Egumandi

I appreciate šŸ˜

Collapse
 
vulcanwm profile image
Medea

great work!

Collapse
 
icyy_bee profile image
Beatrice Egumandi

Thank you!

Collapse
 
fimber01 profile image
Fimber Elemuwa

Great work!