Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Graphql returns null id for mongoose aggregation query

Tags:

Graphql returns null id for mongoose aggregation query, but works ok other mongoose queries.

Here is my mongoose schema:

const { Schema } = mongoose;
const ObjectId = Schema.Types.ObjectId;

const productSchema = new Schema({
  _id: ObjectId,
  price: Number
})

const Product = mongoose.model('Product', productSchema, 'Product')

Here is my Graphql schema:

type Product {
    id: ID
    price: String
}

Graphql normal query:

   context.Product.findOne()

Result with console.log:

[ {
    price: 10,    
    _id: 5d7f8efebff791dcd3bb1b69
}]

Result with graphql:

 "getSearch": [
      {
        "id": "5d7f8efebff791dcd3bb1b69",
        "price": 10,
  }]

Everything is fine here. Now the problem is with aggregation query:

GraphQL query:

context.Product.aggregate(
          [
            { $sample: { size: 1 } }
          ]
    )

Result with console.log:

[ { _id: 5d7f8f23bff791dcd3bb1da3,
    price: 5
}]

Result with GraphQL:

 "test": [
          {
            "id": null",
            "price": 7,
      }]

The problem here is:

  • the id is null
  • the responses from console.log and graphql are different objects
like image 856
Mike Will Avatar asked Sep 30 '19 11:09

Mike Will


2 Answers

Documents in MongoDB normally don't have an id property, just an _id property. This is shown in the console output you're seeing. However, mongoose model instances do have a getter for id that returns the value of id. From the docs:

Mongoose assigns each of your schemas an id virtual getter by default which returns the documents _id field cast to a string, or in the case of ObjectIds, its hexString.

What's returned by methods like find and findOne are instances of the Model that have this getter. However, using aggregate results in plain JavaScript objects being returned instead (with no getter).

You can write a resolver for the id field to pull the value from either id or _id:

function resolve (parent, args, context, info) {
  return parent.id || parent._id
}
like image 165
Daniel Rearden Avatar answered Nov 09 '22 07:11

Daniel Rearden


Adding id element to the result works fine to mine

   const res = await Product.aggregate(
          [
            { $sample: { size: 1 } }
          ]
    )
    res.forEach(element => {
         element.id = element._id 
    });
    return res;
like image 29
Abdulkadir Ugas Avatar answered Nov 09 '22 07:11

Abdulkadir Ugas