本文介绍了graphqlHTTP 不是函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我的简单 graphql express 应用
const express = require('express');const graphqlHTTP = require('express-graphql');const app = express();应用程序使用('/graphql',graphqlHTTP({图形:真实,}));app.listen(4000, () => {console.log(监听请求!");});
运行时出现以下错误:
graphqlHTTP({^类型错误:graphqlHTTP 不是函数在对象(D:PersonalProjectsGraphQLserverapp.js:7:5)在 Module._compile (internal/modules/cjs/loader.js:1138:30)在 Object.Module._extensions..js (internal/modules/cjs/loader.js:1158:10)在 Module.load (internal/modules/cjs/loader.js:986:32)在 Function.Module._load (internal/modules/cjs/loader.js:879:14)在 Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)在内部/main/run_main_module.js:17:47
我该如何解决?提前致谢!
解决方案
查看文档:
const { graphqlHTTP } = require('express-graphql');
const graphqlHTTP = require('express-graphql').graphqlHTTP;
require('express-graphql')
返回一个带有 属性 的对象,名为 graphqlHTTP
,它是您要调用的函数.
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:PersonalProjectsGraphQLserverapp.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? Thanks in advance!
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.
这篇关于graphqlHTTP 不是函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!