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

fix(plugin-graphql): Fix association resolver for manyWay nature. #7959

Merged
merged 1 commit into from Sep 22, 2020

Conversation

danieldspx
Copy link
Contributor

This fixes #4353 and all its duplicates.

Detailed description of the problem:

As said by @abanchev here the problem is at file services/type-definitions.js near line 184:

if (
    ((association.nature === 'manyToMany' && association.dominant) ||
    association.nature === 'manyWay') &&
    _.has(obj, association.alias) // if populated
) { ... } else {
    _.set(queryOpts, ['query', association.via], obj[targetModel.primaryKey]);
}

It turns out that in the case of a 3+ level nested query, the third level will not work because the obj is not populated with the association.alias so it enters the else block and add the association.via in the query. But the problem is that association.via for manyWay is undefined. This will propagate and the code will be looking for something like undefined_id and throw the known error message:

Your filters contain a field 'undefined' that doesn't appear on your model definition nor it's relations

You can reproduce it in this repo that I have prepared: https://github.com/danieldspx/strapi-graphql-example (See the README)

The cause for this problem: As we all know well, if you want to fetch such a nested content like this (with the API endpoint), you need to overwrite your controller (see this stackoverflow awnser). For the GraphQL we do not have something like this. Overwriting your controller will not affect the GraphQL way of fetching the content, thus the problem persist independent of your controller (I have tested this).

Description of my solution:

As you can see I added one line in the switch case and then the problem is fixed because it fetches de data for the association.alias and everything works perfectly and you can go as many deepness levels as you want (as long you respect your depthLimit). This does not break other relations nature.

This fixes strapi#4353 and all its duplicates.

Signed-off-by: Daniel dos Santos Pereira <danield1591998@gmail.com>
@codecov
Copy link

codecov bot commented Sep 19, 2020

Codecov Report

Merging #7959 into master will not change coverage.
The diff coverage is n/a.

Impacted file tree graph

@@           Coverage Diff           @@
##           master    #7959   +/-   ##
=======================================
  Coverage   32.71%   32.71%           
=======================================
  Files        1194     1194           
  Lines       12965    12965           
  Branches     1279     1279           
=======================================
  Hits         4241     4241           
  Misses       7884     7884           
  Partials      840      840           
Flag Coverage Δ
#front 24.81% <ø> (ø)
#unit 53.85% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
...min/admin/src/containers/ProfilePage/utils/form.js 100.00% <ø> (ø)
.../admin/src/containers/Users/EditPage/utils/form.js 100.00% <ø> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 49ce464...6ce862f. Read the comment docs.

@danieldspx danieldspx changed the title fix(plugin-graphql): Fix associaion resolver for manyWay nature. fix(plugin-graphql): Fix association resolver for manyWay nature. Sep 19, 2020
Copy link
Member

@alexandrebodin alexandrebodin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, Thanks !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
issue: enhancement Issue suggesting an enhancement to an existing feature source: plugin:graphql Source is plugin/graphql package
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Graphql - querying deeply nested "has Many" One to Many relationship throws an exception
2 participants