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.
I have a similar use case to the one described here #3052
Currently I'm using the
fieldContextTypesoption available in the plugin, but in my option it could be better to implement aan 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
fieldContextTypesoption.Example
Describe the solution you'd like
This should generate a Context of type
LoggedContext<Context>on fields with directiveisLoggedInDescribe alternatives you've considered
An alternative is using
fieldContextTypesbut you need to manually extend fields and does not work well if you have a huge project.