apollographql / apollo-server

🌍  Spec-compliant and production ready JavaScript GraphQL server that lets you develop in a schema-first way. Built for Express, Connect, Hapi, Koa, and more.
https://www.apollographql.com/docs/apollo-server/
MIT License
13.76k stars 2.03k forks source link

ESM TypeError: resolvable is not a function #7895

Closed macrozone closed 2 months ago

macrozone commented 3 months ago

Issue Description

when switching to ESM, apollo server no longer starts. you get this error:

TypeError: resolvable is not a function
    at ApolloServer._start (/path/to/project/node_modules/@apollo/server/dist/esm/ApolloServer.js:127:25)
    at ApolloServer.start (/path/to/project/node_modules/@apollo/server/dist/esm/ApolloServer.js:115:27)

Link to Reproduction

not yet

Reproduction Steps

  1. install "@apollo/server": "^4.10.4",
  2. set "type": "module" in package.json
  3. start apollo server

import { ApolloServer } from "@apollo/server";

 const server = new ApolloServer<{}>({
    schema,
    plugins: [ApolloServerPluginDrainHttpServer({ httpServer })],
  });

 await server.start();
macrozone commented 3 months ago

looks like a "left-pad" situation. https://github.com/josephg/resolvable is a 17 liner and has not been updated in 3 years.

come on, just don't use dependencies like that

ivosabev commented 3 months ago

I use the following patch-package patch

diff --git a/node_modules/@apollo/server/dist/esm/ApolloServer.js b/node_modules/@apollo/server/dist/esm/ApolloServer.js
index b716f63..25370aa 100644
--- a/node_modules/@apollo/server/dist/esm/ApolloServer.js
+++ b/node_modules/@apollo/server/dist/esm/ApolloServer.js
@@ -1,7 +1,7 @@
 import { isNodeLike } from '@apollo/utils.isnodelike';
 import { InMemoryLRUCache, PrefixingKeyValueCache, } from '@apollo/utils.keyvaluecache';
 import { makeExecutableSchema } from '@graphql-tools/schema';
-import resolvable from '@josephg/resolvable';
+import resolvable from '@josephg/resolvable/index.js';
 import { GraphQLError, assertValidSchema, print, printSchema, } from 'graphql';
 import loglevel from 'loglevel';
 import Negotiator from 'negotiator';
macrozone commented 3 months ago

I use the following patch-package patch

diff --git a/node_modules/@apollo/server/dist/esm/ApolloServer.js b/node_modules/@apollo/server/dist/esm/ApolloServer.js
index b716f63..25370aa 100644
--- a/node_modules/@apollo/server/dist/esm/ApolloServer.js
+++ b/node_modules/@apollo/server/dist/esm/ApolloServer.js
@@ -1,7 +1,7 @@
 import { isNodeLike } from '@apollo/utils.isnodelike';
 import { InMemoryLRUCache, PrefixingKeyValueCache, } from '@apollo/utils.keyvaluecache';
 import { makeExecutableSchema } from '@graphql-tools/schema';
-import resolvable from '@josephg/resolvable';
+import resolvable from '@josephg/resolvable/index.js';
 import { GraphQLError, assertValidSchema, print, printSchema, } from 'graphql';
 import loglevel from 'loglevel';
 import Negotiator from 'negotiator';

thank you, this indeed fixes the issue.

its weird that this happens though, shouldn't node respect the main field as fallback?

pthieu commented 3 months ago

fails on node 20 and above, works on 18 i didn't test any other versions

trevor-scheer commented 2 months ago

I haven't spent much time trying to understand the issue, the JS file appears to be ESM compatible. Maybe the package.json from the resolvable dependency needs a module field? In any case, I opened a PR to inline the dependency which should resolve it. Could someone with a reproduction handy please test it out? There are builds on the PR you can install via the Codesandbox CI comment. Thanks for reporting!

macrozone commented 2 months ago

@trevor-scheer I can confirm that your MR works

trevor-scheer commented 2 months ago

Fixed via #7900, should be available in @apollo/server@4.10.5 momentarily. Thanks for reporting!

github-actions[bot] commented 1 month ago

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. For general questions, we recommend using StackOverflow or our discord server.