i'd also accept some built in support for annotati...
# prisma-client
r
i'd also accept some built in support for annotating the types prisma generates using gross typescript reflection support but that doesn't seem to really be possible
👀 2
n
Hey Ross 👋 sorry for the delay, were you able to figure this out in the meantime? I’m also checking with our Engineering teams to see if they have any advice for your situation and will get back to you soon!
I’ve got a recommendation from one of our Engineers. You could use middleware to append values to the response, considering that the
params
contains the model name:
Copy code
prisma.$use(async (params, next) => {
  const modelName = params.model

  const result = await next(params)

  result.__resolveType = () => {
    return modelName
  }

  return result
})
Its not type-safe though, and so you will need to add a
@ts-ignore
when calling the appended
__resolveType
method:
Copy code
async function main() {
  const prisma = new PrismaClient()

  prisma.$use(async (params, next) => {
    const modelName = params.model

    const result = await next(params)

    result.__resolveType = () => {
      return modelName
    }

    return result
  })

  const created = await prisma.user.create({
    data: {
      email: `user.${Date.now()}@prisma.io`,
    },
  })

  /*
    {
      id: 'd070c3c8-1d9d-4771-8f58-35984d2c93e3',
      createdAt: 2022-07-12T10:32:47.947Z,
      updatedAt: 2022-07-12T10:32:47.948Z,
      name: null,
      email: '<mailto:user.1657621967883@prisma.io|user.1657621967883@prisma.io>',
      __resolveType: [Function (anonymous)]
    }
  */
  console.log(created)

  /*
    user
  */
  // @ts-ignore
  console.log(created.__resolveType())
}
r
got it, yeah, that makes sense. is there anything on the roadmap to address this kinda of metadata need down the line?
and thanks for getting back to me!
n
On the roadmap we have Prisma Client extensions, which is somewhat similar to middleware but type-safe (see my

Prisma Day talk

), so I’d say this is already a better solution than the one I suggested above. However, it seems like the ideal solution for your issue would be something like this: https://github.com/prisma/prisma/issues/5315 If that’s the case, it would be great if you could add a 👍 and a comment to that GitHub issue with your use case to help our Product and Engineering teams prioritize this 🙂 (and if this issue doesn’t capture what you’re looking for, feel free to create a new one describing the feature you’d like to see and your “dream API” for it 😄 )
r
@Andrew Hammond this is the thread i was mentioning 😉
👀 1
done @nikolasburk, i'm actually kinda surprised this doesn't come up all the time since it's a particular problem implementing https://relay.dev/graphql/objectidentification.htm which is a super common pattern in GraphQL APIs of all kinds.
🙏 1