hayes / pothos

Pothos GraphQL is library for creating GraphQL schemas in typescript using a strongly typed code first approach
https://pothos-graphql.dev
ISC License
2.28k stars 153 forks source link

`withAuth` and `authScopes` don't combine #1192

Open GauBen opened 2 months ago

GauBen commented 2 months ago

Hey, I noticed that while you can use both withAuth and authScopes on the same field, it does not actually combine declarations despite what type declarations lead to think

https://github.com/hayes/pothos/blob/3cebfed6f033ee92c2b3492a3fd6d3002906ab08/packages/plugin-scope-auth/src/field-builders.ts#L58C7-L58C26

Example:

builder.mutationField('updateUserPropery', (t) =>
  t.withAuth({ user: true }).boolean({
    authScopes(_, args, context) {
      // Here `context` will be typed according to `AuthContexts`
      // but `{ user: true }` will be overwritten by this function
    },
    resolve() {
      // ...
    }
  })
)

Instead, I'd like both { user: true } and authScopes(){} to be merged

hayes commented 2 months ago

That seems reasonable to me, I think modifying this line https://github.com/hayes/pothos/blob/3cebfed6f033ee92c2b3492a3fd6d3002906ab08/packages%2Fplugin-scope-auth%2Fsrc%2Ffield-builders.ts#L58

To do something like this would work

authScopes: options.authScopes ? { $all: [scopes, options.authScopes] } : scopes,
hayes commented 2 months ago

It's probably a little more complex, i think my example works if the new scopes are an object, but not if it's a function. Handling the function case should be pretty easy though by wrapping it to return the merged scopes