Как вставить несколько записей в коллекцию mongodb, используя insertMany с graphql - PullRequest
0 голосов
/ 01 февраля 2019

У меня есть следующая модель, тип и распознаватель:

import mongoose from "mongoose";

const TipoClienteSchema = mongoose.Schema({
    codigoTipoCliente: String,
    descrTipoCliente: String
});

const tipoClienteModel = mongoose.model('tipoclientes', TipoClienteSchema);
export default tipoClienteModel;

type TipoCliente {
    _id: ID
    codigoTipoCliente: String
    descrTipoCliente: String
}

input iTipoClienteMany {
    codigoTipoCliente: String,
    descrTipoCliente: String
}
type Mutation {
    createInsertMany(tipocliente: [iTipoClienteMany]): Response
}

createInsertMany: async (parent, args, {models}) => {
  const tabla = await models.TipoCliente.insertMany(args.tipocliente)
  return {
    acknowledged: true}
}

Когда в Graphiql я выполняю:

mutation 
  createInsertMany{
    createInsertMany(
    tipocliente:[{
      codigoTipoCliente: "88",
      descrTipoCliente: "prueba tipo cliente"
    },
    {
      codigoTipoCliente: "99",
      descrTipoCliente: "prueba tipo cliente 1"
    }]
    ) {
       acknowledged
    }
  }

Он работает нормально и добавляет два регистра, но мне нужно добавить n записей, используя переменные.Я попытался в Graphiql со следующим:

mutation 
  createInsertMany($codigoTipoCliente: String, $descrTipoCliente: String){
    createInsertMany(
    tipocliente:[{
      codigoTipoCliente: $codigoTipoCliente,
      descrTipoCliente: $descrTipoCliente
    }]
    ) {
      acknowledged
    }
  }

//en la seccion QUERY VARIABLES =>
{
  "codigoTipoCliente": 88,
  "descrTipoCliente": "prueba tipo cliente"
}

и он работает для меня, добавляя запись, но я не знаю, и я не смог заставить ее работать более чем на одну запись, я не знаюкак это сделать с массивом в переменных Кто-то, пожалуйста, вы можете мне помочь ... большое спасибо

...