expressGraphQL不是函数

我正在使用本教程为我的项目学习 GraphQL:https : //www.youtube.com/watch?v= ZQL7tL2S0oQ&ab_channel=WebDevSimplified

我得到错误:

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

我已经尝试过:

  • 这个解决方案:graphqlHTTP 不是一个函数 - 程序在使用 {} 括号和没有它们的情况下都会崩溃
  • 在各行后添加分号

现在的代码如下所示:

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'))

如果我注释掉这一部分:

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

无论使用 {} 括号还是不使用括号,代码都可以正常工作。

回答

请用 graphqlHTTP 替换你的 expressGraphQL,因为它被解构了

用:

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

或者

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

这是因为 express-graphql 模块中存在一个名为 graphqlHTTP 的方法,而您正在使用该模块中不存在的另一个方法名称进行解构

我还注意到在 app.listen 函数上 5000 之后有一个点。

  • I think this question is so popular because [this](https://buddy.works/tutorials/building-graphql-server-using-nodejs-and-express) tutorial gives incorrect instructions

以上是expressGraphQL不是函数的全部内容。
THE END
分享
二维码
< <上一篇
下一篇>>