首页 > 解决方案 > 如何使用 GraphQL 制作包含对象数组的组突变?

问题描述

大家好,我陷入了一个突变,其中有一个数组,一次突变多个对象。

所以这是我的架构InstalmentsGroup

从“猫鼬”导入猫鼬;

const Schema = mongoose.Schema;

const InstalmentsGroupSchema = new Schema(
  {
    offer: {
      type: Schema.Types.ObjectId,
      ref: "Offer"
    },
    instalmentCount: { type: Number },
    instalmentAmout: { type: Number },
    dueTo: { type: Date }
  },
  { timestamps: true }
);

export default mongoose.model("InstalmentsGroup", InstalmentsGroupSchema);

这些是我的types

type Instalment {
    _id: ID!
    instalmentCount: Int!
    instalmentAmout: Int!
    dueTo: Date!
    instalmentGroup: InstalmentGroup
  }

  type InstalmentGroup {
    _id: ID!
    offer: ID!
    instalments: [Instalment!]!
  }

这是我的mutation

createInstalmentGroup(
  offer: ID!
  instalments: [Instalment!]!
): InstalmentGroup

resolver

createInstalmentGroup: async (_, { ...args }, { user }) => {
    try {
      await requireAuth(user);
      const instalments = await InstalmentsGroup.create({ ...args });

      return instalments;
    } catch (error) {
      throw error;
    }
  }

Now all I got is this `Error: The type of Mutation.createInstalmentGroup(instalments:) must be Input Type but got: [Instalment!]!.`

我想做的是看起来像这样

mutation {
    createInstalmentGroup(
        offer: "5cc8876242b1ad68efdd7df3",
        instalments: [
            {
                instalmentCount: 1,
                instalmentAmout: 2000,
                dueTo: "2019-06-01T23:00:00.000Z"
            }. {
                instalmentCount: 2,
                instalmentAmout: 2000,
                dueTo: "2019-07-01T23:00:00.000~"
            }
        ]
    ) 
}

标签: mongodbmongoosegraphql

解决方案


推荐阅读