Skip to content
This repository has been archived by the owner on Feb 10, 2022. It is now read-only.

toJS method #204

Open
cherealnice opened this issue Jan 29, 2018 · 4 comments
Open

toJS method #204

cherealnice opened this issue Jan 29, 2018 · 4 comments

Comments

@cherealnice
Copy link

Hi,

Thanks for making mongorito!

I am currently integrating with graphql and the only way I can send it data in JS form (unless I'm missing something) is by stringifying to JSON, and then parsing it back. Is there a better way? If not, I'm more than happy to make a PR if you can point me in the right direction.

Thanks!

@wesleycoder
Copy link

wesleycoder commented Feb 5, 2018

I'm using GraphQL too.
I'm not sure what do you mean by serializing and parsing it.

I think you do not need to stringify any data to JSON, you only need to call instance.get() on each instance of model before passing to the Schema, this should give you a raw object containing the data from the instance.

As far as I know you can pass any raw JavaScript object to the GraphQL Schema and it will serialize them for you.

If you mean to have another way of passing the data to the client other than JSON I'm sure you cannot do it with GraphQL as its nature is to be a JSON API.

@wesleycoder
Copy link

Following this, i found that actually a toJSON method is implemented in the Model class.
But testing this doesn't work with graphql. It seems to be because of how GraphQL serializes the objects passed to it.
It seems that graphql doesn't use JSON.stringify in order to call toJSON on Model instances, it may be implementing his own serializer.

@cherealnice
Copy link
Author

Thanks for the responses. Looks like I tried to call get() on the collection, which didn't work, but when I map over and call get() it works.

@wesleycoder
Copy link

I'm glad I could help.
That's also what I'm doing (.map and call .get()).

However, I'm still curious on how could we make graphql aware of the method .toJSON on the instances and serialize with them.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants