What’s GraphQL? – Where we can start to learn it?


If you’re Software Engineer, you can’t stop learning, this week is GraphQL. This is truly thing we need to accept. So just enjoy to keep going. Let’s go!

With GraphQL we can get many resources in a single request. Nguồn/ Source:

1. Definition

Before understand the definition about GraphQL, I want to tell you that some point below is not correct.

GraphQL contains QL (Query language), but it’s not a Graph Database, that’s first mistake. Second, Graph QL is not a library, so please remember it.

  • GraphQL is not some sort of graph database
  • It is not a library
  • It is not about graph theory

So here is definition about Graph QL, from the book I readed about this technology and history of it.

Graph QL is a specification for an API query language and a server engine capable of executing such queries.

Take a note that Graph QL like a API query language and we can do queries with it.
Putting a GraphQL server in front of a database will help create minimal architecture. Like a man in the middle.
Nguồn/ Source:

At this point, this may be a bit too vague to truly grasp what GraphQL is all about, but don’t worry, let’s start with an example.

Of course, we are Developer, so take a look in a example always easier.

2. Examle for GraphQL

2.1 Example

This is the “hello world” of GraphQL. A query that asks for the current user, as well as their name. In this query, me and name are referred to as fields.

query { 
	me { 

This query writes to get name from the user. When we do this request to GraphQL server, usually as a simple string. Here’s what the response coming back from a Graph Query Language server would look like in this case:

	"data": { 
    	"me": { 
        	"name": "Kieblog" 

Please remember that the response get from Graph QL always has “data” field key. Inside the data is response data we want to get. It allows clients to define requirements down to single fields, which allows them to fetch exactly what they need. We can do more than fetching simple fields:

query { 
	me { 
        friends(first: 2) { 
        	name age 

This query above do more than one thing. The first, we want to get a name of user also can get next two friends (include name and age).

2.2 Types & Fields

Practically, the type system of a GraphQL engine is often referred to as the schema. A common way of representing a schema is through the Graph Query Language Schema Definition Language (SDL).

Let me show you a example about it. The SDL is such a great tool to express GraphQL schema that we will be using it during the entire book to describe schema examples.

type Location { 
	address: String 
type Product { 
	name: String! price: Price! 

In this case I show you below, if we want to know what fields are available on a Location type, we must look at the Location type definition:

type Location { 
	address: String! 

The location have only one field, in this case it’s address field (return a String).

Let add the Location type to Museum type (the museum always has Location). So the Types look like this.

type Museum { 
	name: String! # Where the shop is located, null if online only. 	location: Location 

The fact that fields can return object types of their own is what powers amazing queries like these:

query { 
	museum(id: 1) { 
    	location { 

3. Reference

Thank for reading my post. Please like page if you think it’s helpful. Happy coding!

Có gì thắc mắc cứ comment đây nha! - Please feel free to comment here!

Kiên Nguyễn

👋 HEY THERE! 👋. My pleasure when you're here to read the article. Please feel free to comment, send a message or just want to say HELLO. Thank you, from bottom of my heart ❤️❤️❤️. Visit my portfolio:

Mời tui ly cà phê

Like page để không bỏ lỡ bài viết mới
Facebook Pagelike Widget
Bài viết mới
Lưu trữ