This is not exactly an educational post about "What
GraphQL " or
why it is so cool , or even about the
experience of using GraphQL in production . I just want to briefly state my opinion on what kind of technology it is and a possible
practical approach to its application.
So, GraphQL is a query language used by Facebook to extract data from graph DBMSs. The language was so successful that the potential scope of its application is much broader - it is called the “REST killer” and even
screwed it to the reactor as another engine for managing data models. Quite briefly what GraphQL is:
- The request is a list of fields that need to be returned in the response. Only fields that were requested are returned.
- the field may be a method with the same name - then the parameters of this method are indicated directly in the request: {name, surname, age, getLikesCount (since: "01/01/2016")}
- if the value of a field or method is an object, then it is also necessary to explicitly specify a list of fields for it: {name, surname, age, bestFriend: {name}}
There are many different opinions about what is innovative in it, but I think the most interesting idea is this:
')
The data model is a special case of the API model.
And in fact, if in an arbitrary json we replace the fields with methods with an empty parameter list, then we get some kind of trimmed API:
{ name: "John", age: 25 friends: [{ name: "Jenny", age: 24 }] }
turns into
interface Human { name(): string age(): int friends: Human[] }
This example demonstrates an important consequence - if the API and data are one and the same, the API can return links to other APIs, i.e. on objects that provide methods that return data (or other APIs). The important point is that the methods are not obliged to be idempotent at all, it may well be update / delete or just a call to some business logic.
Nothing like? For example, a graph of objects in a running program? Taking into account the fact that the GraphQL query, in essence, is a list of methods on the root object to be called, as well as other features:
- strict typification
- interface support
- documented presentation of the schema in the form of a data structure
we get that GraphQL allows you to put an arbitrary object on the network! And, in particular, an active domain model, self-written or compiled using Hibernate ORM
Many people do not like the GraphQL redundancy, its set of chips and gadgets, which it has acquired during its use on Facebook. Many of them make sense only in the context of node.js and a specific development style. But - if we have a scheme and we said RPC, then the obvious solution is code generation. If verbose queries with all these `query`,` mutation` and variable declarations are hidden behind the API, then this flaw is leveled.
In total, the output is an RPC framework, which allows a typed, documented,
object API to be exposed to the network, using json as a transport and a primitive (the parser is written with half a speckle) GraphQL subset. As well as a client code generator, providing a convenient interface for calling.
It remains to write it :)
Thank.