这是我简单的graphql express应用程序

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!");
});

当我运行它时,我会出现以下错误:

 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

我该怎么修?提前谢谢!

推荐答案

看看documentation:

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

请注意,它使用的解构相当于:

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

require('express-graphql')返回一个object,其中property被称为graphqlHTTP,这是您想要调用的函数.

你试图调用对象本身,就好像它是一个函数一样.

Node.js相关问答推荐

try 使用Puppeteer抓取Twitter时数组空

我的位置也移动时左右拖动谷歌 map

无法使用NPM安装REDUX和DATEPPICER

Sequelize、postgres和posgis:在n°;公里

在函数上执行 toString 的Typescript 会产生奇怪的字符 (path_1, (0, Promise.writeFile))

如何使用Next.js/Node/TS正确地上传至S3

如何设置 Puppeteer Select 器的唯一性?

npm install 在 Mac 上的 Node-gyp 构建错误

FirebaseCloudMessaging : PlatformException (PlatformException(null-error, Host platform returned null value for non-null return value., null, null))

使用 .pipe(res) 向客户端发送音频不允许您搜索?

我怎样才能让`git`失败而不是要求提供凭据

后端位于 Docker 容器中时的 SvelteKit SSR fetch()

在 Passport 策略回调中获取请求对象

如何使用适用于 Node.js 的 AWS 开发工具包将 Amazon S3 中的所有对象从一个前缀复制/移动到另一个前缀

使用 Node.JS,如何按时间顺序获取文件列表?

使用 NodeJS 将新对象附加到 DynamoDB 中的 JSON 数组

node.js 示例

node.js 异步库

promise 回调返回promise

如何阻止 babel 将this转换为undefined(并插入use strict)