-
-
Notifications
You must be signed in to change notification settings - Fork 94
Null fields in mongo return sub fields #7
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
Comments
Some more info. Assume in the above code in graphql-js I add some hackish deubgging code like this:
Then with graffiti-mongoose the output looks like this:
And with graphql-compose* it's:
So again, for some reason the |
You expect correct behavior. It seems that graphql-compose-mongose for nested schemas does not has correct Right now just add such thing to MyTC. MyTC.extendField('someField', {
resolve: (source) => source.someField ? source.someField : null,
}); Will be nice if you provide result of console.log(source). |
Suppose that |
I'm still debugging this - the above test case cannot be trusted anymore since some previous debug lines changed stuff. Will post new debugging data ASAP. |
Ok, now the printout is correct. But it would not provide the full story since I need to figure out why "null" is printed when doing toString() of a mongoose object (which is not null). |
Point in code where we should add resolve method with null check to fields with sub schema type: https://github.com/nodkz/graphql-compose-mongoose/blob/master/src/fieldsConverter.js#L152 |
Ok, @nodkz I believe I have now more information about what is going on. (Some is obvious to you probably, but still). When we execute a mongoose query - we get back a document and not a normal javascript object. The mongoose document we get for this mongodb document:
Would have a non-null value on the So when the graphql-js default field resolver gets that mongoose document, and checks Changing the code at: To something like this:
confirms this and prints 'NOT NULL'. Your suggestion to check for null here is possible, we need to use the (undocumented?)
effectively overriding the default field resolver for mongoose documents. But we have also another alternative which is also documented in mongoose API ref and used in graffiti-mongoose. The are converting the mongoose document to a javascript plain object immediately after it returns from the query execution. See here: For us, we might go with the same approach by changing the different resolvers generated by
We can also centrally wrap those resolvers here:
Of course this code is not robust but the gist should be clear.. What do you think? |
PS this broke my "relations" since I relied on the virtual getter |
Awesome analysis. But I didn't know that it produce |
Thank you! too bad spent the last hour writing wrappers around all resolvers ;) |
I think that fix will be fast and easy. But got tons of problems with nested docs in nested doc. So only one correct way was to call I think that NestedSchema = mongoose.Schema(
{
...someFields
}, {
toObject: { virtuals: true },
}
); I think this is more control behavior. |
…ses` and old behavior was quite expensive with `.toObject` call. Also if we take a look at `toObject` implementations then we can find that it calls minimize under the hood. So if you need the old behavior, just write your custom resolver for the field which you want to minimize. Related: graphql-compose#7
…ses` and old behavior was quite expensive with `.toObject` call. Also if we take a look at `toObject` implementations then we can find that it calls minimize under the hood. So if you need the old behavior, just write your custom resolver for the field which you want to minimize. Related: #7
Hey,
I am not sure where this behavior is coming from.. have been debugging both graphql-compose* and graphql-js for the past 5 hours. Any help in the right direction would be great!
Assume this mongodb document:
And this mongoose schema:
The following gql query (assume the getOne field returns the above document):
I expect to get:
This is for example what graffiti-mongoose returns..
But from graphql-compose* I get:
Am I expecting the wrong behavior? I found this issue graphql/graphql-js#424 that support what I expect (I think). Where is the difference?
So far I reached the conclusion that graphql-js does not recognize the value for "someField" to be null here:
https://github.com/graphql/graphql-js/blob/73513d35747cdea255156edbe30d85d9bd0c1b81/src/execution/execute.js#L774
I'm using graphql 0.7.2 although this specific code seems to remain the same in 0.8*.
Thanks!
The text was updated successfully, but these errors were encountered: