express-graphql is not a functioon

"本文档介绍了在Node.js环境中使用Express-GraphQL时遇到的`graphqlHTTP is not a function`错误的原因和解决方案。问题源于对库的引用方式不正确。通过调整导入语句,将`const { graphqlHTTP }
摘要由CSDN通过智能技术生成

项目场景:

学习使用express-graphql


问题描述

node执行时报错

TypeError: graphqlHTTP is not a function
    at Object.<anonymous> (F:\feiq\H52203STEP3\react\server\01-helloworld.js:14:21)
    at Module._compile (node:internal/modules/cjs/loader:1105:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1159:10)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:77:12)
    at node:internal/main/run_main_module:17:47

原因分析

例如:express-graphql 的引用方式错误


解决方案:

改变引用方式

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

// 请注意,它使用的解构等效于:

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

最终代码,hello-world

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

// const { graphqlHTTP } = require('express-graphql');
// 请注意,它使用的解构等效于:

// const graphqlHTTP = require('express-graphql').graphqlHTTP;
var schema = buildSchema(`
  type Query {
    hello: String
  }
`);

var root = { hello: () => 'Hello world!' };

var app = express();
app.use('/graphql', graphqlHTTP({
    schema: schema,
    rootValue: root,
    graphiql: true,
}));

app.listen(3000, () => console.log('Now browse to localhost:3000/graphql'));

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值