如何使用带有猫鼬的graphql更新集合?
例如,我有用户集合。
const UserSchema = new mongoose.Schema({
name: String, // standard types
contacts: { // another mongoose way for providing embedded documents
email: String,
phones: [String], // array of strings
},
gender: { // enum field with values
type: String,
enum: ['male', 'female', 'ladyboy'],
},
someMixed: {
type: mongoose.Schema.Types.Mixed,
description: 'Can be any mixed type, that will be treated as JSON GraphQL Scalar Type',
},
});
const User = mongoose.model('User', UserSchema);
我使用graphql-compose-mongoose
const UserTC = composeWithMongoose(User, customizationOptions);
schemaComposer.Query.addFields({
userById: UserTC.getResolver('findById'),
userByIds: UserTC.getResolver('findByIds'),
userOne: UserTC.getResolver('findOne'),
userMany: UserTC.getResolver('findMany'),
userCount: UserTC.getResolver('count'),
userConnection: UserTC.getResolver('connection'),
userPagination: UserTC.getResolver('pagination'),
});
schemaComposer.Mutation.addFields({
userCreateOne: UserTC.getResolver('createOne'),
userCreateMany: UserTC.getResolver('createMany'),
userUpdateById: UserTC.getResolver('updateById'),
userUpdateOne: UserTC.getResolver('updateOne'),
userUpdateMany: UserTC.getResolver('updateMany'),
userRemoveById: UserTC.getResolver('removeById'),
userRemoveOne: UserTC.getResolver('removeOne'),
userRemoveMany: UserTC.getResolver('removeMany'),
});
const graphqlSchema = schemaComposer.buildSchema();
export default graphqlSchema;
在客户端,我使用findMany
函数吸引所有用户。
现在我想更新用户集合中的某些属性(无论是哪个属性)
users[0].contacts.email = 'blabla@gmail.com';
我的问题是如何将对象(用户)直接发送到服务器并使用updateMany
函数?
如何使用axios
或graphiql
来做到这一点?