Skip to content

Commit

Permalink
Use $lookup to populate area children
Browse files Browse the repository at this point in the history
  • Loading branch information
zichongkao committed May 23, 2023
1 parent 666fa4c commit 0d8503c
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 9 deletions.
1 change: 0 additions & 1 deletion src/__tests__/areas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,6 @@ describe('areas API', () => {
})
expect(areaClimbsResponse.statusCode).toBe(200)
const areaResult = areaClimbsResponse.body.data.area
console.log(areaClimbsResponse.body)
// In leftRightIndex order
expect(areaResult.climbs[0]).toMatchObject({ name: 'left', metadata: { leftRightIndex: 0 } })
expect(areaResult.climbs[1]).toMatchObject({ name: 'middle', metadata: { leftRightIndex: 1 } })
Expand Down
7 changes: 0 additions & 7 deletions src/graphql/resolvers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,13 +200,6 @@ const resolvers = {
// New camel case field
areaName: async (node: AreaType) => node.area_name,

children: async (parent: AreaType, _, { dataSources: { areas } }: Context) => {
if (parent.children.length > 0) {
return await areas.findChildren(parent.children)
}
return []
},

aggregate: async (node: AreaType) => {
return node.aggregate
},
Expand Down
11 changes: 10 additions & 1 deletion src/model/AreaDataSource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,14 +105,23 @@ export default class AreaDataSource extends MongoDataSource<AreaType> {
as: 'climbs' // clobber array of climb IDs with climb objects
}
},
{ // Self-join to populate children areas.
$lookup: {
from: 'areas',
localField: 'children',
foreignField: '_id',
as: 'children'
}
},
{
$set: {
'climbs.gradeContext': '$gradeContext' // manually set area's grade context to climb
}
},
{
$set: {
climbs: { $sortArray: { input: '$climbs', sortBy: { 'metadata.left_right_index': 1 } } }
climbs: { $sortArray: { input: '$climbs', sortBy: { 'metadata.left_right_index': 1 } } },
children: { $sortArray: { input: '$children', sortBy: { 'metadata.leftRightIndex': 1 } } }
}
}
])
Expand Down

0 comments on commit 0d8503c

Please sign in to comment.