Why Open Chain Access Protocol Adopted GraphQL#
Author: Jean Chen (VP of PR, ArcBlock)
ArcBlock’s engineering team will regularly take interviews or write engineering blogs to "decrypt" the design ideas and implementation details behind Open Chain Access Protocol and broaden public understanding. We welcome suggestions to improve our design and products further.
The second article, "Why Open Chain Access Protocol Adopted GraphQL" was written by the ArcBlock marketing team through a series of interviews with the engineering team. For the first time, we’re diving into the reasons why ArcBlock adopted the new generation of query language, GraphQL.
Despite the current downturn in the global cryptocurrency market, this year blockchain technology entered the 3.0 era of the underlying public chain project. Bitcoin and Ethereum, as representatives of the 1.0 and 2.0 eras, have been plagued by low performance, unfriendly design, lack of function, high cost, platform "lock-up" and other pain points, but endless new public chains put forward a variety of technical solutions.
Other public chains are promoting the popularization of blockchain technology, responding to the needs of consumers, chiefly the development and success of decentralized applications (Dapps), but here’s what makes ArcBlock different: it is not a new public chain, but an application development platform. ArcBlock uses cloud technology to help developers access and connect various public chains, allowing them to focus on developing Dapps, making the design of blockchain applications as simple as that of web pages and mobile apps.
Open Chain Access Protocol will be a solid foundation for building such a cloud computing platform, the equivalent of J2EE or Microsoft's.net framework. The open source protocol provides an abstract interface layer that accesses the underlying blockchain to help develop applications that work on different blockchains. Similar to the relationship between ODBC and JDBC in data applications, when Dapps switch between different underlying blockchains or use multiple blockchains with different protocols, you don't even need to change your business logic code.
In order to help developers easily access connections and switch to the underlying public chains, rather than use RESTful as an API query language, which has been widely adopted by the industry, ArcBlock uses GraphQL.
What is GraphQL?#
GraphQL is a new generation of API query languages that Facebook started using internally in 2012 and open source in 2015. On GraphQL's official website, they demonstrate its main features with code.
API developers only need to describe the scheme of the API and provide it with a suitable resolver. The client can efficiently use GraphQL query format to query the data, and the server will return the corresponding result according to the client's needs.
GraphQL supports Query (data query), Mutation (data update), and Subscript (data listening) APIs. Each API will return the client's desired result through the resolver, as shown in the following figure:
The graph in GraphQL is a directed graph, and each layer of a query corresponds to a resolver, so the resolves come down layer by layer to get all the data the user needs:
Compared to RESTful, GraphQL is more efficient, powerful, and flexible. It allows you to define the structure of the data to be queried on the client, and returns the same structure from the server instead of returning redundant data.
When sending a GraphQL query to your API, you can get exactly what you need, nothing more and nothing less. GraphQL queries always return predictable results. Apps using GraphQL are fast and stable because they, and not the server, control the data they get.
GraphQL queries access not just the properties of one resource, but also smoothly follow references between them. While typical RESTful APIs require loading from multiple URLs, GraphQL APIs get all the data your app needs in a single request. Apps using GraphQL can even operate quickly on slow mobile network connections.
GraphQL APIs are organized in terms of types and fields, not endpoints. GraphQL uses types to ensure apps only ask for what’s possible and provide clear and helpful errors. Apps can use types to avoid writing manual parsing code. You can access the full capabilities of your data from a single endpoint.
To recap: GraphQL provides a complete and understandable description of the data in your API, and gives clients the ability to
- Ask for exactly what they need -- nothing more.
- Easily evolve APIs over time.
- Enable powerful developer tools.
Q & A#
More than a month after the launch of OCAP, Robert Mao (founder and chief architect of ArcBlock), Tyr Chen (VP of Engineering), and Peiling Ding (Lead Developer of OCAP project), responded from different perspectives to questions on OCAP technology.
Why did we choose to use GraphQL in OCAP?#
Robert Mao: From a developer's perspective, GraphQL is simple, intuitive, easy to learn, and easy to debug. Its cross-platform application is very convenient, and it is suitable for front-end development, its complexity hidden and encapsulated in the back-end server, perfect for application development. Considering that ArcBlock is a platform to support blockchain applications, OCAP needs to support a variety of different underlying blockchains, and the current block chains do not have a unified standard and architecture, so we need a language that can describe flexible queries and data structures. The design of GraphQL is very suitable for this scenario. Using GraphQL helps to unify the connection and integration between blockchains, traditional databases, and web services. Finally, GraphQL has a mature community and a rich framework of ready-to-use tools.
Peiling Ding: ArcBlock has chosen to consider the developer’s experience, and wants to make it a pleasant one. GraphQL as the query language will make the front-end query more flexible and efficient. The most commonly used tactic in the industry is to provide a set of RESTful API. On the server side, we set up many endpoints, each of which accepts some parameters and provides some services. The apparent benefit of this is that it's easy to implement on the server side, and the average user can easily access it through a browser. However, the drawback is unfriendly design. In RESTful APIs, query initiation is based on the endpoints, which are loosely organized, making code maintenance on the client complicated and unfriendly to developers.
Tyr Chen: From an application development perspective, using GraphQL will reduce the pressure on client developers for data access. The in-depth data that the client wants to access, such as UTXO query requests for Bitcoin, use the RESTful API to return redundant data that you don't need, because you only need to query the transaction hash. Using GraphQL will significantly reduce the pressure on application developers to access data. It's like querying a database with SQL. When GraphQL queries blockchain data, initiates query behavior, and passes it to the server, it will obtain data from different data sources and construct a query feedback return, which can optimize network transmission data. The server returns whatever data the client needs.
How does GraphQL help OCAP achieve uniform and straightforward cross-chain operations?#
Tyr Chen: GraphQL has better implemented the definition of API in the three levels of OCAP development chain access layer. For example: in the first layer of the primary chain API, in the first version of OCAP, it is very convenient with GraphQL to query various data sets, such as Ethereum and Bitcoin; in the second layer of the primary chain API, GraphQL can differentiate the capabilities of different blockchains -- Bitcoin and Ethereum are queried separately, but their syntax structure and the way users write queries are similar. Developers do not need much instruction, and their ability to access blockchains can be easily transferred from one chain to another. ArcBlock has also created a developer-specific OCAP Playground to help you build queries more efficiently with automatic completion, significantly reducing memory costs, and quickly querying the corresponding results.
Robert Mao: OCAP makes it possible to support a variety of different blockchain protocols. Application developers can freely choose the most appropriate method from several different blockchain protocols, different node types, and different deployment modes. The open chain access layer defines a unified API, but the specific implementation of these APIs comes from the chain adapter.
Why are there not many current blockchain technology products using GraphQL?#
Tyr Chen: Although Facebook has been using for several years, GraphQL is still fairly new. Whether on the front or the back end, most developers are still familiar with the RESTful API. Many technology providers prefer to provide services through RESTful. However, I believe that there will be more and more people using GraphQL. Additionally, GraphQL has many thresholds on the server side.
Peiling Ding: If you develop an accessible, browser-based, linked-up data access service, you'll opt for the traditional RESTful API because you can't get people to learn GraphQL. ArcBlock is different from other companies regarding service objects. In the end, though, everyone will open the browser and type in the URL to get online.
How can ArcBlock make good use of GraphQL's existing resources and contribute to the GraphQL community?#
Robert Mao: Although Facebook has open-sourced GraphQL, it only discloses the framework language and does not provide specific implementation methods. Therefore, in the process of developing OCAP using GraphQL we have carried out a lot of exploration and innovation. We are not just "taking doctrine," but making contributions to fill the gaps in the blockchain field for the GraphQL community. Because of our technical skill, ArcBlock should be the first. We hope to start a movement, and drive community developers to use GraphQL to develop OCAP chain adapters and blocklets, building on our initial contributions. We are also happy to work with the GraphQL community to organize events such as hackathons for win-win cooperation.