Skip to content
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

Mongoose Objects returning invalid json #60

Open
jontonsoup opened this issue Nov 19, 2015 · 12 comments
Open

Mongoose Objects returning invalid json #60

jontonsoup opened this issue Nov 19, 2015 · 12 comments

Comments

@jontonsoup
Copy link

Not sure if this is irrelevant b/c future plans to support mongoose objects #4.

But currently the string type returned is invalid json. It appears the json returned has single quotes instead of doubles.

This (invalid JSON)

{"name':'search_term','description':'Company Name: Whole Foods','_id':'564d1f91f5e4c16a32d69376'}"

Compared to this (valid):

"{"name":"search_term","description":"Company Name: Whole Foods","_id":"564d1f91f5e4c16a32d69376"}"

If you point me to the right file, I can make a PR. Also, maybe I'm being silly, but wouldn't #4. be solved just by throwing this string into a JSON.parse() function?

I can do that as well if you would like.

Best,
Jon

@tothandras
Copy link
Contributor

@jontonsoup
We use single quotes by design. Take a look at the generic type. If it were using double quotes the JSON would be broken. As far as we can trust the error messages. :)

@jontonsoup
Copy link
Author

@tothandras ah, I was unclear in my original issue. I have an array of objects as defined like this in my schema:

...
  query_arguments: 
    [{name: String, description: String }],
...

The result ends up like this:

      {
        "query_arguments": [
          "{'name':'search_term','description':'Search term name','_id':'564df276b4ec3e9540b3f140'}",
          "{'name':'page','description':'Page number','_id':'564df276b4ec3e9540b3f13f'}"
        ]
      },

Which is returning invalid json.

Best,
Jon

@tothandras
Copy link
Contributor

@jontonsoup Currently graffiti-mongoose is not creating GraphQLObjectTypes for sub documents, instead it handles it as it would be a generic type. If it were using double quotes it would break the JSON ("key": "{"subkey": 1}" the 4th quote closes the value). The solution would be to create new types for objects like this.

@jontonsoup
Copy link
Author

Ah okay -- to clarify. Do you mean a new mongoose schema or a new graphql type / is there an example online somewhere?

Thanks!
Jon

@tothandras
Copy link
Contributor

The library should handle it with defining new GraphQL type. :) In the meantime you can define new mongoose schema and use object references.

@jontonsoup
Copy link
Author

@tothandras awesome! Thanks.

@jontonsoup
Copy link
Author

@tothandras would a better solution be to escape the json? This problem is giving me a huge headache in properly parsing the nested json.

@tothandras
Copy link
Contributor

@jontonsoup you can open a PR if you find a nice solution:
https://github.com/RisingStack/graffiti-mongoose/blob/master/src/type/custom/generic.js

// what you can do
const json = invalidJson.replace(/\'/g, '"');
const parsed = JSON.parse(json);

@jontonsoup
Copy link
Author

@tothandras

That has actually held me up since November, but I've been running into silly corner cases. For example, right now the database can't have any 's because that would produce invalid json.

I'm running into a use case where I need to save ' now.

@jontonsoup
Copy link
Author

Here is an example.

{'name':'Queen Anne's Lace','value':'QUEE'}

if we escaped the values before they are sent over the wire, this could be decoded properly I think.

@tothandras
Copy link
Contributor

@jontonsoup I am really sorry! I haven't had much time to think about this. Can you check the linked file and the tests, you might be able to find a good solution.

@jontonsoup
Copy link
Author

@tothandras No worries! I looked into it more, and it doesn't look like that file is getting hit specifically.

model_file

...
 stores: [{
    options: [{
      name: {
        type: String
      },
      value: {
        type: String,
        required: true
      }
    }],...
    }]

Right now the name field has the quote in it.

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

No branches or pull requests

2 participants