fix(db-mongodb): fix projection handling for relationship fields in GraphQL queries with select #14850
+154
−0
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?
Fix GraphQL API select flag returning empty arrays for relationship and upload fields when using MongoDB.
Why?
When using the GraphQL API with
select: true, relationship fields were being returned as empty arrays instead of the actual populated data. This issue only affected MongoDB, not SQLite/Postgres.The root cause was in
buildProjectionFromSelect.ts. When GraphQL builds a select object, it creates nested objects for relationship fields (e.g.,{ hasOne: { id: true, text: true } }) to indicate which fields should be selected from the populated relationship. However, the MongoDB projection builder only checked forselect[field.name] === trueat line 65, which doesn't match when the value is an object. The relationship fields then fell through to the switch statement which had no case for relationship or upload types, causing them to be silently skipped from the projection. Without the relationship field in the MongoDB projection, the document was returned without the relationship IDs, so the population phase had nothing to populate - resulting in empty arrays.How?
Added a switch case for relationship and upload field types in
buildProjectionFromSelect.tsthat adds the field to the MongoDB projection. This ensures the raw relationship IDs are included in the query result, allowing the population logic to work correctly.Also added two GraphQL integration tests that verify relationship fields (both regular and polymorphic) are properly returned when using the select flag.
Fixes #14467