title: Mongoose + GraphQL cannot populate model
body: <p>Well, I know there are a lot of questions regarding this issue but neither of their solutions worked for me, or I can't find what is happening.</p>
  
  <p>I have a backend server running Apollo Server + Mongoose on localhost. My issue is I can't populate or create the collection since I get:</p>
  
  <p><strong>Error</strong>: <code>"ID cannot represent value: &lt;Buffer 5e 38 65 18 f1 e3 f5 43 10 d4 c1 45&gt;"</code>.</p>
  
  <p>This is the model:</p>
  
  <pre class="lang-js prettyprint-override"><code>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);
  </code></pre>
  
  <p>This is my type definition:</p>
  
  <pre class="lang-js prettyprint-override"><code>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
      }
  `;
  </code></pre>
  
  <p>And this is my resolver:</p>
  
  <pre class="lang-js prettyprint-override"><code>import Gasto from '../models/Gasto';
  
  export default {
      Query: {
          gastos: () =&gt; Gasto.find().populate('proveedores').exec(),
          gasto: (_, { id }) =&gt; Gasto.findById(id).populate('proveedores').exec()
      },
      Mutation: {
          crearGasto: (_, input) =&gt; Gasto.create(input),
          actualizarGasto: (_, input) =&gt; Gasto.findOneAndUpdate(input),
          eliminarGasto: (_, { id }) =&gt; Gasto.findOneAndDelete(id)
      }
  };
  </code></pre>
  
  <h3>Attempt 1</h3>
  
  <p>I have tried this change on my model definition but it didn't work:</p>
  
  <pre class="lang-js prettyprint-override"><code>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);
  </code></pre>
  
  <h3>Attempt 2</h3>
  
  <p>According to the <a href="https://mongoosejs.com/docs/populate.html" rel="nofollow noreferrer">mongoose documentation</a> I also tried switching between <code>populate('proveedores')</code> (plural), <code>populate('proveedor') (singular)</code> but the error changes:</p>
  
  <pre class="lang-js prettyprint-override"><code>import Gasto from '../models/Gasto';
  
  export default {
      Query: {
          gastos: () =&gt; Gasto.find({}).populate('proveedor').exec(),
          gasto: (_, { id }) =&gt; Gasto.findById(id).populate('proveedor').exec()
      },
      Mutation: {
          crearGasto: (_, input) =&gt; Gasto.create(input),
          actualizarGasto: (_, input) =&gt; Gasto.findOneAndUpdate(input),
          eliminarGasto: (_, { id }) =&gt; Gasto.findOneAndDelete(id)
      }
  };
  </code></pre>
  
  <p><strong>Error</strong>: <code>"message": "Schema hasn't been registered for model \"Proveedor\".\nUse mongoose.model(name, schema)"</code>.</p>
  
  <p>Just in case this is my <code>Proveedor</code> model definition:</p>
  
  <pre class="lang-js prettyprint-override"><code>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);
  </code></pre>
  
  <p>This is what i am querying on the GraphQL playground:</p>
  
  <pre><code>query {
    gastos {
      id
      importe
      proveedor {
        id
        nombre
      }
    }
  }
  </code></pre>
  
  <p>Any clues? Thanks in advance.</p>