-
Notifications
You must be signed in to change notification settings - Fork 52
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
"TypeError: Cannot read property 'forEach' of undefined" when embedded object field is a reference #131
Comments
I suspect this might be a bug in Graphql.js itself. since in the "type" docs of Graphql the "args" property of a field is optional. From here:
Should I open a bug in graphql? |
@yoadsn looking at it |
feel free to open the PR, I will try to fix the tests |
After many hours of debugging- I think I have found the problem - deep inside the type.js source code. |
@yoadsn Thank you, I really appreciate! |
Ok, For future reference here is a brief of the problem and how it was resolved. Since ever, graphql was memoizing access to the getFields(): GraphQLFieldDefinitionMap {
return this._fields || (this._fields =
defineFieldMap(this, this._typeConfig.fields)
);
} code here; Now, graffiti-mongoose generates fields with reference and attaches the correct types to those fields in a big method parent = parent[segment].type.getFields(); code here The Later on, when the schema and inside it that specific The fix was to enumerate the fields using an external method which does not call the internal memoizing leaving the object "clean". parent = getTypeFields(parent[segment].type); This is how other fields were populated in other parts of that method. |
Overview of the Issue
Querying a schema field which is a reference and is a sub field of an embedded object fails graphql validation with error:
The relevant parts of the stack trace of the internal graphql execution is:
Reproduce the Error
Steps to reproduce:
Node version:
5.6.0
(but this also happens on older version)Graffiti-Mongoose version:
5.2.0
(but happened in previous versions as well)Related Issues
#3 Probably should have written the tests.
Suggest a Fix
The problem is a check done by the graphql runtime in:
src/validation/rules/ProvidedNonNullArguments.js
->ProvidedNonNullArguments()
This, checks that non-null args are supplied. The problem is that it always expects an 'args' field to exist on the fields checked by this method (even if empty)
For some reason - the embedded sub fields which are reference does not have an 'args' field. I suspect
this has to do with the
getType()
method in graffiti-mongoose'sgraffiti-mongoose/src/type/type.js
.There, on line 255 it is seen that the field definition is created without an
args
field.See here
but on line 228 which handles the same case for array type of fields - it does.
See here
I tried adding an empty array as the `args' field there - it fixes the problem but fails some of the tests on the schema validation utils from garphql - so I assume that is not a good fix.
I have a PR with a failing test cases I can submit - but that would mean it will be failing until a fix is found. We can merge that to a failing branch if you like and try and analyze it there.
See my branch here for the changes
Thank you for your contribution!
The text was updated successfully, but these errors were encountered: