Skip to content

Type incompatibility with ApolloServer #9

@carrithers

Description

@carrithers

I get the following type error when attempting to pass an ApolloServer instance into the expressMiddleware function.

No overload matches this call.
  Overload 1 of 2, '(server: ApolloServer<BaseContext>, options?: ExpressMiddlewareOptions<BaseContext> | undefined): RequestHandler<...>', gave the following error.
    Argument of type 'ApolloServer<ServerContext>' is not assignable to parameter of type 'ApolloServer<BaseContext>'.
      Types have separate declarations of a private property 'internals'.
  Overload 2 of 2, '(server: ApolloServer<ServerContext>, options: WithRequired<ExpressMiddlewareOptions<ServerContext>, "context">): RequestHandler<...>', gave the following error.
    Argument of type 'import("/Users/alex/dev/pfl/otb/node_modules/.pnpm/@apollo+server@4.12.0_encoding@0.1.13_graphql@16.11.0/node_modules/@apollo/server/dist/esm/ApolloServer", { with: { "resolution-mode": "import" } }).ApolloServer<import("/Users/alex/dev/pfl/otb/services/api/src/types", { with: { "resolution-mode": "import" } }).Serv...' is not assignable to parameter of type 'import("/Users/alex/dev/pfl/otb/node_modules/.pnpm/@apollo+server@4.12.0_encoding@0.1.13_graphql@16.11.0/node_modules/@apollo/server/dist/cjs/ApolloServer").ApolloServer<import("/Users/alex/dev/pfl/otb/services/api/src/types", { with: { "resolution-mode": "import" } }).ServerContext>'.
      Types have separate declarations of a private property 'internals'.

I followed the setup procedure described in the docs:

import { ApolloServer } from '@apollo/server';
import { expressMiddleware } from '@as-integrations/express5';
import { ApolloServerPluginDrainHttpServer } from '@apollo/server/plugin/drainHttpServer';
import express from 'express';

const app = express();
const httpServer = http.createServer(app);

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

await server.start();

app.use(cors(), express.json({ limit: '50mb' }));

app.use(
  '/graphql',
  // Cannot pass `server` to `expressMiddleware` - No overload matches this call.
  expressMiddleware(server, {
    context: async ({ req }) => {},
  })
);

I am able to build using the // @ts-expect-error directive and it seems to run fine. I am using ApolloServer@^4.12.0 and @as-integrations/express5@^1.0.0. I wonder if this has something to do with ESM and CommonJS?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions