concept PostgreSQL in category graphql

This is an excerpt from Manning's book GraphQL in Action.
Another big technological benefit about GraphQL is communicating with multiple services. When you have multiple clients requesting data from multiple data storage services (like PostgreSQL, MongoDB, and a REDIS cache), a GraphQL layer in the middle can simplify and standardize this communication. Instead of a client going to the multiple data services directly, you can have that client communicate with the GraphQL service. Then, the GraphQL service will do the communication with the different data services. This is how GraphQL isolates the clients from needing to communicate in multiple languages. A GraphQL service translates a single client’s request into multiple requests to multiple services using different languages.
Figure 4.3. The UML diagram for the 3 tables in PostgreSQL
![]()
We could use the column alias feature in PostgreSQL to make it return the rows natively as camel case. However, this solution requires listing all the leaf fields in the SELECT statement itself. For example, here’s a version of the
taskMainListresolver function to implement this method:Listing 6.16. Using PostgreSQL aliases to get everything in camel case natively
resolve: async (source, args, { pgPool }) => { const pgResp = await pgPool.query(` SELECT id, content, tags, approach_count AS "approachCount", created_at AS "createdAt" FROM azdev.tasks WHERE // ·-·-· `); return pgResp.rows; },Note how I use the
AS "approachCount"andAS "createdAt"syntax to rename the returned columns. The quotes around the aliases are required because PostgreSQL is case insensitive. To force it to behave otherwise, you need to use quotes.I think this is the better method, not only because it makes the data coming from PostgreSQL in the exact shape we need but also because I think having to list the column names that a statement needs is a good thing. Specificity is always the safer way.