Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 14 additions & 8 deletions packages/graphql/src/schema/fieldToSchemaMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -704,15 +704,15 @@ export const fieldToSchemaMap: FieldToSchemaMap = {

if (result) {
if (isRelatedToManyCollections) {
results.push({
results[i] = {
relationTo: collectionSlug,
value: {
...result,
collection: collectionSlug,
},
})
}
} else {
results.push(result)
results[i] = result
}
}
}
Expand All @@ -725,7 +725,10 @@ export const fieldToSchemaMap: FieldToSchemaMap = {
}

await Promise.all(resultPromises)
return results
// Assigned by index above so the populated order matches the stored
// order regardless of which dataloader promise settles first. Filter
// out holes left by skipped (invalid collection / missing) entries.
return results.filter((result) => result !== undefined)
}

let id = value
Expand Down Expand Up @@ -1123,15 +1126,15 @@ export const fieldToSchemaMap: FieldToSchemaMap = {

if (result) {
if (isRelatedToManyCollections) {
results.push({
results[i] = {
relationTo: collectionSlug,
value: {
...result,
collection: collectionSlug,
},
})
}
} else {
results.push(result)
results[i] = result
}
}
}
Expand All @@ -1144,7 +1147,10 @@ export const fieldToSchemaMap: FieldToSchemaMap = {
}

await Promise.all(resultPromises)
return results
// Assigned by index above so the populated order matches the stored
// order regardless of which dataloader promise settles first. Filter
// out holes left by skipped (invalid collection / missing) entries.
return results.filter((result) => result !== undefined)
}

let id = value
Expand Down
11 changes: 11 additions & 0 deletions test/graphql/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,17 @@ export default buildConfigWithDefaults({
complexity: 801,
},
},
{
type: 'relationship',
name: 'singleRelation',
relationTo: 'posts',
},
{
type: 'relationship',
name: 'manyRelations',
hasMany: true,
relationTo: 'posts',
},
{
name: 'contentBlockField',
type: 'blocks',
Expand Down
38 changes: 38 additions & 0 deletions test/graphql/int.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -268,5 +268,43 @@ query {

expect(afterDelete.errors).toBeUndefined()
})

it('should preserve hasMany relationship order when a single relationship to the same collection is queried first', async () => {
const postA = await payload.create({ collection: 'posts', data: { title: 'A' } })
const postB = await payload.create({ collection: 'posts', data: { title: 'B' } })
const postC = await payload.create({ collection: 'posts', data: { title: 'C' } })

const parent = await payload.create({
collection: 'posts',
data: {
title: 'parent',
manyRelations: [postA.id, postB.id, postC.id],
singleRelation: postA.id,
},
})

const query = `query {
Post(id: ${idToString(parent.id, payload)}) {
singleRelation {
title
}
manyRelations {
title
}
}
}`

const response = await restClient
.GRAPHQL_POST({ body: JSON.stringify({ query }) })
.then((res) => res.json())

expect(response.errors).toBeUndefined()
expect(response.data.Post.manyRelations.map((doc) => doc.title)).toStrictEqual(['A', 'B', 'C'])

await payload.delete({
collection: 'posts',
where: { id: { in: [parent.id, postA.id, postB.id, postC.id] } },
})
})
})
})
Loading