📜 ⬆️ ⬇️

Anatomy of GraphQL Queries

Gentleman's term set


A new data acquisition standard GraphQL has become popular recently. The development of this technology is being actively discussed, new tools are emerging. One of the advantages of GraphQL is that it gives the development team a universal language for describing what data is available in the API. But in what terms to discuss the query language itself and the technology as a whole?


The GraphQL specification contains an almost comprehensive set of terms in all aspects of GraphQL. But the specification is quite voluminous. In this article, we will learn the most important concepts and terms with concrete examples that are sufficient for discussing GraphQL at the expert level.


Note - This article is not the best choice for learning GraphQL from scratch. First, it is recommended that you read the materials on graphql.org , try GraphQL in an excellent course. Learn Apollo and then return to the article to immerse yourself in the language of technical terms.


Basic GraphQL Queries


Often a request is anything that goes to a GraphQL server. There is some confusion associated with this. What is the task for the server for which it is asked to perform? This can be a data query (query), mutation (mutation) or subscription (subscription). The word "request" is strongly associated with a network request in the understanding of HTTP and the transport layer. Therefore, we should start with a few general concepts:



What is the simplest operation? For example, take a very simple GraphQL document.


image
Fig. The parts of a simple query.


Here are the main GraphQL constructs that describe the requested data.



The above query is a somewhat abbreviated form of GraphQL, which allows expressing the need for data as concisely as possible. But the GraphQL operation may include three additional parts. They are used when you need to perform a different action than querying data, or send dynamically generated variables.


The following example shows all these parts.


image
Fig. More detailed request and its components.



Variables are transmitted separately from the text of the request in the format adopted at the transport level. Modern graphQL implementations typically use JSON. Here’s what an object with variables for a previously shown query might look like:


image
Fig. An example of an object with variables.


As you can see, the key coincides with the name specified in the description of the variables. The value is taken from the Episode enumeration type.



Another basic concept is not often mentioned, but it is important when discussing the technical aspects of GraphQL - what is it that is enclosed in curly braces?


image
The term selection set is constantly found in the GraphQL specification. It is associated with the recursive nature of GraphQL, in which nested queries are possible.



Fragments


Fragments bring even more features to GraphQL. However, coming new concepts.



image



Using fragments in operations


Fragments are not particularly useful outside operations. There are two ways to insert fragments, as shown below.


image
Fig. Two types of fragments in the request.



Directives


With the help of directives, additional functions of GraphQL server are implemented. Directives do not affect the resulting values, but can affect the composition of the returned data and, possibly, how they are obtained. Directives can be found almost anywhere in the query, but this article only mentions the skip and include directives that are defined in the current GraphQL specification.


image


Fig. The shown constructions are hardly found in one request, but it is convenient to demonstrate them at once in this form.


In the figure, unrelated examples of where skip and include directives can be used. They set the conditions of the GraphQL executable module to ignore fields and not include their values ​​in the result. The syntax of directives is open, and with their help various GraphQL implementations can add new features without complicating the parsing and execution of queries.



Join the discussion


An important advantage of GraphQL is the availability of a universal language with which you can describe data retrieval. You now have a gentlemanly set of terms for GraphQL discussion, for example in the ongoing discussion about GraphQL subscriptions .


In this article, we touched on only part of the GraphQL specification, which is devoted to the query language. In the following articles, we may consider the terms in which the GraphQL scheme is described.


Want to use GraphQL technology in your daily work? We have various vacancies on frontend, backend and open source!


Translation of the article . Original author Sashko Stubailo


')

Source: https://habr.com/ru/post/335050/


All Articles