graphql-constraint-directive icon indicating copy to clipboard operation
graphql-constraint-directive copied to clipboard

Module not found: Can't resolve 'apollo-server-errors'

Open snikch opened this issue 2 years ago • 9 comments
trafficstars

When importing this package into a next.js app, I get the following error. I believe because https://github.com/confuser/graphql-constraint-directive/blob/master/index.js#L135 attempts to require('apollo-server-errors') which is not a dependency of this package.

../../.yarn/__virtual__/graphql-constraint-directive-virtual-74b5cc573a/0/cache/graphql-constraint-directive-npm-4.1.2-695ac942e4-d61673215b.zip/node_modules/graphql-constraint-directive/index.js:135:39
Module not found: Can't resolve 'apollo-server-errors'

https://nextjs.org/docs/messages/module-not-found

Import trace for requested module:
../../packages/graphql/schemas/scalars/index.ts
../../packages/graphql/index.ts
./pages/api/graphql.ts

I'm not really sure what an appropriate resolution here is, short of moving the individual library support to alternative packages.

snikch avatar Mar 09 '23 07:03 snikch

Hmm, this may be caused by presence of import {PluginDefinition} from "apollo-server-core"; in index.d.ts. We can probably remove this import and make types for Apollo3 plugin weaker a bit. Better solution might be to move Apollo3 plugin to separate js and d.ts file as apollo4 plugin, but it is a breaking change, so I'd like @confuser to help decide what to do.

velias avatar Mar 09 '23 09:03 velias

@apollo/server is included in the package.json so I don't think this is the issue. I created a fork and simply removed the createApolloQueryValidationPlugin function and it is working successfully, so I believe it's definitely the issue that require('apollo-server-errors') is not listed as a dependency.

snikch avatar Mar 09 '23 19:03 snikch

I do know nothing about next.js framework, so it is hard to determine. Is it and your project TypeScript or pure JavaScript? In this module's package.json, apollo-server-core is in devDependency, and apollo-server-errors is its transitive dependency. It is intentionally not in dependencies not to be included in projects not using Apollo3. But devDependencies from modules are typically not included. In the index.js, this require('apollo-server-errors') is intentionally in the code and not at the beginning, so should be used only if this code is used, but I'm not sure, maybe not. In index.d.ts it is imported at the beginning, so always. So I'd expect your project uses TS, and that import in types file is the problem.

Anyway, as I wrote before, for me the best solution should be to extract Apollo 3 plugin (and maybe even Envelop plugin) to separate files. But this is a breaking change, so I'd like to know @confuser opinion first, before I start to implement it.

velias avatar Mar 10 '23 09:03 velias

Agree that the solution would be to have separate plugins!

snikch avatar Mar 12 '23 22:03 snikch

Workaround:

Create a missing_apollo_types.d.ts (name does not matter) file in your project with the following content:

declare module 'apollo-server-core' {
  interface PluginDefinition {
    _: unknown
  }
}

davidruisinger avatar Mar 30 '23 08:03 davidruisinger

@velias I think the best solve which is a larger change is to create a monorepo with a core folder containing the main business logic and then each plugin being its own package. Otherwise it'll mean installing a bunch of dependencies that aren't needed just to cover multiple use cases

confuser avatar May 07 '23 08:05 confuser

Agree, this is indeed the right solution. But it is a bit larger change ;-)

velias avatar May 10 '23 07:05 velias

Given that Apollo 3 is not supported anymore it could be possible to simply remove Apollo 3 plugin at all with all its dependencies.

Docs (https://www.apollographql.com/docs/apollo-server/previous-versions) says:

Apollo Server 3 is deprecated and will transition to end-of-life on October 22nd, 2023.

lkrzyzanek avatar Jun 28 '23 07:06 lkrzyzanek

I've opened a PR here to address this. The fix is quite small, simply moving an import around to prevent unnecessary import (and attempted typecheck) on apollo-server-errors when using the apollo4 import.

damienwebdev avatar Jul 04 '24 14:07 damienwebdev