如何使用GraphQL进行包含一组对象数组的组变异?

时间:2019-05-03 09:57:21

标签: mongodb mongoose 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~"
            }
        ]
    ) 
}

0 个答案:

没有答案