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

ApolloServerPlugin extends AnyFunctionMap causing Typescript Compilation Error #4585

Closed
brgaulin opened this issue Sep 23, 2020 · 3 comments
Closed
Assignees

Comments

@brgaulin
Copy link

Description

In previous versions of Apollo, the ApolloServerPlugin did not extend anything. In the latest version the ApolloServerPlugin now extends AnyFunctionMap. This now requires any object that is being used as a plugin to be structured in a way that it only contains the required functions. Creating say a Typescript Class and passing an instance of the class as a plugin is now unsupported without workarounds.

Package: apollo-server-plugin-base
Version: 0.10.0 (Latest as of September 2020)
PR that caused this: #4453
Change Diff: https://github.com/apollographql/apollo-server/pull/4453/files#diff-0f0cc848fe2c6442424f2ea0559f35ec

Expected Behavior

My Plugin can be a Typescript Class with any variable such as a string or object

Actual Behavior

The type definition of ApolloServerPlugin requires each field to be a function or undefined.
Error if you try and use a class for a Plugin:

  Type 'ApolloLogPlugin' is not assignable to type 'ApolloServerPlugin<Record<string, any>>'.
    Index signature is missing in type 'ApolloLogPlugin'.

Sandbox

I made a quick sandbox, this is the class we have, but its a lot more complicated in our repo, as we are instantiating a common logger and passing it around a few ways. So this exact example isn't super important, other than to see that it fails without the type definition override.

https://codesandbox.io/s/crazy-wind-ee0m1

(Note: I'm too familiar with code sandbox, and how to make it hot-reload with typescript. If you change any code, you might need to hit Restart Server in the sidebar)

Workaround

Add the following code to the top of the class to override the index definition from AnyFunctionMap

[key: string]: any;

See the commented line in loggerPlugin.ts in the sandbox for where this goes

@glasser glasser self-assigned this Sep 23, 2020
@glasser
Copy link
Member

glasser commented Sep 23, 2020

Sorry about that! Early in the v2.18 development process I was using ApolloServerPlugin with the Dispatcher class that requires this declaration but I stopped doing that. I'll get a v2.18.1 out ASAP that drops that extends declaration. Wish I had done this before release — it's the only reason that __internal_plugin_id__ is a function!

glasser added a commit that referenced this issue Sep 23, 2020
This `extends` declaration was added during the development of v2.18.0 for
reasons that turned out to be unnecessary. The declaration makes some
valid plugin implementation no longer type-check, so I'm removing it.

Fixes #4585.
@brgaulin
Copy link
Author

haha, sounds good mate! No worries on it, that was a massive upgrade it looks like, with splitting things out and such. It happens =) I'm glad to hear it wasn't intentional, as we would have some re-thinking to do if it was.

Thanks!

@glasser
Copy link
Member

glasser commented Sep 23, 2020

See #4591.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 20, 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

No branches or pull requests

2 participants