lolopinto / ent

MIT License
52 stars 6 forks source link

Functionality request/expectation: Auto create GQL resolvers for unique fields #380

Open mrtom opened 3 years ago

mrtom commented 3 years ago

When running through the intro tutorial, I was expecting that the codegen would create a query automatically in the GQL schema for any unique field in my ent schema.

For example, the user type created in the tutorial has an EmailAddress field with a unique constraint. I would have liked to have had the following resolver generated automatically:

  @gqlQuery({ name: "userByEmail", type: "User", nullable: true })
  async userByEmail(
    @gqlContextType() context: RequestContext,
    @gqlArg("emailAddress") emailAddress: string,
  ) {
    return await User.loadFromEmailAddress(context.getViewer(), emailAddress);
  }
lolopinto commented 3 years ago

This makes sense.

I'm not sure if it makes sense to have this be done automatically. you can imagine wanting some kind of rate limiting so that your data isn't scraped by default.

I do think there can be a flag that turns this on to get this behavior easily and then you can disable this and switch to do this manually in the future if you want rate limiting.

The reason for this complexity is autogenerated code should go in the base class: UserBase and user code should go in user-land User so if we did this for you, it would need to be in the base class and then if you want to take it over, you'd disable the autogeneration and write it manually like you have done above.