The user details are returned as a JSON file, like this:
Now, that's a lot of information returned by the web service. You only need specific information, such as the username, the name and surname. Can you only get this specific information?
This can only happen if the developer of the API has specifically added functionality to retrieve specific information and that would be weird and unintuitive with REST-ful services.
Basically in most cases, you're stuck with all this data which most will end up being junk.
What if there was a more explicit way of saying, "I need this information and I don't need anything else?"
ENTER, GRAPHQL!
GraphQL is basically a way of saying something like this:
"I need to get data about a specific user. I only need to get their username, their first name and last name. Nothing less, nothing more."
GraphQL will do it. It will give you exactly what you need.
This URL will be the same for the entire API, whether we need user data, or anything else.
Next we need our GraphQL statement. The statement to retrieve the username, name and surname from a user with ID 101 would look something like this:
GraphQL will give you back exactly what you asked for in the exact format you requested it:
So not only did it give you back only what you asked for, the response is the exact same format as the request, as if you gave it some blanks and the API filled them in! Isn't that amazing? No more wasted data!
What's next?
I hope this simple explanation helped you understand the basics of GraphQL and why it has become so popular.
To get started with GraphQL, TheNetNinja has an amazing free course on YouTube on how to build your first GraphQL service with Node and React. Enjoy!
• • •
Missing some Tweet in this thread? You can try to
force a refresh
If you're learning Javascript, sooner or later you'll come across the forEach() and map() functions. It is quite often that there is some confusion about what each one does.
If you're learning Javascript, sooner or later you'll come across the forEach() and map() functions. It is quite often that there is some confusion about what each one does.
🔌Before I begin: This thread is also available in a more convenient and more accessible format at my new project "Let's Explain" (where you'll find all my other "let's explain" threads as well):