expressGraphQL is not a function
Asked Answered
N

4

49

I am learning GraphQL for my project using this tutorial: https://www.youtube.com/watch?v=ZQL7tL2S0oQ&ab_channel=WebDevSimplified

and I get the error:

TypeError: expressGraphQL is not a function
at Object.<anonymous>

I have already tried:

  • this solution: graphqlHTTP is not a function - the program crashes all the same with {} parentheses and without them
  • adding a semicolon after various lines

The code for now looks like this:

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

app.use('/graphql', expressGraphQL({
    graphiql: true,
})
)
app.listen(5000., () => console.log('Server Running'))

If I comment out this section:

app.use('/graphql', expressGraphQL({
graphiql: true,
})
)

the code works perfectly fine both with {} parentheses and without them.

Noach answered 31/12, 2020 at 7:36 Comment(0)
O
131

Please replace your expressGraphQL with graphqlHTTP as it was destructured

Use:

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

or

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

This is because a method called graphqlHTTP exist in the express-graphql module and you are destructure with another method name that does not exist in the module

I also noticed that you have a dot after 5000 on the app.listen function.

Overcast answered 31/12, 2020 at 8:48 Comment(6)
I think this question is so popular because this tutorial gives incorrect instructionsPlassey
and +1 also coming from this tutorial (:Alsoran
@fatiu: Very helpful, thanks.Incrocci
I am also coming from the same tutorial. I opened a PR on the author's github repo fixing the issue.Ratio
github.com/ankitamasand/gql-server/pull/3Ratio
best answer! Also, you can use it as a reference expressGraphQL using const { graphqlHTTP: expressGraphQL } = require('express-graphql'); Puzzlement
E
18

Use following as a solution.

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


const app = express();

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

app.listen(4000, () => {
    console.log('Listning');
}) 
Everyplace answered 16/10, 2021 at 7:58 Comment(0)
S
5

Destructure graphqlHTTP from express-graphql

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

app.use("/graphql", graphqlHTTP({ graphiql: true }));
Salpingectomy answered 7/12, 2021 at 17:19 Comment(0)
S
3
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.enter code here

Solo answered 5/6, 2021 at 16:4 Comment(1)
Can anyone explain why this destructuring is needed? I worked with graph before and you didnt have to call .graphqlHTTP. I worked the same way this persons tutorial is stating so I am just curious what changed in the last year or so and why this is now needed?Babbie

© 2022 - 2024 — McMap. All rights reserved.