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

[typescript-resolver] Context extender for fields with directives #7505

Closed
ludusrusso opened this issue Feb 10, 2022 · 1 comment
Closed
Labels
stage/6-released The issue has been solved on a released version of the library

Comments

@ludusrusso
Copy link
Contributor

ludusrusso commented Feb 10, 2022

I have a similar use case to the one described here #3052

Currently I'm using the fieldContextTypes option available in the plugin, but in my option it could be better to implement a
an option that allow to extend the context in some way if a directive is present in a filed. In this case, if you have several field with a specific directive, you do not have to manually map each field in the fieldContextTypes option.

Example

# schema.graphql
type Mutation {
  notAuthMutaiton(): [NotAuthRes!]!
  loggedMutation (content: String!): loggedRes! @isLoggedIn
}

type Query {
  loggedQuery: LoggedRes!  @isLoggedIn
}
// context.ts

// This resolver context is used for all fields
export interface Context {
 someData: number
}

// This type function should be used for fields with `isLoggedIn` directive
export type LoggedContext<Context = any> = Context & {user: User}

Describe the solution you'd like

# codegen.yml
generates:
  ./src/graphql/resolvers/types.generated.ts:
    plugins:
      - typescript
      - typescript-resolvers
    config:
      contextType: ./context.ts#Context
     directiveContextExtender:
      - isLoggedIn: ./context.ts#LoggedContext

This should generate a Context of type LoggedContext<Context> on fields with directive isLoggedIn

Describe alternatives you've considered
An alternative is using fieldContextTypes but you need to manually extend fields and does not work well if you have a huge project.

@charlypoly charlypoly added the stage/4-pull-request A pull request has been opened that aims to solve the issue label Mar 10, 2022
@charlypoly
Copy link
Contributor

Thank you for your contribution @ludusrusso! ⚡

directiveContextTypes is now available in @graphql-codegen/[email protected] 📦

@charlypoly charlypoly added stage/6-released The issue has been solved on a released version of the library and removed stage/4-pull-request A pull request has been opened that aims to solve the issue labels Mar 17, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stage/6-released The issue has been solved on a released version of the library
Projects
None yet
Development

No branches or pull requests

2 participants