Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Forward directive arguments params to context type #9669

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

robertherber
Copy link

@robertherber robertherber commented Sep 12, 2023

Description

This adds any arguments passed to directives to the directiveContextTypes, allowing more for specific context types.

Here is an example:

directive @auth (
  match: JSONObject
  skip: Boolean
  includes: JSONObject
) on FIELD_DEFINITION

type Mutation {
  createEntity(name: String!): Entity! @auth(match: { cmsCanModifyEntities: true })

  getEntity(name: String!): Entity! @auth(skip: true)
}

With this PR we can customize the ContextType of each resolver to adapt to whatever arguments where passed to the @auth directive.

In our example we can respect both the skip: true (i.e. no token in context) as well as cmsCanModifyEntities: true (our auth middleware has verified that the token contains this):

type AuthContextWithToken<TContext, DirectiveArgs extends {
      readonly match?: Record<string, unknown>
      readonly includes?: Record<string, unknown>
      readonly skip?: boolean
    }> = DirectiveArgs['skip'] extends true
      ? Omit<TContext, 'decodedToken' | 'token'>
      : Omit<TContext, 'decodedToken'> & {
        readonly decodedToken: DirectiveArgs['match']
        & Record<keyof DirectiveArgs['includes'], ReadonlyArray<DirectiveArgs['includes']>>
      }

Related # (issue)
#9668

Type of change

Please delete options that are not relevant.

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

Not 100% sure if we should consider this a breaking change, it expects the directiveContextTypes to accept 2 type parameters instead of 1.

Screenshots/Sandbox (if appropriate/relevant):

signal-2023-09-12-143323_004
signal-2023-09-12-143323_003
signal-2023-09-12-143323_002

How Has This Been Tested?

Please describe the tests that you ran to verify your changes. Provide instructions so we can reproduce. Please also list any relevant details for your test configuration

  • Used through patch in readapt
  • Added a resolver (bar) demonstrating the functionality in the current test suite.

Test Environment:

  • OS: MacOS
  • @graphql-codegen/...:
  • NodeJS: 18

Checklist:

  • I have followed the CONTRIBUTING doc and the style guidelines of this project
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes
  • Any dependent changes have been merged and published in downstream modules

@changeset-bot
Copy link

changeset-bot bot commented Sep 12, 2023

🦋 Changeset detected

Latest commit: fac1505

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

This PR includes changesets to release 10 packages
Name Type
@graphql-codegen/testing Minor
@graphql-codegen/typescript-resolvers Minor
@graphql-codegen/visitor-plugin-common Minor
@graphql-codegen/typescript-document-nodes Patch
@graphql-codegen/gql-tag-operations Patch
@graphql-codegen/typescript-operations Patch
@graphql-codegen/typed-document-node Patch
@graphql-codegen/typescript Patch
@graphql-codegen/graphql-modules-preset Patch
@graphql-codegen/client-preset Patch

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

@robertherber robertherber changed the title Add directive params to context type Forward directive arguments params to context type Sep 12, 2023
@robertherber
Copy link
Author

@dotansimha Any chance of getting a test run (and review) on this? Thx :)

Copy link
Collaborator

@saihaj saihaj left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice tests

@robertherber
Copy link
Author

@saihaj @dotansimha What are we missing to get this merged? Is there anything more I can do?

I see some checks are failing - but not sure it has to do with this PR (I see other PRs seems to fail similarly).

@robertherber
Copy link
Author

@saihaj @dotansimha Any chance of getting this merged? Thanks :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants