Ene*_*kes 1 express babeljs graphql
我正在尝试运行 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
Run Code Online (Sandbox Code Playgroud)
我用 babel 把它转译成 ES5,但是每次当我尝试用 express 运行它时
import GraphHTTP from 'express-graphql'
import Schema from './schema'
app.use('/grapql', GraphHTTP({
schema: Schema,
pretty: true,
graphiql: true
}))
Run Code Online (Sandbox Code Playgroud)
我收到此错误
\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
Run Code Online (Sandbox Code Playgroud)
如果我有一些打字错误,我会一遍又一遍地检查它,但我没有找到任何东西。
而不是type: GraphQLList(user)
使用type: new GraphQLList(user)
GraphQLList
是 aclass
并且您必须创建它的实例并使用它,但是您已将其作为函数调用。
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})
}
}
}
}
})
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
428 次 |
最近记录: |