簡體   English   中英

將代碼從ES5轉換為ES6並導出無法按預期進行

[英]Converting code from ES5 to ES6 and export is not working as expected

嘗試將一些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
  }
}

我猜編譯器希望此代碼是一個類,但僅應該是對象文字,我在做什么錯?

您本質上轉換了此ES5行:

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

對此:

import GraphQLList from 'graphql'

您應該可以在這里看到錯誤。

在ES6代碼中, GraphQLList將是一個對象, 其中包含該模塊的所有導出,包括一個名為GraphQLList

因此,您可以從此更改ES6代碼:

new GraphQLList(ConfigType)

對此:

new GraphQLList.GraphQLList(ConfigType)

或者,如評論員所述,只需執行以下操作:

import { GraphQLList } from 'graphql'

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM