Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bulk areas query #451

Merged
merged 1 commit into from
Mar 5, 2025
Merged
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
4 changes: 4 additions & 0 deletions src/db/AreaTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -257,3 +257,7 @@ export enum OperationType {
/** Set areas' sorting index */
orderAreas = 'orderArea'
}

export interface BulkAreasGQLQueryInput {
ancestors: string[]
}
9 changes: 7 additions & 2 deletions src/graphql/area/AreaQueries.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AreaType } from '../../db/AreaTypes'
import { AreaType, BulkAreasGQLQueryInput } from '../../db/AreaTypes'
import { GQLContext } from '../../types'

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

bulkAreas: async (_: any, params, { dataSources }: GQLContext): Promise<AreaType[]> => {
const { areas } = dataSources
const { ancestors } = params as BulkAreasGQLQueryInput
return await areas.bulkDownloadAreas(ancestors)
}
}

export default AreaQueries
7 changes: 7 additions & 0 deletions src/graphql/schema/Area.gql
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
type Query {
area(uuid: ID): Area
areas(filter: Filter, sort: Sort): [Area]

"""
Bulk download an area and its children starting from ancestors paths (inclusive).
To keep payload at a reasonable size ancestors must have at least 2 elements.
"""
bulkAreas(ancestors: [String!]!): [Area]

stats: Stats
cragsNear(
placeId: String
Expand Down
14 changes: 14 additions & 0 deletions src/model/AreaDataSource.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { GraphQLError } from 'graphql'
import { ApolloServerErrorCode } from '@apollo/server/errors'
import { MongoDataSource } from 'apollo-datasource-mongodb'
import { Filter } from 'mongodb'
import muuid from 'uuid-mongodb'
Expand Down Expand Up @@ -275,4 +277,16 @@ export default class AreaDataSource extends MongoDataSource<AreaType> {
}
return await this.areaModel.find(filter).lean()
}

async bulkDownloadAreas (ancestors: string[]): Promise<AreaType[]> {
if (ancestors.length < 2) {
throw new GraphQLError('Must provide at least 2 ancestors.', {
extensions: {
code: ApolloServerErrorCode.BAD_USER_INPUT
}
})
}
const ancestorsCSV = ancestors.join(',')
return await this.findDescendantsByPath(ancestorsCSV)
}
}
Loading