ADVERTISEMENT

graphqlHTTP is not a function

Look at the documentation:

const { graphqlHTTP } = require('express-graphql');

Note that it uses destructuring equivalent to:

const graphqlHTTP = require('express-graphql').graphqlHTTP;

require('express-graphql') returns an object with a property called graphqlHTTP that is the function you want to call.

You're trying to call the object itself as if it was a function.


Quentin's answer was on spot. Apparently the npm documentation was updated but some of the tutorials on YouTube were not. That's why there's a certain degree of confusion for learners like myself. There are still outdated versions of the code like

This one: https://github.com/iamshaunjp/graphql-playlist/blob/lesson-36/server/app.js

This one: https://github.com/WebDevSimplified/Learn-GraphQL/blob/master/server.js

Or this one: https://github.com/bradtraversy/customerbase/blob/master/server.js

They should all be updated to

const { graphqlHTTP } = require('express-graphql');

and then

app.use('/graphql', graphqlHTTP({
    schema:schema,
    graphiql:true
}));

Just to make it more clear:

Before "express-graphql" was returning a direct function or a class with the function and we could assign it to any variable like graphqlServer

const graphqlServer = require('express-graphql');

Now, it returns the whole object that has a function inside it named "graphqlHTTP". hence the code should be exactly

const { graphqlHTTP } = require('express-graphql');

and to make a connection,

app.use('/graphql', graphqlHTTP({
    // your config
}));