graphqlHTTP is not a function
Asked Answered
A

9

76

Here is my simple graphql express app

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

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

app.listen(4000, () => {
    console.log("listening for request!");
});

I'm getting the following errors when I run it:

 graphqlHTTP({
    ^

TypeError: graphqlHTTP is not a function
    at Object.<anonymous> (D:\PersonalProjects\GraphQL\server\app.js:7:5)
    at Module._compile (internal/modules/cjs/loader.js:1138:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1158:10)
    at Module.load (internal/modules/cjs/loader.js:986:32)
    at Function.Module._load (internal/modules/cjs/loader.js:879:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)  
    at internal/main/run_main_module.js:17:47

How can I fix it

Apanage answered 6/7, 2020 at 17:3 Comment(0)
P
232

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.

Papillose answered 6/7, 2020 at 17:6 Comment(1)
Thanks. This was really helpful. I hit this error while following a GraphQL in 40 minutes video tutorial dated Mar 2, 2019.Schrimsher
L
32

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
}));
Lignocellulose answered 7/7, 2020 at 18:38 Comment(0)
O
9

You can use

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

app.use('/graphql', gqlHTTP.graphqlHTTP({
 // something
}))
Opinionative answered 11/9, 2020 at 8:47 Comment(0)
B
9

This code was written with an earlier version of express-graphql.

Prior to v0.10.0, you could use

var graphqlHTTP = require('express-graphql');

After v0.10.0, you need to use

var { graphqlHTTP } = require('express-graphql');
Bigham answered 12/2, 2022 at 15:21 Comment(0)
H
7

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
})); 
Hendershot answered 13/9, 2020 at 17:18 Comment(0)
F
3

Had the same issue solved by the above answers.

For anyone wondering, express-graphql version 0.10.0 is where the relevant change started so you can check your package.json express-graphql dependency version number.

https://www.npmjs.com/package/express-graphql/v/0.9.0
https://www.npmjs.com/package/express-graphql/v/0.10.0

Fernanda answered 17/7, 2020 at 16:31 Comment(0)
B
3

Simple Setup:

DO NOT USE YOUR OWN PARAMETER STRICTLY USE { graphqlHTTP }!!!!

Just mount express-graphql as a route handler:

const express = require('express');
const { graphqlHTTP } = require('express-graphql');
 
const app = express();
 
app.use(
  '/graphql',
  graphqlHTTP({
    schema: MyGraphQLSchema,
    graphiql: true,
  }),
);
 
app.listen(4000, () => {
    console.log('Server is running on port 4K')
);
Brockbrocken answered 11/11, 2020 at 12:42 Comment(0)
C
2

Use this approach:

const { graphqlHTTP } = require('express-graphql');
Colombo answered 22/2, 2023 at 10:26 Comment(0)
A
0

As Quentin mentioned, you are trying to call the object itself as if it were a function. You could update the following line to use destructuring: i.e. Update --> const graphqlHTTP = require('express-graphql'); to --> const { graphqlHTTP } = require('express-graphql');

Apollus answered 27/10, 2022 at 18:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.