You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

ES5转ES6后GraphQL代码导出异常:_graphql2.default不是构造函数

解决_graphql2.default is not a constructor错误

嘿,我一眼就瞅出问题所在啦!这个报错的核心原因是你在ES6里导入GraphQLList的方式不对,咱们一步步来修正:

1. 错误的导入方式是问题根源

在你的ES5代码里,你是从graphql模块中提取特定属性GraphQLList:

var GraphQLList = require('graphql').GraphQLList;

但到了ES6,你用了默认导入import GraphQLList from 'graphql',这时候GraphQLList拿到的是graphql模块的默认导出(根本不是你需要的那个构造函数),所以当你执行new GraphQLList(ConfigType)时,自然会报错说它不是构造函数。

正确的ES6导入写法应该用命名导入:

import { GraphQLList } from 'graphql'

2. 优化export default的写法(非报错直接原因,但更规范)

你当前的export default queryType = { ... }写法虽然能运行,但可读性不强,建议改成更清晰的形式:要么直接导出对象字面量,要么先定义变量再导出。

修正后的完整ES6代码

import { GraphQLList } from 'graphql'
import ConfigModel from '../../models/config'
import { configType as ConfigType } from '../types/config'

// Query
export default {
  type: new GraphQLList(ConfigType),
  description: "The configuration for the home 'page'",
  resolve: function () {
    const config = ConfigModel.find()
    if (!config) {
      throw new Error('Error')
    }
    return config
  }
}

这样修改后,那个构造函数的错误就会消失啦~

内容的提问来源于stack exchange,提问作者Alex Foxleigh

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 08:16:36