-
Notifications
You must be signed in to change notification settings - Fork 64
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Error: Converting circular structure to EJSON: #74
Comments
same here |
I also have this issue, and I think it is caused by the way Mongoose models are represented in the memory. They always have circular structures. Or maybe add a flag to enable lean documents and then state in the documentation that the TTL option will only work in combination with lean documents when using Mongoose models. |
There are a number of places in the code where
I'm not familiar with them, but I'm glad we at least have a workaround. |
So, the problem happens using Mongoose because this library tries to serialize a Mongoose “Document” instance, which contains a bunch of methods such as getters/setters, validation, virtuals, etc. Depending on how complex/nested the mongoose model is, extra references(inner Mongoose stuff) should exist, and the serialization through bson.EJSON.stringfy() seems to get a bit confused traversing it and causes that error. There are a few approaches I’ve tested that solve the problem: 1 – Fetch all mongoose models using .lean() to avoid Mongoose hydration. That substantially reduces the object’s size and complexity as well as speed-up the queries since it returns a plain JS object. Thus, simple to be serialized/deserialized without any problems. Here is a comprehensive explanation from Mongoose about it. 2 – Change the serialization process. It is unclear why bson.EJSON.stringify is in place for that once Mongoose/Mongo Nodejs driver does not return BSON document, but JS Objects instead. On my tests, the infamous JSON.stringify() seems to offer better serialization performance than bson.EJSON(.stringify()/.parse()) and bson(.serialize(), deserialize()). Not to say that it also can serialize those hydrated mongoose documents without errors. There are also plenty of other options like avro-js and protobuf-js for faster serialization, but they should require additional tests. @lorensr, we can discuss a pull request for that if you want/need. |
Might the JS objects have things like int32s that don't survive https://github.com/mongodb/js-bson#ejsonstringifyvalue-replacer-space-options |
AFAIK, the Nodejs Mongo driver is the responsible layer for parsing/translating BSON to native JS objects and prop values during CRUD operations through it. |
Yeah, they're no longer binary BSON, but I think the object can have types that aren't supported by JSON (hence EJSON). Would be happy to review a PR for approach 1. |
We at B42 have done approach 1 on our fork: https://github.com/B42-Training/apollo-datasource-mongodb |
It's done.✌
…On Wed, Aug 10, 2022 at 11:23 PM Loren
--
Best regards,
Leonardo Otoni de Assis
|
Currently apollo-datasource-mongodb semi-supports mongoose. This is the second patch required to allow caching to work correctly - otherwise bjson throws circular reference errors due to how mongoose hydrates objects. By changing the mongoose integration to always return lean we can fix the conversion. Known Issue: GraphQLGuide/apollo-datasource-mongodb#74 Mongoose Lean: https://mongoosejs.com/docs/tutorials/lean.html
Currently apollo-datasource-mongodb semi-supports mongoose. This is the second patch required to allow caching to work correctly - otherwise bjson throws circular reference errors due to how mongoose hydrates objects. By changing the mongoose integration to always return lean we can fix the conversion. Known Issue: GraphQLGuide/apollo-datasource-mongodb#74 Mongoose Lean: https://mongoosejs.com/docs/tutorials/lean.html
Hello,
I get the following error when I specify a TTL on
findOneById
,findByFields
, etcError: Converting circular structure to EJSON:
I'm sure that it is caused by something weird in my Mongoose schema, Do you have ideas on how to debug the issue
Version: "apollo-datasource-mongodb": "^0.4.6",
The text was updated successfully, but these errors were encountered: