GraphQL, description for query
An alternative to SQL for accessing database content in a single query.
It is an interface to regular databases that allows you to read and edit content more intuitively. Its origins come from Facebook's need for faster mobile apps: you had to get member profiles immediately and more directly.
This query language is particularly suitable when a client, usually a mobile or web application, wants to access a dataset one query at a time while the database is constantly changing. We describe the request in the form of a plan, and in response we receive the content corresponding to each section of the plan ...
In practice, the query is similar to a JavaScript object that specifies only attribute names. The goal is to get the value of these attributes. It has a hierarchical structure: for a post we have a set of data, and for each, for example, the author, other data like a name and an avatar .
Example of a query:
{
post {
title,
content,
date,
author,
comments {
author {
name,
avatar
},
content
}
}
}
The SQL equivalent requires access to multiple tables - records, comments, and registered members. Therefore, several motions will be required.
We get the following answer
{
"data" {
"post": {
"title": "Grande nouvelle",
"content": "Voilà ce qui s'est passé..."
"author": "Emma Kant",
"comments": [
{
"author": {
"name": "Joe",
"avatar": "x1"
},
"content": "Bien dit"
},
{
...etc...
}
]
}
}
}
Note that the response places the attributes in parentheses while the query does not apply. It is intended for software designed to interpret this type of structure, while the response is a JSON object that can be used by any software and programming language.
Use GraphQL
The implementation was performed in JavaScript, which is offered for download on GitHub, GraphQL.js. This is a module for Node.js that is installed with npm.
This is more of a framework than software ready to receive requests, it remains to implement your own request server, which is basic and when you are familiar with Node.js. The server provides access to data via HTTP, WebSocket or other protocol. Here is an example of a Node.js server.
The ease of query formulation is due to the complexity of the models that need to be defined to allow for this kind of query. The example of Star Wars given on the GraphQL website shows us enough: we very quickly want to return to SQL and its tables simply from rows and columns!
However, for databases with a static structure and frequent requests for information, this is the best solution .
The implementation of interfaces in different languages is a movement that is in full swing, with already implementations for languages used on the server, such as Java, Go, C #, JavaScript .
Links and tools:
- GraphicsQL. On GitHub, a graphical interactive interface to queries.