It seems like I have encountered a unique issue that none of the existing solutions have been able to resolve. I am running a backend server with Apollo Server + Mongoose on localhost. However, I am facing difficulties in populating or creating the collection due to the following error:
Error:
"ID cannot represent value: <Buffer 5e 38 65 18 f1 e3 f5 43 10 d4 c1 45>"
.
Below is the model I am working with:
import mongoose from 'mongoose';
const Float = require('mongoose-float').loadType(mongoose);
const gastoSchema = new mongoose.Schema({
descripcion: String,
importe: Float,
proveedor: {
type: mongoose.Schema.Types.ObjectId,
ref: 'Proveedor'
},
}, {
timestamps: {
createdAt: 'fechaCreacion',
updatedAt: 'fechaActualizacion'
}
});
export default mongoose.model('gastos', gastoSchema);
This is the GraphQL type definition I have used:
import { gql } from 'apollo-server';
export default gql`
type Gasto {
id: ID
descripcion: String
importe: Float
proveedor: Proveedor
}
extend type Query {
gastos: [Gasto]
gasto(id: ID!): Gasto
}
extend type Mutation {
crearGasto(descripcion: String, importe: Float, proveedor: ID): Gasto
actualizarGasto(id: ID!, descripcion: String, importe: Float, proveedor: ID): Gasto
eliminarGasto(id: ID!): String
}
`;
Furthermore, here is the resolver implementation:
import Gasto from '../models/Gasto';
export default {
Query: {
gastos: () => Gasto.find().populate('proveedores').exec(),
gasto: (_, { id }) => Gasto.findById(id).populate('proveedores').exec()
},
Mutation: {
crearGasto: (_, input) => Gasto.create(input),
actualizarGasto: (_, input) => Gasto.findOneAndUpdate(input),
eliminarGasto: (_, { id }) => Gasto.findOneAndDelete(id)
}
};
Attempt 1
I made a modification to my model definition based on a suggestion, but unfortunately, it didn't resolve the issue:
import mongoose from 'mongoose';
const Float = require('mongoose-float').loadType(mongoose);
const ObjectId = require('mongoose').Types.ObjectId;
ObjectId.prototype.valueOf = function() { return this.toString(); }
const gastoSchema = new mongoose.Schema({
descripcion: String,
importe: Float,
proveedor: {
type: ObjectId,
ref: 'Proveedor'
},
}, {
timestamps: {
createdAt: 'fechaCreacion',
updatedAt: 'fechaActualizacion'
}
});
export default mongoose.model('gastos', gastoSchema);
Attempt 2
Following the mongoose documentation, I tried switching between populate('proveedores')
(plural) and populate('proveedor') (singular)
, but a new error occurred:
import Gasto from '../models/Gasto';
export default {
Query: {
gastos: () => Gasto.find({}).populate('proveedor').exec(),
gasto: (_, { id }) => Gasto.findById(id).populate('proveedor').exec()
},
Mutation: {
crearGasto: (_, input) => Gasto.create(input),
actualizarGasto: (_, input) => Gasto.findOneAndUpdate(input),
eliminarGasto: (_, { id }) => Gasto.findOneAndDelete(id)
}
};
Error:
"message": "Schema hasn't been registered for model \"Proveedor\".\nUse mongoose.model(name, schema)"
.
Just to provide complete information, here is the definition of my Proveedor
model:
import mongoose from 'mongoose';
const proveedorSchema = new mongoose.Schema({
nombre: {
type: String,
required: true
},
telefono: String,
direccion: String,
email: String,
}, {
timestamps: {
createdAt: 'fechaCreacion',
updatedAt: 'fechaActualizacion'
}
});
export default mongoose.model('proveedores', proveedorSchema);
Lastly, this is the GraphQL query I am using in the playground:
query {
gastos {
id
importe
proveedor {
id
nombre
}
}
}
If anyone has any insights or suggestions, I would greatly appreciate it. Thank you in advance.