Graphql: “message”: “ID cannot represent value: 5a72240cf31713598588b70f”

Graphql: “message”: “ID cannot represent value: 5a72240cf31713598588b70f”


7

I use moongose for work with mongodb on nodejs based app. Currently, I try to implement Grapqhl based API.

My query graphql schema looks like:

const schema = buildSchema(`                                                 
    type Query {                                                            
      predictionModels(active: Boolean): [PredictionModel]
    },                                                                                   
    type PredictionModel {                                                                                                 
        _id: ID                                                                                                             
        title: String
        active: Boolean                                                                                                
    }                                                                                                      
`)  

but when I use:

query {
  predictionModels(active: true){
    _id
  }
}

As the response I get:

"errors": [
    {
      "message": "ID cannot represent value: 5a72240cf31713598588b70f",
      "locations": [
        {
          "line": 3,
          "column": 5
        }
      ],
      "path": [
        "predictionModels",
        0,
        "_id"
      ]
    } ....

Prediction model mongoose schema:

const predictionModelSchema = new Schema({                                                                              
    title: { type: String, require: true, unique: true },                                                               
    modelId: { type: String, require: true, unique: true },                                                             
    description: { type: String, unique: true },                                                                        
    language: {type: String, enum: [ProgrammingLanguage.JS, ProgrammingLanguage.R], require: true},                     
    estimates: [{ type : Schema.Types.ObjectId, ref: 'PredictionModelEstimate'}],                                                                                             
    method: { type: methodType, require: true},                                                                         
    active: { type: Boolean, require: true}                                                                             
})

1

  • This message shows up if the value provided for _id is neither a string nor an integer. Mongoose object id fields should be properly serialised by graphql-js by this function. Can you add your mongoose model?

    – Herku

    Oct 25, 2018 at 10:21

1 Answer
1


0

If this is a type issue, you could use:

var ObjectId = require('mongoose').Types.ObjectId;
_id: new ObjectId(ID)



Leave a Reply

Your email address will not be published. Required fields are marked *