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

Unable to use registerServer with Typescript and Apollo Server 2 #1140

Closed
majelbstoat opened this issue Jun 5, 2018 · 14 comments
Closed

Unable to use registerServer with Typescript and Apollo Server 2 #1140

majelbstoat opened this issue Jun 5, 2018 · 14 comments

Comments

@majelbstoat
Copy link
Contributor

majelbstoat commented Jun 5, 2018

Following the documentation for Apollo Server 2, there are errors when using registerServer and Typescript

import { ApolloServer } from 'apollo-server'
import express from 'express'
import { registerServer } from 'apollo-server-express'

const server = new ApolloServer({ // config })
const app = express()
registerServer({ server, app })

Gives the following error:

 [tsl] ERROR in /app/server/index.ts(62,16)
       TS2345: Argument of type '{ server: ApolloServer; app: Express; }' is not assignable to parameter of type 'ServerRegistration'.
   Types of property 'server' are incompatible.
     Type 'ApolloServer' is not assignable to type 'ApolloServerBase<Request>'.
       Types have separate declarations of a private property 'schema'.

apollo-server 2.0.0-beta4
apollo-server-express 2.0.0-beta3

Temporary workaround is:

const server = new ApolloServer({}) as any
@evans
Copy link
Contributor

evans commented Jun 6, 2018

Super odd, we have that in the test, which are typescript, here. What's inside of your tsconfig?

Would love a PR with a failing test inside of the apollo-server package if you can manage!

@majelbstoat
Copy link
Contributor Author

Yeah, it's very weird, because clicking into the definition takes me directly to ApolloServerBase 🤔.

Full tsconfig.json:

{
  "compileOnSave": false,
  "compilerOptions": {
    "declaration": false,
    "allowSyntheticDefaultImports": true,
    "esModuleInterop": true,
    "lib": ["es2015", "es2016"],
    "module": "commonjs",
    "moduleResolution": "node",
    "noFallthroughCasesInSwitch": true,
    "noUnusedLocals": true,
    "noUnusedParameters": true,
    "outDir": "dist/",
    "skipLibCheck": true,
    "sourceMap": true,
    "strict": true,
    "target": "es5",
    "typeRoots": ["./node_modules/@types", "./app/@types"]
  },
  "exclude": ["lib/editor"]
}

Typescript 2.9.1.

@evans
Copy link
Contributor

evans commented Jun 13, 2018

This should be fixed after #1161

@evans evans closed this as completed Jun 13, 2018
@timohermans
Copy link

I'm experiencing this issue with ^2.13.0 versions:

TS2345: Argument of type 'typeof ApolloServer' is not assignable to parameter of type 'new (Config: any) => ApolloServerBase'.
  Type 'ApolloServer' is not assignable to type 'ApolloServerBase'.
    Types have separate declarations of a private property 'logger'.

@jhanschoo
Copy link

@timohermans Can you open a separate issue referencing this?

@jabbar86
Copy link

jabbar86 commented Aug 6, 2020

Downgrading to


{
  "apollo-server-express": "2.9.13",
  "apollo-server-testing": "2.9.13
}

works for me.

@sgentile
Copy link

Not sure why downgrading constitutes it being closed - this fails when trying to use Apollo server version 2 - trying to pass express app into applyMiddleware

server.applyMiddleware({ app, path });

@milenkovic
Copy link

Same here.
Versions:

"apollo-server-express": "^2.19.1",<br>
"express": "^4.17.1",

Implementation:

const server = new ApolloServer({ typeDefs });
server.applyMiddleware({ app });
app.listen(port, (err: Error) => {
  if (err) {
    logger.error('something bad happened', err);
  } else {
    logger.info(`server is listening on: ${port}`);
  }
});

Error:

Type 'Express' is not assignable to type 'Application'.
  Types of property 'engine' are incompatible...

@ransfordarthurjr
Copy link

Adding the object keys for applyMiddleware values solved it for me

instead of

...
const expressServer: express.Application = express();
...
const path: string = 'graphql';
apolloServer.applyMiddleware({ expressServer, path });
...

Do this (notice the object key-value pairs in the applyMiddleware object, not the just values)

...
const expressServer: express.Application = express();
...
const path: string = 'graphql';
apolloServer.applyMiddleware({ app: expressServer, path: path });
...

package.json

"apollo-server-express": "^2.19.1",
"express": "^4.17.1"

@atkinchris
Copy link

For me, this was due to mismatched @types/express across my application. I'm using [email protected] and [email protected] (the latest versions of both on npm, at the time of writing).

[email protected] uses @types/[email protected] (introduced in 68043d3). All other packages I'm using, including transitive dependencies of apollo-server-express and my root dependency of @types/express are all using @types/[email protected] (matching the express version).

@loudmouth
Copy link

@atkinchris's solution worked for me.

Thanks for saving the day!

@glasser
Copy link
Member

glasser commented Feb 12, 2021

I'm not sure if this is precisely that issue, but I definitely discovered in our app that some of the express-related DefinitelyTyped (@types) packages made backwards-incompatible changes and I needed to upgrade a bunch of them in parallel instead of just one. (Their deps on each other use very vague version specs, like * or something.)

@rahultrivedi1800
Copy link

For me, this was due to mismatched @types/express across my application. I'm using [email protected] and [email protected] (the latest versions of both on npm, at the time of writing).

[email protected] uses @types/[email protected] (introduced in 68043d3). All other packages I'm using, including transitive dependencies of apollo-server-express and my root dependency of @types/express are all using @types/[email protected] (matching the express version).

Worked for me!

@BenDev12
Copy link

@atkinchris , solution worked. Thanks mate

@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