graphqlHTTP 不是函数 [英] graphqlHTTP is not a function

查看:20
本文介绍了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!

解决方案

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.

这篇关于graphqlHTTP 不是函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆