flat2hier-graphql — Folds your joined sql rows into a hierarchy of objects suitable for graphql response

Akash Gupta
Theta One Software
Published in
4 min readJan 7, 2021
flat2hier-graphql

GraphQL is an open-source data query and manipulation language for APIs, and a runtime for fulfilling queries with existing data. GraphQL was developed internally by Facebook in 2012 before being publicly released in 2015. It provides an approach to developing web APIs and has been compared and contrasted with REST and other web service architectures. It allows clients to define the structure of the data required, and the same structure of the data is returned from the server, therefore preventing excessively large amounts of data from being returned, but this has implications for how effective web caching of query results can be.

A typical example

POST Request:

Response:

In a quest to retrieve database data in which is suitable for a graphql response we built a tool which helps us in achieving the aforementioned. flat2hier exposes a single function flat2hier(rows: Array<Object>, removeDollor: boolean): Array<Object> | Object

It takes an array of object and groups the objects by the follwing rules/convention and returns an array

Conventions

  • For each row it splits each keys by _ say level_keys
  • For each level key it adds an object or array for its value and if it is not the last level_keys
  • If a level_key starts with $ it adds an array, otherwise an object
  • If level_key starts with $ and it has a child level key id it uses that id for subsequest row to insert in that same object in the array

Usage

Suppose we have two tables

Table 1: cars

Table 2: drivers — Every driver is assigned to zero or more cars using another table

Table 3: assigned_cars

Now, suppose we have a graphql schema like:

For retrieving all drivers we can retrive all rows using sql join. But we need to group by driver and make an array of car for same driver. flat2hier does this using some simple convention. e.g.

flat2hier folds your array of row by:

  • Breaking each key/col name by _, and add the partial key into a object hierarchy(new object if at top level otherwise at current level). e.g. user_mailbox_certs_ssl128 => { user: { mailbox: { certs: { ssl128: <value> } } } }
  • If a partial key(after splitting by _) starts with $ instead of adding a object for that key, it adds an array of object. e.g. $query_queryMetadata_sender_id => {$query: [ { queryMetadata: { sender: {id: <value>}} } ]}
  • If it finds another partial key with starting with a $ and has a partial key child with name id it tries to find a object in the same key with matching id, if it finds one it creates object at current level and append in the array it just found.

An advanced example:

Output:

Installation

  • Clone this repository
  • Install dev dependencies
  • Run example/server.js with nodejs
/* Directly install from github */npm install --save git+https://github.com/Eyezon/flat2hier-graphql
Photo by Pankaj Patel on Unsplash

The first thing you’ll notice is that GraphQL queries mirror their response. This makes it easy to predict the shape of the data returned from a query, as well as to write a query if you know the data your app needs. More importantly, this makes GraphQL really easy to learn and use. GraphQL naturally follows relationships between objects, where a RESTful service may require a complex join statement in SQL. This data hierarchy pairs well with graph-structured data stores and ultimately with the hierarchical user interfaces it’s used within. We believe this tool will be an addition to developer’s handbag which makes life easy by retrieving the data in form of a GraphQL response.

--

--