如何在 GraphQLObjectType 中定义对象字段?
How to define an object field in GraphQLObjectType?
所以我正在尝试在 MongoDB 中创建一个用户集合,并使用 GraphQL 和 mongoose 对其进行查询。
我在路径 'pathToServer\server\models\user.js' 中创建了我的用户模式,它看起来像这样:
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const userSchema = new Schema({
name: {
firstName: String,
lastName: String,
},
email: String,
password: String,
})
module.exports = mongoose.model('User', userSchema);
我已经创建了一个 GraphQL 类型,目前我在路径 'pathToServer\server\schema\types\user.js' 中有它,它看起来像这样:
const graphql = require('graphql');
const {GraphQLObjectType, GraphQLList, GraphQLInt, GraphQLID, GraphQLString, GraphQLSchema, GraphQLNonNull} = graphql;
const UserType = new GraphQLObjectType({
name: 'User',
fields: () => ({
id: {type: GraphQLID},
email: {type: GraphQLString},
name: new GraphQLObjectType({
firstName: {type: GraphQLString},
lastName: {type: GraphQLString}
})
})
});
module.exports = UserType;
最后,我在路径 'pathToServer\server\schema\schema.js' :
中有了带有查询和突变的 GraphQL 模式
const graphql = require('graphql');
const {GraphQLObjectType, GraphQLList, GraphQLInt, GraphQLID, GraphQLString, GraphQLSchema, GraphQLNonNull} = graphql;
const User = require('../models/user');
const UserType = require('./types/user');
const RootQuery = new GraphQLObjectType({
name: 'RootQueryType',
fields: {
user: {
type: UserType,
args: {
id: {
type: GraphQLID
}
},
resolve(parent, args){
return User.findById(args.id);
}
},
users: {
type: new GraphQLList(UserType),
resolve(parent, args){
return User.find({})
}
}
}
})
const Mutation = new GraphQLObjectType({
name: 'Mutation',
fields: {
addUser: {
type: UserType,
args: {
name: {
firstName: {type: new GraphQLNonNull(GraphQLString)},
lastName: {type: new GraphQLNonNull(GraphQLString)}
},
email: {type: new GraphQLNonNull(GraphQLString)},
password: {type: new GraphQLNonNull(GraphQLString)}
},
resolve(parent, args){
let user = new User({
name: args.name,
email: args.email,
password: args.password,
});
return user.save();
}
}
}
})
module.exports = new GraphQLSchema({
query: RootQuery,
mutation: Mutation
})
问题是每当我启动服务器时它都会抛出一个错误:
Error: Must provide name.
at invariant (pathToServer\server\node_modules\graphql\jsutils\invariant.js:19:11)
at new GraphQLObjectType (pathToServer\server\node_modules\graphql\type\definition.js:499:66)
at fields (pathToServer\server\schema\types\user.js:10:15)
at resolveThunk (pathToServer\server\node_modules\graphql\type\definition.js:370:40)
at defineFieldMap (pathToServer\server\node_modules\graphql\type\definition.js:532:18)
at GraphQLObjectType.getFields (pathToServer\server\node_modules\graphql\type\definition.js:506:44)
at typeMapReducer (pathToServer\server\node_modules\graphql\type\schema.js:232:38)
at pathToServer\server\node_modules\graphql\type\schema.js:239:20
at Array.forEach (<anonymous>)
at typeMapReducer (pathToServer\server\node_modules\graphql\type\schema.js:232:51)
at Array.reduce (<anonymous>)
at new GraphQLSchema (pathToServer\server\node_modules\graphql\type\schema.js:122:28)
at Object.<anonymous> (pathToServer\server\schema\schema.js:79:18)
at Module._compile (module.js:652:30)
at Object.Module._extensions..js (module.js:663:10)
at Module.load (module.js:565:32)
也许我没有正确定义名称字段?我认为它可能会受到不同的对待,因为我模型中的名称字段是一个包含字段 firstName 和 lastName 的对象。
你能看一下吗?
提前致谢!
编辑
我从
编辑了用户类型
const UserType = new GraphQLObjectType({
name: 'User',
fields: () => ({
id: {type: GraphQLID},
email: {type: GraphQLString},
name: new GraphQLObjectType({
firstName: {type: GraphQLString},
lastName: {type: GraphQLString}
})
})
});
至
const UserType = new GraphQLObjectType({
name: 'User',
fields: () => ({
id: {type: GraphQLID},
email: {type: GraphQLString},
name: {
firstName: {type: GraphQLString},
lastName: {type: GraphQLString}
}
})
});
现在服务器启动了,但它在 graphiql 中给我这个错误:
{
"errors": [
{
"message": "The type of User.name must be Output Type but got: undefined.\n\nThe type of Mutation.addUser(name:) must be Input Type but got: undefined."
}
]
}
您最初的尝试是正确的。部分问题是您传递给 UserType
的 name
字段的类型需要完全定义。也就是说,它不仅需要一个 fields
属性,而且还需要一个 name
属性 本身。另一个问题是 User.name
需要将其类型明确设置为 属性。为了可读性和重用性,我会让你的 NameType 成为一个单独的变量:
const NameType = new graphQLObjectType({
name: 'Name',
fields: () => ({
firstName: { type: GraphQLString },
lastName: { type: GraphQLString },
}),
})
const UserType = new GraphQLObjectType({
name: 'User',
fields: () => ({
id: { type: GraphQLID },
email: { type: GraphQLString },
name: { type: NameType }
})
})
所以我正在尝试在 MongoDB 中创建一个用户集合,并使用 GraphQL 和 mongoose 对其进行查询。
我在路径 'pathToServer\server\models\user.js' 中创建了我的用户模式,它看起来像这样:
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const userSchema = new Schema({
name: {
firstName: String,
lastName: String,
},
email: String,
password: String,
})
module.exports = mongoose.model('User', userSchema);
我已经创建了一个 GraphQL 类型,目前我在路径 'pathToServer\server\schema\types\user.js' 中有它,它看起来像这样:
const graphql = require('graphql');
const {GraphQLObjectType, GraphQLList, GraphQLInt, GraphQLID, GraphQLString, GraphQLSchema, GraphQLNonNull} = graphql;
const UserType = new GraphQLObjectType({
name: 'User',
fields: () => ({
id: {type: GraphQLID},
email: {type: GraphQLString},
name: new GraphQLObjectType({
firstName: {type: GraphQLString},
lastName: {type: GraphQLString}
})
})
});
module.exports = UserType;
最后,我在路径 'pathToServer\server\schema\schema.js' :
中有了带有查询和突变的 GraphQL 模式const graphql = require('graphql');
const {GraphQLObjectType, GraphQLList, GraphQLInt, GraphQLID, GraphQLString, GraphQLSchema, GraphQLNonNull} = graphql;
const User = require('../models/user');
const UserType = require('./types/user');
const RootQuery = new GraphQLObjectType({
name: 'RootQueryType',
fields: {
user: {
type: UserType,
args: {
id: {
type: GraphQLID
}
},
resolve(parent, args){
return User.findById(args.id);
}
},
users: {
type: new GraphQLList(UserType),
resolve(parent, args){
return User.find({})
}
}
}
})
const Mutation = new GraphQLObjectType({
name: 'Mutation',
fields: {
addUser: {
type: UserType,
args: {
name: {
firstName: {type: new GraphQLNonNull(GraphQLString)},
lastName: {type: new GraphQLNonNull(GraphQLString)}
},
email: {type: new GraphQLNonNull(GraphQLString)},
password: {type: new GraphQLNonNull(GraphQLString)}
},
resolve(parent, args){
let user = new User({
name: args.name,
email: args.email,
password: args.password,
});
return user.save();
}
}
}
})
module.exports = new GraphQLSchema({
query: RootQuery,
mutation: Mutation
})
问题是每当我启动服务器时它都会抛出一个错误:
Error: Must provide name.
at invariant (pathToServer\server\node_modules\graphql\jsutils\invariant.js:19:11)
at new GraphQLObjectType (pathToServer\server\node_modules\graphql\type\definition.js:499:66)
at fields (pathToServer\server\schema\types\user.js:10:15)
at resolveThunk (pathToServer\server\node_modules\graphql\type\definition.js:370:40)
at defineFieldMap (pathToServer\server\node_modules\graphql\type\definition.js:532:18)
at GraphQLObjectType.getFields (pathToServer\server\node_modules\graphql\type\definition.js:506:44)
at typeMapReducer (pathToServer\server\node_modules\graphql\type\schema.js:232:38)
at pathToServer\server\node_modules\graphql\type\schema.js:239:20
at Array.forEach (<anonymous>)
at typeMapReducer (pathToServer\server\node_modules\graphql\type\schema.js:232:51)
at Array.reduce (<anonymous>)
at new GraphQLSchema (pathToServer\server\node_modules\graphql\type\schema.js:122:28)
at Object.<anonymous> (pathToServer\server\schema\schema.js:79:18)
at Module._compile (module.js:652:30)
at Object.Module._extensions..js (module.js:663:10)
at Module.load (module.js:565:32)
也许我没有正确定义名称字段?我认为它可能会受到不同的对待,因为我模型中的名称字段是一个包含字段 firstName 和 lastName 的对象。
你能看一下吗?
提前致谢!
编辑 我从
编辑了用户类型const UserType = new GraphQLObjectType({
name: 'User',
fields: () => ({
id: {type: GraphQLID},
email: {type: GraphQLString},
name: new GraphQLObjectType({
firstName: {type: GraphQLString},
lastName: {type: GraphQLString}
})
})
});
至
const UserType = new GraphQLObjectType({
name: 'User',
fields: () => ({
id: {type: GraphQLID},
email: {type: GraphQLString},
name: {
firstName: {type: GraphQLString},
lastName: {type: GraphQLString}
}
})
});
现在服务器启动了,但它在 graphiql 中给我这个错误:
{
"errors": [
{
"message": "The type of User.name must be Output Type but got: undefined.\n\nThe type of Mutation.addUser(name:) must be Input Type but got: undefined."
}
]
}
您最初的尝试是正确的。部分问题是您传递给 UserType
的 name
字段的类型需要完全定义。也就是说,它不仅需要一个 fields
属性,而且还需要一个 name
属性 本身。另一个问题是 User.name
需要将其类型明确设置为 属性。为了可读性和重用性,我会让你的 NameType 成为一个单独的变量:
const NameType = new graphQLObjectType({
name: 'Name',
fields: () => ({
firstName: { type: GraphQLString },
lastName: { type: GraphQLString },
}),
})
const UserType = new GraphQLObjectType({
name: 'User',
fields: () => ({
id: { type: GraphQLID },
email: { type: GraphQLString },
name: { type: NameType }
})
})