Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

graphQL join on common column

how we can do data join on a common column in graphQL.

For example in SQL : Select t.name and z.address where t.id=z.id;

how is this managed by graphQL query.

like image 807
SillyPerson Avatar asked Aug 09 '26 03:08

SillyPerson


1 Answers

How is this managed by GraphQL query?

The short answer: It's not (managed). You have to write it in your resolvers.

Your GraphQL schema is basically just a declaration of types that represent nodes, and fields that represent edges in a graph. GraphQL is the language to query this graph. At each edge in your schema, you write a function for how to get the data when the query traverses that edge. This function is called a "resolver", and you can put pretty much any code in your resolvers as long as it returns valid data. This means GraphQL is completely, absolutely database-independent. Your resolvers are responsible for talking to your database(s).

So when it comes to SQL, and joins, the concern of making the queries is entirely the responsibility of the API developer. GraphQL knows nothing about SQL or joins, so this is essentially custom logic for your application which you must write. It turns out that building SQL queries to resolve data for GraphQL queries is very difficult without running into problems like eagerly fetching too much data or the "N+1" problem.

Some tools in the open-source community have emerged to help solve this problem. Here are a couple I would recommend in the Node.js space:

  • DataLoader - A database-agnostic tool that batches queries and caches individual records.
  • Join Monster - A SQL-tailored tool for efficient data-retrieval and query batching. It examines each query and your schema and generates SQL queries dynamically.

Disclaimer: I'm a co-creator of Join Monster.

like image 193
Andy Carlson Avatar answered Aug 11 '26 09:08

Andy Carlson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!