dotansimha / dotansimha/graphql-code-generator

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

Open
#6,483 0 comments 2 reactions 0 assignees View on GitHub
core kind/enhancement
Dominant language
TypeScript
Stars
11.3k
Forks
1.4k
Avg merge
1d 1h
Merged PRs (30d)
23

Description

**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](https://www.apollographql.com/docs/apollo-server/migration/#subscriptions) 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:

```ts
// 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`](https://www.graphql-code-generator.com/docs/plugins/typescript-resolvers) to achieve the end goal.

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

rather than

```yaml
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.

```yaml
config:
fieldContextTypes:
- Subscription.*#path/to/context#IGraphQLSubscriptionContext
```

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.