Skip to content

Move TContext generic from requestDidStart method to ApolloServerPlugin Interface#3525

Merged
trevor-scheer merged 6 commits into
apollographql:masterfrom
jdmoody:master
Dec 9, 2019
Merged

Move TContext generic from requestDidStart method to ApolloServerPlugin Interface#3525
trevor-scheer merged 6 commits into
apollographql:masterfrom
jdmoody:master

Conversation

@jdmoody

@jdmoody jdmoody commented Nov 17, 2019

Copy link
Copy Markdown
Contributor

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
Copy Markdown

@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

@trevor-scheer trevor-scheer left a comment

Copy link
Copy Markdown
Contributor

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

abernix commented Nov 20, 2019

Copy link
Copy Markdown
Member

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

jdmoody commented Nov 20, 2019

Copy link
Copy Markdown
Contributor Author

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

trevor-scheer commented Nov 21, 2019

Copy link
Copy Markdown
Contributor

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
Copy Markdown

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
Copy Markdown
Contributor

Bumping this for Monday, cc @abernix.

Comment thread packages/apollo-server-plugin-base/src/index.ts Outdated
Comment thread packages/apollo-server-plugin-base/src/index.ts
@abernix

abernix commented Dec 3, 2019

Copy link
Copy Markdown
Member

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
Copy Markdown
Contributor

Thanks again, @jdmoody!

@dancon

dancon commented Dec 10, 2019

Copy link
Copy Markdown

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.

6 participants