GraphQL as a universal RPC

    This is not quite an educational post about “What is 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 this 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 turned out to be so successful that the potential scope of its application is much wider - it is called the “REST killer” and even screwed it to the reaction as the next data model management engine. In a nutshell about what GraphQL is:

    - the request is a list of fields that need to be returned in the response. Only those fields that were requested
    are returned - the field can turn out to be a method of 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 the field or method Is an object, then for it you also need to explicitly specify a list of fields: {name, surname, age, bestFriend: {name}}

    There are many different opinions about what exactly is so innovative in it, but, I think, the most interesting idea here's the thing:

    The data model is a special case of the API model.


    And in fact, if in arbitrary json we replace the fields with methods with an empty list of parameters, we get some sort of truncated 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 the data are one and the same, then the API can return links to other APIs, i.e. to objects that provide methods that return data (or other APIs). An important point is that methods do not have to be idempotent at all, it may well be update / delete or just a call to some business logic.

    Doesn’t resemble anything? For example, a graph of objects in a running program? Given that the GraphQL query, in fact, is a list of methods on the root object that you need to call, as well as other features:

    - strict typing
    - support for interfaces
    - documented representation of the schema as a data structure

    we get that GraphQL allows you to expose an arbitrary object to the network! And, in particular, an active domain model, self-written or assembled using Hibernate-type ORMs.

    Many do not like GraphQL redundancy, its set of chips and lotions, which it has grown over the time it was used on Facebook. Many of them make sense only in the context of node.js and a specific development style. But - if we have a circuit and we said RPC, then the obvious solution is code generation. If verbose queries with all this `query`,` mutation` and variable declarations are hidden behind the API, then this flaw is leveled.

    In total, the output is an RPC framework that allows you to set up a typed, documented, object-based networkAn API that uses json as a transport and a primitive (parser is written with a half-spit) subset of GraphQL. As well as a client code generator that provides a convenient interface for calling.

    It remains to write it :)

    Thank you.

    Also popular now: