【问题标题】:Transpiled GraphQL with Babel is throwing error "Cannot call class as function"使用 Babel 转换的 GraphQL 抛出错误“无法将类调用为函数”
【发布时间】:2017-08-02 19:03:48
【问题描述】:

我正在尝试运行 GraphQL 服务器。我在 GraphQL 中有简单的架构

import {
  GraphQLObjectType,
  GraphQLInt,
  GraphQLString,
  GraphQLList,
  GraphQLSchema
} from 'graphql'
import db from './models'

const user = new GraphQLObjectType({
  name: "user",
  description: 'This represents a user',
  fields: () => {
    return {
        id: {
            type: GraphQLInt,
            resolve(user) {
                return user.id
            }
        },
        firstName: {
            type: GraphQLString,
            resole(user) {
                return user.firstName
            }
        },
        lastName: {
            type: GraphQLString,
            resole(user) {
                return user.lastName
            }
        },
        email: {
            type: GraphQLString,
            resole(user) {
                return user.email
            }
        },
        createdAt: {
            type: GraphQLString,
            resole(user) {
                return user.createdAt
            }
        },
        updatedAt: {
            type: GraphQLString,
            resole(user) => {
                return user.updatedAt
            }
        }
      }
     }
 })


 const Query = new GraphQLObjectType({
   name: 'Query',
   description: 'This is root Query',
   fields: () => {
      return {
        users: {
            type: GraphQLList(user),
            args: {
                id: {
                    type: GraphQLInt
                },
                email: {
                    type: GraphQLString
                }
            },
            resolve(root, args) {
                return db.user.findAll({where: args})
            }
          }
       }
    }
 })

 const Schema = new GraphQLSchema({
  query: Query
 })

 export default Schema

我用 babel 将它转译成 ES5,但每次我尝试用 express 运行它时

import GraphHTTP from 'express-graphql'
import Schema from './schema'

app.use('/grapql', GraphHTTP({
  schema: Schema,
  pretty: true,
  graphiql: true
}))

我收到这个错误

 \node_modules\graphql\type\definition.js:41
 function _classCallCheck(instance, Constructor) { if (!instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }                                                             
TypeError: Cannot call a class as a function

如果我有一些打字错误但我没有找到任何东西,我会一次又一次地检查它。

【问题讨论】:

    标签: express graphql babeljs


    【解决方案1】:

    type: new GraphQLList(user)代替type: GraphQLList(user)

    GraphQLListclass,您必须创建它的实例并使用它,但您已将其作为函数调用。

     const Query = new GraphQLObjectType({
       name: 'Query',
       description: 'This is root Query',
       fields: () => {
          return {
            users: {
                type: new GraphQLList(user),   
                args: {
                    id: {
                        type: GraphQLInt
                    },
                    email: {
                        type: GraphQLString
                    }
                },
                resolve(root, args) {
                    return db.user.findAll({where: args})
                }
              }
           }
        }
     })
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-23
      • 2016-10-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-07
      • 1970-01-01
      • 2023-01-25
      相关资源
      最近更新 更多