【发布时间】:2018-11-09 10:45:07
【问题描述】:
尝试将一些 GraphQL 代码从 ES5 转换为 ES6 时出现以下错误:
_graphql2.default is not a constructor
这是新的 ES6 代码:
import GraphQLList from 'graphql'
import ConfigModel from '../../models/config'
import { configType as ConfigType } from '../types/config'
// Query
export default queryType = {
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
}
}
ES5 代码如下所示:
var GraphQLList = require('graphql').GraphQLList;
var ConfigModel = require('../../models/config');
var ConfigType = require('../types/config').configType;
// Query
exports.queryType = {
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
}
}
我猜转译器希望这段代码是一个类,但它应该只是一个对象文字,我做错了什么?
【问题讨论】:
-
试试
import { GraphQLList } from 'graphql'(graphql不是构造函数,而且从来不是;你曾经实例化require('graphql').GraphQLList) -
是的,做到了。我能问一下为什么会这样吗?如果我想解构或重命名我的导入,我认为我只需要大括号?为什么一件物品需要它?
-
或者,您可以(并且应该)使用:
import graphql from 'graphql';和new graphql.GraphQLList(...),这正是您之前所做的。
标签: javascript ecmascript-6 graphql babeljs