Skip to content

Commit 4a38521

Browse files
committed
bulk areas query
1 parent 247f8a9 commit 4a38521

File tree

4 files changed

+32
-2
lines changed

4 files changed

+32
-2
lines changed

src/db/AreaTypes.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,3 +259,7 @@ export enum OperationType {
259259
/** Set areas' sorting index */
260260
orderAreas = 'orderArea'
261261
}
262+
263+
export interface BulkAreasGQLQueryInput {
264+
ancestors: string[]
265+
}

src/graphql/area/AreaQueries.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { AreaType } from '../../db/AreaTypes'
1+
import { AreaType, BulkAreasGQLQueryInput } from '../../db/AreaTypes'
22
import { GQLContext } from '../../types'
33

44
const AreaQueries = {
@@ -11,8 +11,13 @@ const AreaQueries = {
1111
countries: async (_, params, { dataSources }: GQLContext): Promise<AreaType[]> => {
1212
const { areas } = dataSources
1313
return await areas.listAllCountries()
14-
}
14+
},
1515

16+
bulkAreas: async (_: any, params, { dataSources }: GQLContext): Promise<AreaType[]> => {
17+
const { areas } = dataSources
18+
const { ancestors } = params as BulkAreasGQLQueryInput
19+
return await areas.bulkDownloadAreas(ancestors)
20+
}
1621
}
1722

1823
export default AreaQueries

src/graphql/schema/Area.gql

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
type Query {
22
area(uuid: ID): Area
33
areas(filter: Filter, sort: Sort): [Area]
4+
5+
"""
6+
Bulk download an area and its children starting from ancestors paths (inclusive).
7+
To keep payload at a reasonable size ancestors must have at least 2 elements.
8+
"""
9+
bulkAreas(ancestors: [String!]!): [Area]
10+
411
stats: Stats
512
cragsNear(
613
placeId: String

src/model/AreaDataSource.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { GraphQLError } from 'graphql'
2+
import { ApolloServerErrorCode } from '@apollo/server/errors'
13
import { MongoDataSource } from 'apollo-datasource-mongodb'
24
import { Filter } from 'mongodb'
35
import muuid from 'uuid-mongodb'
@@ -275,4 +277,16 @@ export default class AreaDataSource extends MongoDataSource<AreaType> {
275277
}
276278
return await this.areaModel.find(filter).lean()
277279
}
280+
281+
async bulkDownloadAreas (ancestors: string[]): Promise<AreaType[]> {
282+
if (ancestors.length < 2) {
283+
throw new GraphQLError('Must provide at least 2 ancestors.', {
284+
extensions: {
285+
code: ApolloServerErrorCode.BAD_USER_INPUT
286+
}
287+
})
288+
}
289+
const ancestorsCSV = ancestors.join(',')
290+
return await this.findDescendantsByPath(ancestorsCSV)
291+
}
278292
}

0 commit comments

Comments
 (0)