Skip to content

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

@ludusrusso

Description

@ludusrusso

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    stage/6-releasedThe issue has been solved on a released version of the library

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions