Skip to content

Commit

Permalink
Merge branch 'Kareem-Medhat-promise-all-findmany-aggregate'
Browse files Browse the repository at this point in the history
  • Loading branch information
queicherius committed Nov 20, 2023
2 parents 8362b79 + 477d53e commit cdb8e64
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 6 deletions.
7 changes: 7 additions & 0 deletions .all-contributorsrc
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,13 @@
"avatar_url": "https://avatars.githubusercontent.com/u/49038?v=4",
"profile": "https://orta.io/",
"contributions": ["code", "doc", "test"]
},
{
"login": "Kareem-Medhat",
"name": "Kareem-Medhat",
"avatar_url": "https://avatars.githubusercontent.com/u/39652808?v=4",
"profile": "https://github.com/Kareem-Medhat",
"contributions": ["code"]
}
],
"files": ["README.md"],
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/d
<td align="center" valign="top" width="14.28%"><a href="https://kervin.tech/"><img src="https://avatars.githubusercontent.com/u/15281277?v=4?s=75" width="75px;" alt="Kervin Vasquez"/><br /><sub><b>Kervin Vasquez</b></sub></a><br /><a href="https://github.com/devoxa/prisma-relay-cursor-connection/commits?author=kervin5" title="Code">💻</a> <a href="https://github.com/devoxa/prisma-relay-cursor-connection/commits?author=kervin5" title="Tests">⚠️</a></td>
<td align="center" valign="top" width="14.28%"><a href="http://stackoverflow.com/users/515932/jeff-gu-kang?tab=profile"><img src="https://avatars.githubusercontent.com/u/216363?v=4?s=75" width="75px;" alt="Jeff Gu Kang"/><br /><sub><b>Jeff Gu Kang</b></sub></a><br /><a href="https://github.com/devoxa/prisma-relay-cursor-connection/commits?author=JeffGuKang" title="Documentation">📖</a></td>
<td align="center" valign="top" width="14.28%"><a href="https://orta.io/"><img src="https://avatars.githubusercontent.com/u/49038?v=4?s=75" width="75px;" alt="Orta Therox"/><br /><sub><b>Orta Therox</b></sub></a><br /><a href="https://github.com/devoxa/prisma-relay-cursor-connection/commits?author=orta" title="Code">💻</a> <a href="https://github.com/devoxa/prisma-relay-cursor-connection/commits?author=orta" title="Documentation">📖</a> <a href="https://github.com/devoxa/prisma-relay-cursor-connection/commits?author=orta" title="Tests">⚠️</a></td>
<td align="center" valign="top" width="14.28%"><a href="https://github.com/Kareem-Medhat"><img src="https://avatars.githubusercontent.com/u/39652808?v=4?s=75" width="75px;" alt="Kareem-Medhat"/><br /><sub><b>Kareem-Medhat</b></sub></a><br /><a href="https://github.com/devoxa/prisma-relay-cursor-connection/commits?author=Kareem-Medhat" title="Code">💻</a></td>
</tr>
</tbody>
</table>
Expand Down
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

0 comments on commit cdb8e64

Please sign in to comment.