5.5 GraphQL as a ‘Fetching Tree’

While the name GraphQL may imply that this API structure optimized for graph databases, this is not actually the case. GraphQL allows server-side developers to model the resources and processes provided by their servers as a Domain Specific Language. Thus, server-side developers can create a querying language that maps specifically to the structure of their database, and force client-side developers to conform to their naming conventions and structure in order to formulate their queries, yet simultaneously allowing client-side developers to execute more efficient queries.

The best way to understand how a GraphQL query is processed is by abstracting away what queries generally look like. Typically, client-side applications are designed in the form of discrete pages, which are seeded with some tiny bit of data, and then perform a cascade of fetches to get the data needed to provide a unique user experience to the end-user. As an example, let’s assume that, on a client-sid web application, a profile page is ‘seeded’ with a user-id, and from that information the web application can reach out to multiple endpoints across the backend server architecture to grab the information necessary to populate the rest of the page. The fundamental insight in the development of GraphQL is that, in most cases, this contingent data fetching forms a tree that is more or less fixed for a given page. Data from early responses contain the keys for subsequent requests (such the address of my profile image in the REST example provided earlier), and the linkages between these requests are usually straightforward. As such, if the client can factor all of these disparate fetches into one spot, encode them as one large fetching tree, and send that fetching tree to the server, the client can receive all of the user’s data in request, eliminating the multiple roundtrips that are often needed with REST APIs, and consequently saving significant bandwidth and latency. Now that we have established this abstraction of a query as a fetching tree, let us examine how the fetching tree defined within GraphQL.

Last updated