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

Move TContext generic from requestDidStart method to ApolloServerPlugin Interface #3525

Merged
merged 6 commits into from
Dec 9, 2019

Conversation

jdmoody
Copy link
Contributor

@jdmoody jdmoody commented Nov 17, 2019

When using an ApolloServerPlugin with a different context, I get the following TypeScript error:

Type 'TContext' is not assignable to type 'Context'.

I believe this is due to annotating the requestDidStart context argument with a different context (in an attempt to get type-safety for the function):

export const MyPlugin: ApolloServerPlugin = {
    requestDidStart(context: GraphQLRequestContext<MyContext>) {

After this change, I'll be able to set the context on the plugin, instead:

export const MyPlugin: ApolloServerPlugin<MyContext> = {
    requestDidStart(context) {

If there's another way to accomplish this, please let me know! Thanks!

@apollo-cla
Copy link

@jdmoody: Thank you for submitting a pull request! Before we can merge it, you'll need to sign the Apollo Contributor License Agreement here: https://contribute.apollographql.com/

@jdmoody jdmoody force-pushed the master branch 2 times, most recently from 7b7b996 to e91029b Compare November 17, 2019 02:54
Copy link
Member

@trevor-scheer trevor-scheer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This LGTM @jdmoody, and thank you for submitting this! I'd like for @abernix to take a peek for a quick sanity check as well.

@abernix
Copy link
Member

abernix commented Nov 20, 2019

Thanks for looking at this!

Without re-creating a running example myself, I believe I understand why this is necessary (And while I'm almost certain that providing TContext on ApolloServerPlugin would be more ideal) but wouldn't this be a breaking change for anyone who was currently passing their own context type in the TContext type argument to (the existing) requestDidStart? (Were you able/unable to pass in TContext to requestDidStart in your own implementation of it?)

@jdmoody
Copy link
Contributor Author

jdmoody commented Nov 20, 2019

Without re-creating a running example myself, I believe I understand why this is necessary (And while I'm almost certain that providing TContext on ApolloServerPlugin would be more ideal) but wouldn't this be a breaking change for anyone who was currently passing their own context type in the TContext type argument to (the existing) requestDidStart? (Were you able/unable to pass in TContext to requestDidStart in your own implementation of it?)

@abernix, as far as I can tell, there's no way to pass in a type argument to an Interface method when implementing that Interface.

I think the type argument can only be passed in when calling requestDidStart, which is done within apollo-server-core, right?

if (!plugin.requestDidStart) continue;


I initially tried something like this:

import MyContext from 'src/context'

export const MyPlugin: ApolloServerPlugin = {
    requestDidStart<MyContext>(context) {

But this just defines a new generic called MyContext rather than passing in the MyContext type defined above.

@trevor-scheer
Copy link
Member

trevor-scheer commented Nov 21, 2019

Yeah, this checks out on my end as a fix for something that's currently in a broken state. I recall running into a similar issue.

Minimal reproduction @abernix:
https://bit.ly/2qyEKc2

@MichalLytek
Copy link

I've also encountered this problem about not being able to provide custom Context type to the plugin hook.

This change also allows to use inline plugins:

plugins: [
  {
    requestDidStart(requestContext: GraphQLRequestContext<Context>) {
      return {
        willSendResponse() {
          console.log(requestContext.context.requestId);
        },
      };
    },
  },
],

@trevor-scheer
Copy link
Member

Bumping this for Monday, cc @abernix.

@abernix
Copy link
Member

abernix commented Dec 3, 2019

It's worth noting that what @MichalLytek provided above does work and doesn't sacrifice any type safety from what I can tell — since requestDidStart is early enough in the pipeline where it doesn't have any additional type narrowing applied to its GraphQLRequestContext. It is, however, obviously still not the intuitive approach, so I'm still supportive of landing this.

Furthermore, if we couple this change to this declaration for ApolloServerPlugin on the plugins member of this interface, we may have some type wins?

@trevor-scheer trevor-scheer merged commit 9e1f54a into apollographql:master Dec 9, 2019
trevor-scheer added a commit that referenced this pull request Dec 9, 2019
@trevor-scheer
Copy link
Member

Thanks again, @jdmoody!

@dancon
Copy link

dancon commented Dec 10, 2019

when to release the latest version? @abernix @trevor-scheer

@abernix abernix added this to the Release 2.9.14 milestone Dec 17, 2019
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 16, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants