Prisma1: `$fragment` API of TS client should use more specific return type by default

Created on 11 Oct 2018  路  1Comment  路  Source: prisma/prisma1

Consider the following call:

const userFieldSelection = `
  fragment UserSelection on User {
    id
    email
  }
`
const users = await prisma.users().$fragment(userFieldSelection)

Currently the type of users is {} by default. This should be more specific and the call to $fragment should by return the XNode type of the model it was called on.

So, in the above case the type for users should be UserNode[].

areclients

Most helpful comment

Thanks a lot for this suggestion @nikolasburk, however, I don't think it's a good idea to add the model type (e.g. UserNode) as the default generic param since the return value (specified by the fragment string) doesn't necessarily have to match the shape of the model type because of GraphQL field aliases.

Unfortunately, TypeScript doesn't seem to allow a way to "force" the user to provide a generic type param because of it's type inference mechanism but latest if a user tries to access a field (e.g. user.myField) there will be an error message saying Property 'myField' does not exist on type '{}'..

>All comments

Thanks a lot for this suggestion @nikolasburk, however, I don't think it's a good idea to add the model type (e.g. UserNode) as the default generic param since the return value (specified by the fragment string) doesn't necessarily have to match the shape of the model type because of GraphQL field aliases.

Unfortunately, TypeScript doesn't seem to allow a way to "force" the user to provide a generic type param because of it's type inference mechanism but latest if a user tries to access a field (e.g. user.myField) there will be an error message saying Property 'myField' does not exist on type '{}'..

Was this page helpful?
0 / 5 - 0 ratings

Related issues

schickling picture schickling  路  3Comments

Fi1osof picture Fi1osof  路  3Comments

marktani picture marktani  路  3Comments

notrab picture notrab  路  3Comments

schickling picture schickling  路  3Comments