Unable to get Apollo Server to fire up with Express integration

I’m trying to set up an Apollo GraphQL server via apollo-express-server and I’m running into some issues.

I’ve followed the docs as far as using the integration/middleware is concerned, but I’m running into a few errors when using the following code:

index.js

import express from 'express';
import { ApolloServer, makeExecutableSchema } from 'apollo-server-express';

// GQL Imports
import resolvers from './gql/resolvers';
import typeDefs from './gql/typeDefs.gql';

async function startApolloServer() {

    // Define Apollo and Express server instances!
    const app = express();
    const server = new ApolloServer({
        typeDefs,
        resolvers
    });

    // Fire up the server
    await server.start();

    // Apply middleware
    server.applyMiddleware({ app });

    // Fire up server via Promise after setting port
    const port = process.env.PORT || 4000;
    await new Promise(resolve => app.listen({ port }, resolve));
    console.log(`Server operational at: http://localhost${port}/${server.graphqlPath}`);
    return { server, app };

}

gql/resolvers.js

const resolvers = {
    Query: {
        hello: () => {
            return 'World!'
        }
    }
}

export default resolvers;

gql/typeDefs.gql (using babel-plugin-import-graphql for this)

type Query {
    hello: String
}

Not sure what I’m doing incorrectly, but the errors are as follows:

Argument type {typeDefs, resolvers: {Query: {hello: (function(): string)}}} is not assignable to parameter type ApolloServerExpressConfig (new ApolloServer line)

and

Invalid number of arguments, expected 3..4 in reference to the call to app.listen().

Thanks in advance to anyone who’s able to point me in the right direction!

@czbaker I’d suggest trying things without babel-plugin-import-graphql first, to reduce the potential problem areas. So maybe wrap your typeDefs in a gql call first, then use them.

I tried that, and I think that fixed a bit of the problem, but it’s still telling me that my parameters aren’t acceptable when I try to define the configuration for my new ApolloServer instance.

I’ve got a super basic resolver map:

const resolvers = {
    Query: {
        hello: () => 'world!';
    }
}

I just don’t know if it’s Typescript being weird (I’m not using TS in my project, but I know that Apollo is written with it) or what the case may be.

I did move the type definitions to a JS file using the gql tag and that’s working fine, though.

I think also this problem only seems to exist when I use the async “version” of starting up the server, but when I do it the old way, it works fine. If that’s the case, do I need to do anything else that’s async-specific with the initialization of Express/Apollo together?

What’s the exact error you’re seeing - is it the one you mentioned earlier?

Invalid number of arguments, expected 3…4 in reference to the call to app.listen().

These are the two errors I’m seeing:

Argument type {typeDefs: DocumentNode, resolvers: {Query: {hello(): string}}} is not assignable to parameter type ApolloServerExpressConfig

as well as

Invalid number of arguments, expected 3..4 (when app.listen is called in the Promise)

What version of apollo-server-express are you using? What version of express? Could you put together a small reproduction?

I put together this small CodeSandbox example which I think demonstrates how to use Apollo Server and Express, including the latest updates to apollo-server-express. Perhaps that’s helpful in debugging? Perhaps you could “fork” my example and edit it to reproduce your problem and share something with us that we could look at more closely?

Wishing you luck!

Here’s the current state of what I’ve got going on in a CodeSandbox.

I’ve fixed the bit at the end with Express being called (I think), but I still can’t figure out why it hangs when it tries to initialize the ApolloServer instance. The resolver map looks fine to me, and so do the type definitions (I’m now using a gql tag and dropped the GraphQL import plugin that I was using before.

Anyway, still not sure what it’s angry about, but would love to fix it.

So, I re-wrote stuff entirely in TypeScript and now it’s fine, so I don’t know what the issue was before.

Only weird thing is that the console.log() line never gets called when server initializes, but it works fine otherwise with zero errors.

Oh well. Thanks, folks!