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

Sort area children by leftRightIndex #296

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from
Open

Conversation

zichongkao
Copy link
Contributor

"Area" graphql objects have a children field containing other areas or climbs. Currently these are unsorted -- we leave the frontend to sort them. But the frontend tends to sort them the same way based on their leftRightIndex so why not just return that as a default order returned by the backend? This way frontends won't have to do any sorting.

In response to issue: #294 and first discussed in this PR: OpenBeta/open-tacos#696 (comment)

@@ -21,3 +23,17 @@ export function exhaustiveCheck (_value: never): never {

export const geojsonPointToLongitude = (point: Point): number => point.coordinates[0]
export const geojsonPointToLatitude = (point: Point): number => point.coordinates[1]

export function compareAreaLeftRightIndex (a: AreaType, b: AreaType): number {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I considered merging these two, but then I'd need to be able to narrow the type of a and b. And that's not very easy since these are custom types, not just strings or numbers. And so it gets a bit messy. I figure just splitting them out is a bit repetitive but a lot more readable.

@zichongkao zichongkao force-pushed the kao-leftrightsort branch 2 times, most recently from 3fbd76e to 6320b57 Compare May 21, 2023 07:03
@@ -202,7 +202,7 @@ const resolvers = {

children: async (parent: AreaType, _, { dataSources: { areas } }: Context) => {
if (parent.children.length > 0) {
return await areas.findManyByIds(parent.children)
return (await areas.findManyByIds(parent.children)).sort(compareAreaLeftRightIndex)
Copy link
Contributor

@vnugent vnugent May 21, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think if we let Mongo handle the sorting here and in findManyClimbsByUuids(). This way we don't have to write comparators.
Something like:

// inside AreaDataSource
 ...
 return await this.areaModel.find().
   where('metadata.leftRightIndex').
   in(parent.children).
   sort({ 'metadata.leftRightIndex': 1 }).lean()

edit: add lean()

@zichongkao
Copy link
Contributor Author

zichongkao commented May 21, 2023 via email

},
{
$set: {
climbs: { $sortArray: { input: '$climbs', sortBy: { 'metadata.left_right_index': 1 } } },
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$sortArray requires Mongo 5.2 and up. Otherwise we need something like this: https://stackoverflow.com/questions/15388127/mongodb-sort-inner-array

{
$set: {
climbs: { $sortArray: { input: '$climbs', sortBy: { 'metadata.left_right_index': 1 } } },
children: { $sortArray: { input: '$children', sortBy: { 'metadata.leftRightIndex': 1 } } }
Copy link
Contributor

@vnugent vnugent May 23, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you confirm children[] contains actual area objects? I think it only contains a list of area _id's. When we ask for an area.children[] in GQL, we're making another DB query here:
https://github.com/OpenBeta/openbeta-graphql/blob/develop/src/graphql/resolvers.ts#L203

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh yeah you're right! I've not tested this far because it throws a "No such operation as $sortArray" error.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if we did a $lookup for children above this $set so that we have the full object?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if we did a $lookup for children above this $set so that we have the full object?

I thought the query would get too wildly complicated for a small performance gain, whereas the GQL field resolution way in my comment above is more elegant and simpler (but requires an additional query).

@vnugent
Copy link
Contributor

vnugent commented May 24, 2023

I'd be content with whatever solution you come with that doesn't require a DB upgrade, or we can leave this PR open until we do the upgrade.

@zichongkao
Copy link
Contributor Author

Agree that we can hold this back until we have time to upgrade Mongo to v6 as tracked here: #300

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants