fix(graphql): gql relationship has many null values #9307
+102
−1
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 thecontext.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 then
threads to something below-constant. This makes the end result something greater thanO(n)
. Compare that to this proposed result, which retains the existingO(n)
time to run the threads, and addsO(n)
more work to filter out the null values, resulting inO(n)+O(n)=O(n)
execution time.Fixes #9236