Skip to content

Commit

Permalink
perf(gatsby): Optimise distinct queries (#30457)
Browse files Browse the repository at this point in the history
  • Loading branch information
ascorbic authored Mar 25, 2021
1 parent 4571d2b commit f462e23
Showing 1 changed file with 15 additions and 6 deletions.
21 changes: 15 additions & 6 deletions packages/gatsby/src/schema/resolvers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,14 +114,23 @@ export const distinct: GatsbyResolver<
> = function distinctResolver(source, args): Array<string> {
const { field } = args
const { edges } = source
const values = edges.reduce((acc, { node }) => {

const values = new Set<string>()
edges.forEach(({ node }) => {
const value =
getValueAt(node, `__gatsby_resolved.${field}`) || getValueAt(node, field)
return value != null
? acc.concat(value instanceof Date ? value.toISOString() : value)
: acc
}, [])
return Array.from(new Set(values)).sort()
if (value === null || value === undefined) {
return
}
if (Array.isArray(value)) {
value.forEach(subValue => values.add(subValue))
} else if (value instanceof Date) {
values.add(value.toISOString())
} else {
values.add(value)
}
})
return Array.from(values).sort()
}

type IGatsbyGroupReturnValue<NodeType> = Array<
Expand Down

0 comments on commit f462e23

Please sign in to comment.