Skip to content

Commit 5795287

Browse files
committed
add not-found guard to GetCampaign, refactor ListCampaignChecks to recursive pagination
Aligns GetCampaign with the empty-ID convention used by GetCategory, GetScorecard, etc. Converts ListCampaignChecks from iterative to recursive pagination to match the rest of the SDK. Made-with: Cursor
1 parent 36178f7 commit 5795287

1 file changed

Lines changed: 24 additions & 6 deletions

File tree

campaign.go

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
package opslevel
22

3+
import (
4+
"fmt"
5+
6+
"github.com/hasura/go-graphql-client"
7+
)
8+
39
type ListCampaignsVariables struct {
410
After *string
511
First *int
@@ -46,6 +52,12 @@ func (client *Client) GetCampaign(id ID) (*Campaign, error) {
4652
"id": id,
4753
}
4854
err := client.Query(&q, v, WithName("CampaignGet"))
55+
if q.Account.Campaign.Id == "" {
56+
err = graphql.Errors{graphql.Error{
57+
Message: fmt.Sprintf("campaign with ID '%s' not found", id),
58+
Path: []any{"account", "campaign"},
59+
}}
60+
}
4961
return &q.Account.Campaign, HandleErrors(err, nil)
5062
}
5163

@@ -105,7 +117,7 @@ type campaignCheckConnection struct {
105117
PageInfo PageInfo `graphql:"pageInfo"`
106118
}
107119

108-
func (client *Client) ListCampaignChecks(campaignId ID) ([]CampaignCheckNode, error) {
120+
func (client *Client) ListCampaignChecks(campaignId ID, variables ...*PayloadVariables) ([]CampaignCheckNode, error) {
109121
var q struct {
110122
Account struct {
111123
Campaign struct {
@@ -114,20 +126,26 @@ func (client *Client) ListCampaignChecks(campaignId ID) ([]CampaignCheckNode, er
114126
}
115127
}
116128

117-
pages := client.InitialPageVariablesPointer()
118-
(*pages)["id"] = campaignId
129+
var pages *PayloadVariables
130+
if len(variables) > 0 && variables[0] != nil {
131+
pages = variables[0]
132+
} else {
133+
pages = client.InitialPageVariablesPointer()
134+
(*pages)["id"] = campaignId
135+
}
119136

120137
if err := client.Query(&q, *pages, WithName("CampaignChecksList")); err != nil {
121138
return nil, err
122139
}
123140

124141
allChecks := q.Account.Campaign.Checks.Nodes
125-
for q.Account.Campaign.Checks.PageInfo.HasNextPage {
142+
if q.Account.Campaign.Checks.PageInfo.HasNextPage {
126143
(*pages)["after"] = q.Account.Campaign.Checks.PageInfo.End
127-
if err := client.Query(&q, *pages, WithName("CampaignChecksList")); err != nil {
144+
resp, err := client.ListCampaignChecks(campaignId, pages)
145+
if err != nil {
128146
return nil, err
129147
}
130-
allChecks = append(allChecks, q.Account.Campaign.Checks.Nodes...)
148+
allChecks = append(allChecks, resp...)
131149
}
132150
return allChecks, nil
133151
}

0 commit comments

Comments
 (0)