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(graphql): gql relationship has many null values #9307

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

6TELOIV
Copy link

@6TELOIV 6TELOIV commented Nov 18, 2024

What?

The gql api responds with a null value error when running a query on a hasMany relationship field where the related collection has read access control blocking some of the records from being read.

Why?

The cause of that issue is that the payload gql relationship resolver function must return new GraphQLList(new GraphQLNonNull(type)) but the context.req.payloadDataLoader.load will return null for data that is blocked by access control. This results in the resolver function returning null values, and causes the error.

How?

I fixed this by adding a filter to remove the null values from the result: return results.filter((result) => result !== null). I believe this is the appropriate way to fix this as explained below.

Because the code is written to run the data loading in parallel, the n promises all run and insert something into the results array at their predetermined index to keep the items in order. Thinking about trying to do the same while skipping null values, I would think you'd need to use some sort of heap/sorted data structure, which would slow the insertion for the n threads to something below-constant. This makes the end result something greater than O(n). Compare that to this proposed result, which retains the existing O(n) time to run the threads, and adds O(n) more work to filter out the null values, resulting in O(n)+O(n)=O(n) execution time.

Fixes #9236

@6TELOIV
Copy link
Author

6TELOIV commented Nov 18, 2024

This PR also adds a regression test for the issue which I used to verify the presence of the issue as well as the fix. If you checkout commit #3399b29 you can see the failing test.

Copy link
Contributor

This PR is stale due to lack of activity.

To keep the PR open, please indicate that it is still relevant in a comment below.

@github-actions github-actions bot added the stale label Dec 12, 2024
@6TELOIV
Copy link
Author

6TELOIV commented Dec 12, 2024

I just brought the branch up to date with main, and the issue is still present. This PR should fix it.

@AlessioGr AlessioGr removed the stale label Dec 13, 2024
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

Successfully merging this pull request may close these issues.

GraphQL Error for hasMany relation where some records are denied read access, but others are not
3 participants