Skip to content

Commit 38515ff

Browse files
committed
Use $lookup to populate area children
1 parent 666fa4c commit 38515ff

File tree

3 files changed

+10
-14
lines changed

3 files changed

+10
-14
lines changed

src/__tests__/areas.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,6 @@ describe('areas API', () => {
202202
})
203203
expect(areaClimbsResponse.statusCode).toBe(200)
204204
const areaResult = areaClimbsResponse.body.data.area
205-
console.log(areaClimbsResponse.body)
206205
// In leftRightIndex order
207206
expect(areaResult.climbs[0]).toMatchObject({ name: 'left', metadata: { leftRightIndex: 0 } })
208207
expect(areaResult.climbs[1]).toMatchObject({ name: 'middle', metadata: { leftRightIndex: 1 } })

src/graphql/resolvers.ts

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -200,13 +200,6 @@ const resolvers = {
200200
// New camel case field
201201
areaName: async (node: AreaType) => node.area_name,
202202

203-
children: async (parent: AreaType, _, { dataSources: { areas } }: Context) => {
204-
if (parent.children.length > 0) {
205-
return await areas.findChildren(parent.children)
206-
}
207-
return []
208-
},
209-
210203
aggregate: async (node: AreaType) => {
211204
return node.aggregate
212205
},

src/model/AreaDataSource.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -105,14 +105,23 @@ export default class AreaDataSource extends MongoDataSource<AreaType> {
105105
as: 'climbs' // clobber array of climb IDs with climb objects
106106
}
107107
},
108+
{ // Self-join to populate children areas.
109+
$lookup: {
110+
from: 'areas',
111+
localField: 'children',
112+
foreignField: '_id',
113+
as: 'children'
114+
}
115+
},
108116
{
109117
$set: {
110118
'climbs.gradeContext': '$gradeContext' // manually set area's grade context to climb
111119
}
112120
},
113121
{
114122
$set: {
115-
climbs: { $sortArray: { input: '$climbs', sortBy: { 'metadata.left_right_index': 1 } } }
123+
climbs: { $sortArray: { input: '$climbs', sortBy: { 'metadata.left_right_index': 1 } } },
124+
children: { $sortArray: { input: '$children', sortBy: { 'metadata.leftRightIndex': 1 } } }
116125
}
117126
}
118127
])
@@ -123,11 +132,6 @@ export default class AreaDataSource extends MongoDataSource<AreaType> {
123132
throw new Error(`Area ${uuid.toUUID().toString()} not found.`)
124133
}
125134

126-
async findChildren (children: mongooseTypes.ObjectId[]): Promise<AreaType[]> {
127-
return await this.areaModel.find().where('_id').in(children)
128-
.sort({ 'metadata.leftRightIndex': 1 }).lean()
129-
}
130-
131135
async findManyClimbsByUuids (uuidList: muuid.MUUID[]): Promise<ClimbType[]> {
132136
const rs = await this.climbModel.find().where('_id').in(uuidList)
133137
return rs

0 commit comments

Comments
 (0)