Skip to content

Commit 5910a35

Browse files
authored
fix: handle GraphQL errors from OpenCollective API (#119)
1 parent f410756 commit 5910a35

1 file changed

Lines changed: 16 additions & 10 deletions

File tree

src/providers/opencollective.ts

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -56,18 +56,21 @@ export async function fetchOpenCollectiveSponsors(
5656
offset = 0
5757
do {
5858
const query = makeSubscriptionsQuery(id, slug, githubHandle, offset, !includePastSponsors)
59-
const data = await $fetch(API, {
59+
const { data, errors } = await $fetch(API, {
6060
method: 'POST',
6161
body: { query },
6262
headers: {
6363
'Api-Key': `${key}`,
6464
'Content-Type': 'application/json',
6565
},
66-
}) as any
67-
const nodes = data.data.account.orders.nodes
68-
const totalCount = data.data.account.orders.totalCount
66+
})
67+
if (errors?.length) {
68+
throw new Error(`OpenCollective subscriptions query errors: ${errors.map((e: Error) => e.message).join('; ')}`)
69+
}
70+
const nodes = data.account.orders.nodes || []
71+
const totalCount = data.account.orders.totalCount
6972

70-
sponsors.push(...(nodes || []))
73+
sponsors.push(...nodes)
7174

7275
if ((nodes.length) !== 0) {
7376
if (totalCount > offset + nodes.length)
@@ -86,18 +89,21 @@ export async function fetchOpenCollectiveSponsors(
8689
? undefined
8790
: new Date(now.getFullYear(), now.getMonth(), 1)
8891
const query = makeTransactionsQuery(id, slug, githubHandle, offset, dateFrom, undefined, sponseesMode)
89-
const data = await $fetch(API, {
92+
const { data, errors } = await $fetch(API, {
9093
method: 'POST',
9194
body: { query },
9295
headers: {
9396
'Api-Key': `${key}`,
9497
'Content-Type': 'application/json',
9598
},
96-
}) as any
97-
const nodes = data.data.account.transactions.nodes
98-
const totalCount = data.data.account.transactions.totalCount
99+
})
100+
if (errors?.length) {
101+
throw new Error(`OpenCollective transactions query errors: ${errors.map((e: Error) => e.message).join('; ')}`)
102+
}
103+
const nodes = data.account.transactions.nodes || []
104+
const totalCount = data.account.transactions.totalCount
99105

100-
monthlyTransactions.push(...(nodes || []))
106+
monthlyTransactions.push(...nodes)
101107
if ((nodes.length) !== 0) {
102108
if (totalCount > offset + nodes.length)
103109
offset += nodes.length

0 commit comments

Comments
 (0)