n1ru4l / envelop

Envelop is a lightweight library allowing developers to easily develop, share, collaborate and extend their GraphQL execution layer. Envelop is the missing GraphQL plugin system.
https://envelop.dev
MIT License
779 stars 126 forks source link

Refactor Generic Auth plugin #2281

Closed ardatan closed 3 weeks ago

ardatan commented 1 month ago

Refactor Generic Auth plugin;

useGenericAuth({
  // ...
  authDirectiveName: 'auth',
});
useGenericAuth({
  // ...
- directiveOrExtensionFieldName: 'auth',
+ authDirectiveName: 'auth',
});

type User @authenticated { id: ID! name: String! }


- `validateUser` function does not receive `fieldAuthDirectiveNode` and `fieldAuthExtension` anymore. Instead, it takes `fieldAuthArgs` which is an object that contains the arguments of the auth directive or extension. So you don't need to parse the arguments manually anymore.

```ts
const validateUser: ValidateUserFn = params => {
  if (!params.fieldAuthArgs.roles.includes('admin')) {
    return createUnauthorizedError(params);
  }
};
export type ValidateUserFnParams<UserType> = {
  /** The user object. */
  user: UserType;
  /** The field node from the operation that is being validated. */
  fieldNode: FieldNode;
  /** The parent type which has the field that is being validated. */
  parentType: GraphQLObjectType | GraphQLInterfaceType;
  /** The auth directive arguments for the type */
  typeAuthArgs?: Record<string, any>;
  /** The directives for the type */
  typeDirectives?: ReturnType<typeof getDirectiveExtensions>;
  /** Scopes that type requires */
  typeScopes?: string[][];
  /** Policies that type requires */
  typePolicies?: string[][];
  /** The object field */
  field: GraphQLField<any, any>;
  /** The auth directive arguments for the field */
  fieldAuthArgs?: Record<string, any>;
  /** The directives for the field */
  fieldDirectives?: ReturnType<typeof getDirectiveExtensions>;
  /** Scopes that field requires */
  fieldScopes?: string[][];
  /** Policies that field requires */
  fieldPolicies?: string[][];
  /** Extracted scopes from the user object */
  userScopes: string[];
  /** Policies for the user */
  userPolicies: string[];
  /** The args passed to the execution function (including operation context and variables) **/
  executionArgs: ExecutionArgs;
  /** Resolve path */
  path: ReadonlyArray<string | number>;
};
directive @requiresScopes(scopes: [String!]!) on OBJECT | INTERFACE | FIELD_DEFINITION

directive @policy(policy: String!) on OBJECT | INTERFACE | FIELD_DEFINITION

Check README for more information.

changeset-bot[bot] commented 1 month ago

🦋 Changeset detected

Latest commit: 5895172c4ba7cb39522bc4afb71761ddb3d5f904

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 2 packages | Name | Type | | ---------------------------- | ----- | | @envelop/generic-auth | Major | | @envelop/extended-validation | Minor |

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

github-actions[bot] commented 1 month ago
### 💻 Website Preview The latest changes are available as preview in: [https://a4016de4.envelop.pages.dev](https://a4016de4.envelop.pages.dev)
theguild-bot commented 1 month ago

✅ Benchmark Results

     ✓ no_errors
     ✓ expected_result

     checks.............................................: 100.00% ✓ 739216      ✗ 0     
     ✓ { mode:envelop-cache-and-no-internal-tracing }...: 100.00% ✓ 160410      ✗ 0     
     ✓ { mode:envelop-cache-jit }.......................: 100.00% ✓ 320262      ✗ 0     
     ✓ { mode:envelop-just-cache }......................: 100.00% ✓ 159938      ✗ 0     
     ✓ { mode:graphql-js }..............................: 100.00% ✓ 98606       ✗ 0     
     data_received......................................: 2.8 GB  24 MB/s
     data_sent..........................................: 161 MB  1.3 MB/s
     envelop_init.......................................: avg=0s      min=0s       med=0s      max=0s       p(90)=0s      p(95)=0s     
     ✓ { mode:envelop-cache-jit }.......................: avg=0s      min=0s       med=0s      max=0s       p(90)=0s      p(95)=0s     
     ✓ { mode:envelop-just-cache }......................: avg=0s      min=0s       med=0s      max=0s       p(90)=0s      p(95)=0s     
     ✓ { mode:graphql-js }..............................: avg=0s      min=0s       med=0s      max=0s       p(90)=0s      p(95)=0s     
     envelop_total......................................: avg=0s      min=0s       med=0s      max=0s       p(90)=0s      p(95)=0s     
     ✓ { mode:envelop-cache-jit }.......................: avg=0s      min=0s       med=0s      max=0s       p(90)=0s      p(95)=0s     
     ✓ { mode:envelop-just-cache }......................: avg=0s      min=0s       med=0s      max=0s       p(90)=0s      p(95)=0s     
     ✓ { mode:graphql-js }..............................: avg=0s      min=0s       med=0s      max=0s       p(90)=0s      p(95)=0s     
     event_loop_lag.....................................: avg=0s      min=0s       med=0s      max=0s       p(90)=0s      p(95)=0s     
     ✓ { mode:envelop-cache-and-no-internal-tracing }...: avg=0s      min=0s       med=0s      max=0s       p(90)=0s      p(95)=0s     
     ✓ { mode:envelop-cache-jit }.......................: avg=0s      min=0s       med=0s      max=0s       p(90)=0s      p(95)=0s     
     ✓ { mode:envelop-just-cache }......................: avg=0s      min=0s       med=0s      max=0s       p(90)=0s      p(95)=0s     
     ✓ { mode:graphql-js }..............................: avg=0s      min=0s       med=0s      max=0s       p(90)=0s      p(95)=0s     
     graphql_context....................................: avg=0s      min=0s       med=0s      max=0s       p(90)=0s      p(95)=0s     
     ✓ { mode:envelop-cache-jit }.......................: avg=0s      min=0s       med=0s      max=0s       p(90)=0s      p(95)=0s     
     ✓ { mode:envelop-just-cache }......................: avg=0s      min=0s       med=0s      max=0s       p(90)=0s      p(95)=0s     
     ✓ { mode:graphql-js }..............................: avg=0s      min=0s       med=0s      max=0s       p(90)=0s      p(95)=0s     
     graphql_execute....................................: avg=0s      min=0s       med=0s      max=0s       p(90)=0s      p(95)=0s     
     ✓ { mode:envelop-cache-jit }.......................: avg=0s      min=0s       med=0s      max=0s       p(90)=0s      p(95)=0s     
     ✓ { mode:envelop-just-cache }......................: avg=0s      min=0s       med=0s      max=0s       p(90)=0s      p(95)=0s     
     ✓ { mode:graphql-js }..............................: avg=0s      min=0s       med=0s      max=0s       p(90)=0s      p(95)=0s     
     graphql_parse......................................: avg=0s      min=0s       med=0s      max=0s       p(90)=0s      p(95)=0s     
     ✓ { mode:envelop-cache-jit }.......................: avg=0s      min=0s       med=0s      max=0s       p(90)=0s      p(95)=0s     
     ✓ { mode:envelop-just-cache }......................: avg=0s      min=0s       med=0s      max=0s       p(90)=0s      p(95)=0s     
     ✓ { mode:graphql-js }..............................: avg=0s      min=0s       med=0s      max=0s       p(90)=0s      p(95)=0s     
     graphql_validate...................................: avg=0s      min=0s       med=0s      max=0s       p(90)=0s      p(95)=0s     
     ✓ { mode:envelop-cache-jit }.......................: avg=0s      min=0s       med=0s      max=0s       p(90)=0s      p(95)=0s     
     ✓ { mode:envelop-just-cache }......................: avg=0s      min=0s       med=0s      max=0s       p(90)=0s      p(95)=0s     
     ✓ { mode:graphql-js }..............................: avg=0s      min=0s       med=0s      max=0s       p(90)=0s      p(95)=0s     
     http_req_blocked...................................: avg=1.98µs  min=772ns    med=1.84µs  max=3.82ms   p(90)=2.45µs  p(95)=2.78µs 
     http_req_connecting................................: avg=16ns    min=0s       med=0s      max=838.73µs p(90)=0s      p(95)=0s     
     http_req_duration..................................: avg=2.96ms  min=170.32µs med=2.81ms  max=45.39ms  p(90)=5.45ms  p(95)=6.26ms 
       { expected_response:true }.......................: avg=2.96ms  min=170.32µs med=2.81ms  max=45.39ms  p(90)=5.45ms  p(95)=6.26ms 
     ✓ { mode:envelop-cache-and-no-internal-tracing }...: avg=3.46ms  min=428.01µs med=3.05ms  max=27.42ms  p(90)=5.86ms  p(95)=6.32ms 
     ✓ { mode:envelop-cache-jit }.......................: avg=1.57ms  min=170.32µs med=1.24ms  max=17.4ms   p(90)=2.55ms  p(95)=2.76ms 
     ✓ { mode:envelop-just-cache }......................: avg=3.47ms  min=476.63µs med=3.02ms  max=23.77ms  p(90)=5.86ms  p(95)=6.32ms 
     ✓ { mode:graphql-js }..............................: avg=5.8ms   min=804.39µs med=4.91ms  max=45.39ms  p(90)=9.72ms  p(95)=10.57ms
     http_req_failed....................................: 0.00%   ✓ 0           ✗ 369608
     http_req_receiving.................................: avg=35.31µs min=12.5µs   med=31.3µs  max=12.31ms  p(90)=47.22µs p(95)=51.87µs
     http_req_sending...................................: avg=13.25µs min=4.82µs   med=10.96µs max=14.17ms  p(90)=20.16µs p(95)=21.85µs
     http_req_tls_handshaking...........................: avg=0s      min=0s       med=0s      max=0s       p(90)=0s      p(95)=0s     
     http_req_waiting...................................: avg=2.91ms  min=140.39µs med=2.76ms  max=45.31ms  p(90)=5.4ms   p(95)=6.2ms  
     http_reqs..........................................: 369608  3080.023145/s
     iteration_duration.................................: avg=3.23ms  min=356.26µs med=3.06ms  max=46.16ms  p(90)=5.73ms  p(95)=6.55ms 
     iterations.........................................: 369608  3080.023145/s
     vus................................................: 10      min=10        max=10  
     vus_max............................................: 20      min=20        max=20  
github-actions[bot] commented 1 month ago

🚀 Snapshot Release (alpha)

The latest changes of this PR are available as alpha on npm (based on the declared changesets): Package Version Info
@envelop/extended-validation 4.1.0-alpha-20240812203512-43551114 npm ↗︎ unpkg ↗︎
@envelop/generic-auth 8.0.0-alpha-20240812203512-43551114 npm ↗︎ unpkg ↗︎