DEV Community

Cover image for I've created knex-graphql-utils, which optimizes your GraphQL server with PostgreSQL
Kay Gosho
Kay Gosho

Posted on • Edited on

3

I've created knex-graphql-utils, which optimizes your GraphQL server with PostgreSQL

I've created a tiny library which improves performance for GraphQL + Knex.

tl;dr

Before

image

After

image

  • Filter select columns based on a query from clients
  • Load relations without N+1 problem (pagination available!)

https://github.com/acro5piano/knex-graphql-utils

Currently only PostgreSQL is supported, but I'll support other RDBMS too if needed.

Motivation

Creating a GraphQL service with a Relational Database is a hard thing. We should take care of:

  • Performance for querying relations. N+1 problem will happen if you don't use Dataloader.
  • select * make your server slow, but hard to filter columns based on requests.
  • Pagination. Dataloader pattern is hard to implement pagination without a hacky union or window functions. knex-graphql-utils uses row_number() window function to do it.

As a developer, I noticed that I'm doing the same thing again and again - so I convert my work into a library.

The syntax is like this:

image

Please try it! Any feedback is welcome.

https://github.com/acro5piano/knex-graphql-utils

Image of Datadog

The Future of AI, LLMs, and Observability on Google Cloud

Datadog sat down with Google’s Director of AI to discuss the current and future states of AI, ML, and LLMs on Google Cloud. Discover 7 key insights for technical leaders, covering everything from upskilling teams to observability best practices

Learn More

Top comments (1)

Collapse
 
adamwdennis profile image
Adam Dennis

@acro5piano thanks for the write-up on this. I'm actively working on a NestJS/GraphQL/TypeORM project, and was looking into adding dataloader, and found this post... I didn't know about the pagination problem with dataloader, so I might hold off for certain resolvers for now. Anyway, just appreciate the info and the link to the github issue. If i were using knexjs, I would use your lib. Cheers!

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more