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-resolvers] Map custom context type on a type-level #6483

Open
Tracked by #8296 ...
cerinoligutom opened this issue Aug 14, 2021 · 0 comments
Open
Tracked by #8296 ...

[typescript-resolvers] Map custom context type on a type-level #6483

cerinoligutom opened this issue Aug 14, 2021 · 0 comments
Labels
core Related to codegen core/cli kind/enhancement New feature or request

Comments

@cerinoligutom
Copy link
Contributor

Is your feature request related to a problem? Please describe.

I would like to map a custom context type on a type-level (Subscription in this case) and all fields under that type should inherit the mapped context type.

Apollo Server v3 has decoupled the subscription server entirely and because of that, I cannot create the subscription resolver's context from ApolloServer's class constructor anymore and therefore could not pass the Express req and res objects so I decided not to pass it for subscriptions since I'm not using it anyway. I wouldn't want to make the req and res objects to become optional from the original context type either as that'd mean extra checks for those that are using it.

With that said, the context type for my Subscriptions are now different compared to those in Queries and Mutations and I would like codegen to use the appropriate type based on a simple mapping in the config. Simplified example of the interfaces' shape below:

// For Queries and Mutations
interface IGraphQLContext {
  req: Express.Request;
  res: Express.Response;
  readonly userId: UniqueID | null;
}

// For Subscriptions
interface IGraphQLSubscriptionContext {
  readonly userId: UniqueID | null;
}

#3052 is similar to my case but the merged solution just limits itself to the defined fields of the type in the codegen config. Having n subscription resolvers would mean having n entries in the config as well to achieve the end goal. I wouldn't want the config to grow for every new subscription resolver added. Would be nice if we can just define the type, in this case Subscription, and all fields under it would use the mapped context type.

Describe the solution you'd like

I would like all fields under the type to use the mapped context type on that type rather than the current supported feature where I would need to define each field using fieldContextTypes to achieve the end goal.

config:
  fieldContextTypes:
    - Subscription#path/to/context#IGraphQLSubscriptionContext

rather than

config:
  fieldContextTypes:
    - Subscription.readMessageFromChat#path/to/context#IGraphQLSubscriptionContext
    - Subscription.listenForNotifications#path/to/context#IGraphQLSubscriptionContext
    - Subscription.channelAdded#path/to/context#IGraphQLSubscriptionContext
    # list grows for every new subscription resolver

Describe alternatives you've considered

Maybe wildcards or pattern matching for other advanced use cases (e.g. different context type for different domains based on a substring in the field)? Not sure if this will have any big impact or even possible but just throwing ideas.

config:
  fieldContextTypes:
    - Subscription.*#path/to/context#IGraphQLSubscriptionContext
@n1ru4l n1ru4l added the kind/enhancement New feature or request label Aug 20, 2021
@charlypoly charlypoly added the core Related to codegen core/cli label Nov 3, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
core Related to codegen core/cli kind/enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants