matthewdaniel
matthewdaniel

Reputation: 1980

What is the best practice to join tables during graphql query

If my graphql has a user which has posts that are followed which obviously have posted users is there a best practice to efficiently get the data but not over request from the database.

should not join posts table { users { first_name } }

should join posts but not back onto users { users { followedPosts { title } } }

should join posts and back onto users for poster { users {followedPosts { poster { first_name } } } }

I've thought about inspecting the context on the initial resolver to see if the appropriate fields are fetched but in the library i'm using it is pretty ugly.

I'd like to know if there are good practices that exist to solve this and add appropriate joins or does everyone just query the heck out of the database and try to do some caching.

Also, if joins aren't common, what about inspecting the posters that will be requested and fetch them all in one go rather than as graphql resolves down the chain.

Upvotes: 0

Views: 1557

Answers (1)

yachaka
yachaka

Reputation: 5579

Stick to separate queries for separate tables and have a look at DataLoader for batching multiples calls to the same table, and eventually some per-request caching.

This is usually more than enough.

Upvotes: 0

Related Questions