typegraphql-prisma
typegraphql-prisma copied to clipboard
The best way to hide output field
Is your feature request related to a problem? Please describe.
I want to declare which field i want to hide in graphql schema.
Describe the solution you'd like
In typegraphql way, dev just dont add @Field to the field.
But in typegraphql-prisma, @Field was add to the field automatically.
So if typegraphql Field decorator support pass a param called out
, then we can simply config it in applyModelsEnhanceMap like this.
applyModelsEnhanceMap({
User: {
fields: {
password: [Field({ out: false })],
},
},
})
Describe alternatives you've considered
I have read the official doc, and find a way to hide field, https://prisma.typegraphql.com/docs/advanced/hiding-field
But i think schema.prisma
is mean to define db schema and typescript schema. And applyModelsEnhanceMap is created to defined some stuff about typegraphql.
So maybe better to be configable in apply***EnhanceMap.
Or we can add a Unout decorator in typegraphql-prisma. then the code will be like this.
applyModelsEnhanceMap({
User: {
fields: {
password: [Unout()],
},
},
})
BTW, typegraphql
and typegraphql-prisma
are very nice. It helps me a lot, Thank you so much.
https://prisma.typegraphql.com/docs/advanced/hiding-field
In typegraphql way, dev just dont add @field to the field. But in typegraphql-prisma, @field was add to the field automatically.
It's because of the decorators nature - in order to collect the metadata, they need to be placed on top of every field.
In Prisma schema, we have access to all the metadata, so it's not needed.
And I think that supporting both whitelist and blacklist ways at the same time might be very confusing and also error-prone, when you put @typeGraphQL.type()
and @typeGraphQL.field(out: false)
at the same time as this will result in conflict
https://prisma.typegraphql.com/docs/advanced/hiding-field
But how do I hide some fields for some users and show for others? Let's say I want to show the user's e-mail for the admins but not for other kind of users?
@melanke You need to write custom field resolvers that will do the logic of returning the value or throwing forbidden error. GraphQL does not support dynamic schemas per user, so you can't conditionally hide the field.
@MichalLytek so I simply create a @FieldResolver
of an existing field on my prisma schema and throw an exception inside this resolver on some cases? I don't need to use @TypeGraphQL.omit
comment on this case, right? Thanks for the tip
@MichalLytek Do you need to have an existing field in your prisma schema for @FieldResolver
to work? For my use case, I'm hiding a clientSecret
field in the schema but want to create a separate field called clientSecretExists
with a custom field resolver. So far I have the following:
@Resolver((of) => Tenant)
class CustomTenantResolver {
@FieldResolver()
clientSecretExists(@Root() tenant: Tenant): boolean {
return tenant.clientSecret !== undefined;
}
}
The problem is that I'm getting the error Error: Unable to infer GraphQL type from TypeScript reflection system. You need to provide explicit type for 'clientSecretExists' of 'CustomTenantResolver' class
because @FieldResolver
doesn't know what it's resolving.
@topbloc-beiswenger For type inference you need to have configured tsconfig
properly:
https://typegraphql.com/docs/installation.html
Then you should not be force to do @FieldResolver(() => Boolean)
Closing as @TypeGraphQL.omit(output: true)
is the desired solution 🔒