如何在graphql-compose-mongoose中使用updateMany更新集合?

时间:2019-05-15 14:34:28

标签: node.js mongodb mongoose graphql graphql-compose-mongoose

如何使用带有猫鼬的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函数?

如何使用axiosgraphiql来做到这一点?

0 个答案:

没有答案