Graphql returns null id for mongoose aggregation query

我的梦境 提交于 2020-01-15 11:43:09

问题


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

回答1:


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
}


来源:https://stackoverflow.com/questions/58166982/graphql-returns-null-id-for-mongoose-aggregation-query

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!