typegraphql-prisma icon indicating copy to clipboard operation
typegraphql-prisma copied to clipboard

The best way to hide output field

Open Yrobot opened this issue 3 years ago • 9 comments

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.

Yrobot avatar Jul 27 '21 08:07 Yrobot

Or we can add a Unout decorator in typegraphql-prisma. then the code will be like this.

  applyModelsEnhanceMap({
    User: {
      fields: {
        password: [Unout()],
      },
    },
  })

Yrobot avatar Jul 27 '21 08:07 Yrobot

BTW, typegraphql and typegraphql-prisma are very nice. It helps me a lot, Thank you so much.

Yrobot avatar Jul 27 '21 08:07 Yrobot

https://prisma.typegraphql.com/docs/advanced/hiding-field

JClackett avatar Aug 03 '21 17:08 JClackett

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

MichalLytek avatar Aug 25 '21 10:08 MichalLytek

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 avatar Sep 24 '21 16:09 melanke

@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 avatar Sep 24 '21 16:09 MichalLytek

@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

melanke avatar Sep 24 '21 16:09 melanke

@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 avatar Oct 04 '21 22:10 topbloc-beiswenger

@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)

MichalLytek avatar Oct 05 '21 06:10 MichalLytek

Closing as @TypeGraphQL.omit(output: true) is the desired solution 🔒

MichalLytek avatar Feb 08 '23 09:02 MichalLytek