2

I am trying to create an apollo graphql mutation which can insert multiple rows into DB. This is my schema structure:

export default typeDefs = [`
 type OptionDataObject {
  id: Int,
  UserID: Int,
  TypeID: Int,
  Value: Int
 }

 type OptionData {
  id: Int,
  UserID: Int,
  TypeID: Int,
  Value: Int
 }

 addOptionData(listOfOptionData: [OptionDataObject]): [OptionData]

 type Query {
  getOptionData(UserID: Int): [OptionData]
 }

 schema {
  query: Query
  mutation: Mutation
 }
`];

My corresponding resolver:

export default resolver = {
 Query: {
  var UserID = parseInt(args.UserID);
  if(UserID) {
   return OptionData.findAll({
    where: args
   })
  }
 }, 
 Mutation: {
  addOptionData(_, args) {
   return OptionData.createMany(args.listOfOptionData);
  }
 }
}

The problem here is that I already have findAll defined for returning multiple queries as a list, but there is no createMany method. Do I need to define it? In the networkInterface?

I am not able to find any documentation online for multiple row inserts and updates. Any tips would be greatly helpful.

Thanks!

Abhi
  • 51
  • 4

2 Answers2

1

if you're using mongoose, you can call Model.create and pass it your array of documents to create.

http://mongoosejs.com/docs/api.html#model_Model.create Mongoose create multiple documents

Community
  • 1
  • 1
rkstar
  • 1,178
  • 14
  • 27
  • currently my application is built using apollo graphql, with mysql db; it is not using mongoose – Abhi Mar 21 '17 at 18:16
0

If there's no createMany method defined, you've to create it by yourself in the resolvers.

Can we have more information on the UI logic ?

awzx
  • 1,023
  • 2
  • 12
  • 31