GlobalIDs with prismaObject
; Using prismaObject
and prismaNode
in Conjunction
#866
-
Trying to summarize my use case as short as possible; there's a lot going on with our setup. Plugins: Prisma, Relay, ScopeAuth, Simple Objects The project I'm working on has a combination of regular Here's an minimal example that mimics the relevant parts of our setup; hope I'm explaining everything right, but bonus points if you correct something wrong with my explanation of how everything fits together! Consider the following blog-type schema: // note that IDs are stored as Int
model User {
id Int @id @default(autoincrement())
name String
email String @unique
post Post[]
}
model Post {
id Int @id @default(autoincrement())
title String
content String
published Boolean @default(false)
author User @relation(fields: [authorId], references: [id])
authorId Int
tag Tag[]
}
// in the real world this would be a m-n relationship, using 1-m to keep things simple here (avoid join tables)
model Tag {
id Int @id @default(autoincrement())
post Post @relation(fields: [postId], references: [id])
postId Int
} Currently, our setup looks something like this: import { builder } from './libs/pothos/src/builder';
builder.prismaNode('User', (t) => ({
id: { field: 'id' },
}))
builder.prismaNode('Post', (t) => ({
id: { field: 'id' },
runScopesOnType: true,
authScopes: (post, ctx) => ({
canAccess: ctx.user.id === post.author_id,
}),
fields: {
tag: t.field({
type: ['Tag'],
})
}
}));
builder.prismaObject('Tag', (t) => ({
fields: { ... },
})) Because tag isn't a node and purposely isn't given a Queue the issue. As a It gets confusing for both sides of the tech stack when IDs aren't the same type. For example, if we query ` Potential SolutionsI see two different solutions here. One is to defer all the complexity to the frontend, and have them convert all Int into globalIDs after receiving queried data The other solution is to perform this work on the backend. After a query is completed, determine which fields are Int and convert them all into globalIDs before returning to the frontend. Lastly, we could migrate to use As I'm writing this out, the second solution mentioned there doesn't seem as horrible to implement. However, still worth asking if there's a better way to do all this. Hopefully all of this makes sense; typing it up late at night after trying to brainstorm off and on during the day. Happy to provide more context about our setup if there's a specific design pattern we should be using instead that might solve all our problems. Thank you! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
You can use t.globalID to add globalID fields: https://pothos-graphql.dev/docs/plugins/relay#global-ids builder.prismaObject('Tag', {
fields: t => ({
id: t.globalID(({
resolve: tag => ({ id: tag.id, type: 'Type' })
})
}),
}) |
Beta Was this translation helpful? Give feedback.
You can use t.globalID to add globalID fields: https://pothos-graphql.dev/docs/plugins/relay#global-ids