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

Add named export #5526

Closed
wants to merge 1 commit into from
Closed

Add named export #5526

wants to merge 1 commit into from

Conversation

DaSchTour
Copy link

To avoid issues with default exports like
apollo_server_plugin_response_cache_1.default is not a function

To avoid issues with default exports like
`apollo_server_plugin_response_cache_1.default is not a function`
@apollo-cla
Copy link

@DaSchTour: 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/

@glasser
Copy link
Member

glasser commented Jul 21, 2021

Is there a general problem where default exports don't work for you? It's a common pattern in npm packages, and we use it in a few other places in this repo, so just making this one change doesn't make too much sense. How are you writing your import/require such that it doesn't work?

@DaSchTour
Copy link
Author

DaSchTour commented Jul 22, 2021

@glasser I repeatedly had problems with default exports in different projects. I see this as a anti-pattern in JS development. In this case I had the issue when using the apollo-server-plugin-response-cache in a nest.js project within an nx workspace. I finally solved it by using import * as responseCachePlugin from 'apollo-server-plugin-response-cache'; instead of import responseCachePlugin from 'apollo-server-plugin-response-cache'; and adding it like this plugins: [responseCachePlugin as any], instead of plugins: [responseCachePlugin()], into the plugin. Although I can't really verify if it works. I don't see that the cache is really used, but I don't know if it's because of the import or something else.

@DaSchTour
Copy link
Author

DaSchTour commented Jul 22, 2021

Funny thing is, that plugins: [(responseCachePlugin as any)()], also works (doesn't throw any error). But I don't see any requests beeing cached.

I also have cacheControl set. But that also doesn't seam to do anything. NestJS and Apollo doesn't seam to really play well when it comes to caching 😞

@@ -1,3 +1,4 @@
import plugin from './ApolloServerPluginResponseCache';
export default plugin;
export { plugin as ApolloServerPluginResponseCache };
module.exports = plugin;
Copy link
Member

Choose a reason for hiding this comment

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

Actually, there is something weird here. Why does this file have both export default and module.exports =? That's the problem, which I'll fix.

glasser added a commit that referenced this pull request Jul 23, 2021
For some reason, these plugins defined their exports both using `export
default` and `module.exports =`. In practice this means that the latter
overrides the former or something, which in some contexts causes
problems (see #5526).
@glasser glasser added this to the MM-2021-07 milestone Jul 23, 2021
glasser added a commit that referenced this pull request Jul 23, 2021
For some reason, these plugins defined their exports both using `export
default` and `module.exports =`. In practice this means that the latter
overrides the former or something, which in some contexts causes
problems (see #5526).
@glasser
Copy link
Member

glasser commented Jul 23, 2021

I suspect that #5542 fixes the issue. While I agree that named exports are generally nicer than defaults, I think "you can use either default or a name to get the same thing" is worse than either choice so for these particular packages I'm going to leave them as the defaults.

As far as getting caching working goes, are you seeing cache-control response headers set? A post in GH discussions or community.apollographql.com might be a good place to get help with that too.

@glasser glasser closed this Jul 23, 2021
glasser added a commit that referenced this pull request Jul 23, 2021
For some reason, these plugins defined their exports both using `export
default` and `module.exports =`. In practice this means that the latter
overrides the former or something, which in some contexts causes
problems (see #5526).
@DaSchTour
Copy link
Author

I'm not sure if you saw, that it's possible to do very strange things with this default export and it only works with typecasting, because the typings do not match the import. That's very bad developer experience. And this is also one of the few apollo packages that have a default export.

@hwillson hwillson removed this from the MM-2021-07 milestone Jul 29, 2021
@glasser
Copy link
Member

glasser commented Aug 2, 2021

@DaSchTour If the "very strange things" still occur in apollo-server-plugin-response-cache 3.1.0 or newer, let me know. Otherwise I'll assume it's just an artifact of the weird line removed in #5542.

@DaSchTour
Copy link
Author

@glasser following the documentation using 3.1.1 I get this error

 TS2322: Type '(configService: ConfigService) => { debug: boolean; tracing: boolean; playground: boolean; introspection: boolean; autoSchemaFile: string; cacheControl: { defaultMaxAge: number; }; cache: RedisCache | InMemoryLRUCache<...>; plugins: ApolloServerPlugin<...>[]; context: ({ req, res }: any) => { ...; }; }' is not assignable to type '(...args: any[]) => GqlModuleOptions | Promise<GqlModuleOptions>'.
  Type '{ debug: boolean; tracing: boolean; playground: boolean; introspection: boolean; autoSchemaFile: string; cacheControl: { defaultMaxAge: number; }; cache: RedisCache | InMemoryLRUCache<string>; plugins: ApolloServerPlugin<...>[]; context: ({ req, res }: any) => { ...; }; }' is not assignable to type 'GqlModuleOptions | Promise<GqlModuleOptions>'.
    Type '{ debug: boolean; tracing: boolean; playground: boolean; introspection: boolean; autoSchemaFile: string; cacheControl: { defaultMaxAge: number; }; cache: RedisCache | InMemoryLRUCache<string>; plugins: ApolloServerPlugin<...>[]; context: ({ req, res }: any) => { ...; }; }' is not assignable to type 'GqlModuleOptions'.
      Types of property 'plugins' are incompatible.
        Type 'ApolloServerPlugin<BaseContext>[]' is not assignable to type 'PluginDefinition[]'.
          Type 'ApolloServerPlugin<BaseContext>' is not assignable to type 'PluginDefinition'.
            Type 'import("/…/node_modules/apollo-server-plugin-response-cache/node_modules/apollo-server-plugin-base/dist/index").ApolloServerPlugin<import("/…/node_modules/apollo-server-plugin-response-cache/node_modules/apollo-server-types/dist/index").BaseContext>' is not assignable to type 'import("/…/node_modules/apollo-server-plugin-base/dist/index").ApolloServerPlugin<import("/…/node_modules/apollo-server-types/dist/index").BaseContext>'.
              Types of property 'requestDidStart' are incompatible.
                Type '(requestContext: GraphQLRequestContext<BaseContext>) => Promise<void | GraphQLRequestListener<BaseContext>>' is not assignable to type '(requestContext: GraphQLRequestContext<BaseContext>) => void | GraphQLRequestListener<BaseContext>'.
                  Types of parameters 'requestContext' and 'requestContext' are incompatible.
                    Type 'import("/…/node_modules/apollo-server-types/dist/index").GraphQLRequestContext<import("/…/node_modules/apollo-server-types/dist/index").BaseContext>' is not assignable to type 'import("/…/node_modules/apollo-server-plugin-response-cache/node_modules/apollo-server-types/dist/index").GraphQLRequestContext<import("/…/node_modules/apollo-server-plugin-response-cache/node_modules/apollo-server-types/dist/index").BaseContext>'.
                      The types of 'request.http.headers' are incompatible between these types.
                        Type 'import("/…/node_modules/apollo-server-env/dist/fetch").Headers' is not assignable to type 'import("/…/node_modules/apollo-server-plugin-response-cache/node_modules/apollo-server-env/dist/fetch").Headers'.

@glasser
Copy link
Member

glasser commented Aug 3, 2021

This isn't about named exports. You have two copies of apollo-server-env installed (one nested under apollo-server-plugin-response-cache). We had to make an incompatible change to that package in v3.1.1 as a bug fix (possibly doing this in a patch release was bad, but it was for compatibility with changes that @types/node-fetch made in a patch release). Can you make sure all of your packages are at the latest version (or share your package-lock.json) and perhaps run npm dedupe?

@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

4 participants