You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hi, I'm coming from GraphQL land and the concept of Fragment Colocation/Fragment Masking in tools like graphql-codegen and gql.tada is popular, where one colocates the actual data needed for a usage, with the implementation itself to avoid issues like overfetching as usage changes over time. Fields from a fragment are hidden behind a "mask" so that only the code that declares the data dependency can use it, encouraging each individual usage to declare their dependencies independently.
Is there a way to achieve something similar to this in Drizzle? For example, if I declared a users table with a posts relation, and a component needs the user's ID, name, and certain fields from posts like id, title, body; would I be able to somehow make a type-safe notion of a "fragment" of this query, and then merge that somehow onto the actual ultimate query?
Like is this a pattern that is idiomatic and isn't something inadvisable?
import{deepmerge}from"deepmerge-ts"import{relations}from"drizzle-orm";import{pgTable,text,uuid}from"drizzle-orm/pg-core";import{drizzle}from'drizzle-orm/node-postgres';constusers=pgTable("users",{id: uuid("id").primaryKey(),name: text("name")})constposts=pgTable("posts",{id: uuid("id").primaryKey(),author_id: uuid("author_id"),title: text("title"),body: text("body")})constusersRelations=relations(users,({many})=>({posts: many(posts)}))constpostsRelations=relations(posts,({one})=>({posts: one(users,{fields: [posts.author_id],references: [users.id]})}))constclient={}asany;// testingconstdb=drizzle(client,{schema: {users, posts, usersRelations, postsRelations}})typeFindUsersQueryParams=Parameters<typeofdb.query.users.findMany>[0];// for one componentconsttoQuery1={columns: {id: true,},with: {posts: {columns: {id: true,title: true}}}}satisfiesFindUsersQueryfunctionQuery1Component(props: {data: Awaited<ReturnType<typeofdb.query.users.findMany<typeoftoQuery1>>>}){returnprops.data.map(user=><divkey={user.id}>{row.posts.map(post=><divkey={post.id}>{post.title}</div>)}</div>)}// for another componentconsttoQuery2={columns: {id: true,name:true},with: {posts: {columns: {body: true}}}}satisfiesFindUsersQueryfunctionQuery2Component(props: {data: Awaited<ReturnType<typeofdb.query.users.findMany<typeoftoQuery2>>>}){returnprops.data.map(user=><divkey={user.id}>{row.posts.map(post=><divkey={post.id}>{post.body}</div>)}</div>)}// when ultimately fetching the dataconstresult=awaitdb.query.users.findMany(deepmerge(toQuery1,toQuery2))// then pass the result down to the components as needed<Query1Componentdata={result}/><Query2Componentdata={result}/>
This loses that "masking" aspect of it (so components can improperly re-use data from the other dependent queries, I assume there's no way around that) but it seems like the general pattern would work.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Hi, I'm coming from GraphQL land and the concept of Fragment Colocation/Fragment Masking in tools like
graphql-codegen
andgql.tada
is popular, where one colocates the actual data needed for a usage, with the implementation itself to avoid issues like overfetching as usage changes over time. Fields from a fragment are hidden behind a "mask" so that only the code that declares the data dependency can use it, encouraging each individual usage to declare their dependencies independently.Is there a way to achieve something similar to this in Drizzle? For example, if I declared a
users
table with aposts
relation, and a component needs theuser
's ID, name, and certain fields fromposts
likeid
,title
,body
; would I be able to somehow make a type-safe notion of a "fragment" of this query, and then merge that somehow onto the actual ultimate query?Like is this a pattern that is idiomatic and isn't something inadvisable?
This loses that "masking" aspect of it (so components can improperly re-use data from the other dependent queries, I assume there's no way around that) but it seems like the general pattern would work.
Thanks!
Beta Was this translation helpful? Give feedback.
All reactions