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

Run findMany and aggregate simultaneously #813

Merged
Merged
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
24 changes: 18 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,12 @@ export async function findManyCursorConnection<
const skip = cursor ? 1 : undefined

// Execute the underlying query operations
records = await findMany({ cursor, take, skip })
totalCount = hasRequestedField('totalCount') ? await aggregate() : -1
const results = await Promise.all([
findMany({ cursor, take, skip }),
hasRequestedField('totalCount') ? aggregate() : Promise.resolve(-1),
])
records = results[0]
totalCount = results[1]

// See if we are "after" another record, indicating a previous page
hasPreviousPage = !!args.after
Expand All @@ -64,8 +68,12 @@ export async function findManyCursorConnection<
const skip = cursor ? 1 : undefined

// Execute the underlying query operations
records = await findMany({ cursor, take, skip })
totalCount = hasRequestedField('totalCount') ? await aggregate() : -1
const results = await Promise.all([
findMany({ cursor, take, skip }),
hasRequestedField('totalCount') ? aggregate() : Promise.resolve(-1),
])
records = results[0]
totalCount = results[1]

// See if we are "before" another record, indicating a next page
hasNextPage = !!args.before
Expand All @@ -77,8 +85,12 @@ export async function findManyCursorConnection<
if (hasPreviousPage) records.shift()
} else {
// Execute the underlying query operations
records = hasRequestedField('edges') || hasRequestedField('nodes') ? await findMany({}) : []
totalCount = hasRequestedField('totalCount') ? await aggregate() : -1
const results = await Promise.all([
hasRequestedField('edges') || hasRequestedField('nodes') ? findMany({}) : Promise.resolve([]),
hasRequestedField('totalCount') ? aggregate() : Promise.resolve(-1),
])
records = results[0]
totalCount = results[1]

// Since we are getting all records, there are no pages
hasNextPage = false
Expand Down