Skip to content
This repository has been archived by the owner on Jun 21, 2023. It is now read-only.

Commit

Permalink
refactor queries (#81)
Browse files Browse the repository at this point in the history
* this was already set

* make a generic paginate and use with ProjectItems

* use a single Project struct type

* generic paginate ProjectFields

* fix tests

* cleanup

* add missing query variables

* projectAttribute type constraint

* fix tests

* do not fetch not needed fields and items

* explanation

* pass around pointers to avoid copying

* add queries_test.go

* bug fix

* do not request data that will not be shown

* begone magic numbers

* add type constraint to pager
  • Loading branch information
mntlty authored Mar 23, 2023
1 parent e4e74de commit 4507ef5
Show file tree
Hide file tree
Showing 30 changed files with 899 additions and 343 deletions.
6 changes: 5 additions & 1 deletion cmd/close/close.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func runClose(config closeConfig) error {
return err
}

project, err := queries.NewProject(config.client, owner, config.opts.number)
project, err := queries.NewProject(config.client, owner, config.opts.number, false)
if err != nil {
return err
}
Expand All @@ -131,6 +131,10 @@ func closeArgs(config closeConfig) (*updateProjectMutation, map[string]interface
ProjectID: githubv4.ID(config.opts.projectID),
Closed: githubv4.NewBoolean(githubv4.Boolean(closed)),
},
"firstItems": githubv4.Int(0),
"afterItems": (*githubv4.String)(nil),
"firstFields": githubv4.Int(0),
"afterFields": (*githubv4.String)(nil),
}
}

Expand Down
38 changes: 27 additions & 11 deletions cmd/close/close_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,12 @@ func TestRunClose_User(t *testing.T) {
JSON(map[string]interface{}{
"query": "query UserProject.*",
"variables": map[string]interface{}{
"login": "monalisa",
"number": 1,
"login": "monalisa",
"number": 1,
"firstItems": 0,
"afterItems": nil,
"firstFields": 0,
"afterFields": nil,
},
}).
Reply(200).
Expand All @@ -59,7 +63,7 @@ func TestRunClose_User(t *testing.T) {
// close project
gock.New("https://api.github.com").
Post("/graphql").
BodyString(`{"query":"mutation CloseProjectV2.*"variables":{"input":{"projectId":"an ID","closed":true}}}`).
BodyString(`{"query":"mutation CloseProjectV2.*"variables":{"afterFields":null,"afterItems":null,"firstFields":0,"firstItems":0,"input":{"projectId":"an ID","closed":true}}}`).
Reply(200).
JSON(map[string]interface{}{
"data": map[string]interface{}{
Expand Down Expand Up @@ -126,8 +130,12 @@ func TestRunClose_Org(t *testing.T) {
JSON(map[string]interface{}{
"query": "query OrgProject.*",
"variables": map[string]interface{}{
"login": "github",
"number": 1,
"login": "github",
"number": 1,
"firstItems": 0,
"afterItems": nil,
"firstFields": 0,
"afterFields": nil,
},
}).
Reply(200).
Expand All @@ -144,7 +152,7 @@ func TestRunClose_Org(t *testing.T) {
// close project
gock.New("https://api.github.com").
Post("/graphql").
BodyString(`{"query":"mutation CloseProjectV2.*"variables":{"input":{"projectId":"an ID","closed":true}}}`).
BodyString(`{"query":"mutation CloseProjectV2.*"variables":{"afterFields":null,"afterItems":null,"firstFields":0,"firstItems":0,"input":{"projectId":"an ID","closed":true}}}`).
Reply(200).
JSON(map[string]interface{}{
"data": map[string]interface{}{
Expand Down Expand Up @@ -208,7 +216,11 @@ func TestRunClose_Me(t *testing.T) {
JSON(map[string]interface{}{
"query": "query ViewerProject.*",
"variables": map[string]interface{}{
"number": 1,
"number": 1,
"firstItems": 0,
"afterItems": nil,
"firstFields": 0,
"afterFields": nil,
},
}).
Reply(200).
Expand All @@ -225,7 +237,7 @@ func TestRunClose_Me(t *testing.T) {
// close project
gock.New("https://api.github.com").
Post("/graphql").
BodyString(`{"query":"mutation CloseProjectV2.*"variables":{"input":{"projectId":"an ID","closed":true}}}`).
BodyString(`{"query":"mutation CloseProjectV2.*"variables":{"afterFields":null,"afterItems":null,"firstFields":0,"firstItems":0,"input":{"projectId":"an ID","closed":true}}}`).
Reply(200).
JSON(map[string]interface{}{
"data": map[string]interface{}{
Expand Down Expand Up @@ -292,8 +304,12 @@ func TestRunClose_Reopen(t *testing.T) {
JSON(map[string]interface{}{
"query": "query UserProject.*",
"variables": map[string]interface{}{
"login": "monalisa",
"number": 1,
"login": "monalisa",
"number": 1,
"firstItems": 0,
"afterItems": nil,
"firstFields": 0,
"afterFields": nil,
},
}).
Reply(200).
Expand All @@ -310,7 +326,7 @@ func TestRunClose_Reopen(t *testing.T) {
// close project
gock.New("https://api.github.com").
Post("/graphql").
BodyString(`{"query":"mutation CloseProjectV2.*"variables":{"input":{"projectId":"an ID","closed":false}}}`).
BodyString(`{"query":"mutation CloseProjectV2.*"variables":{"afterFields":null,"afterItems":null,"firstFields":0,"firstItems":0,"input":{"projectId":"an ID","closed":false}}}`).
Reply(200).
JSON(map[string]interface{}{
"data": map[string]interface{}{
Expand Down
6 changes: 5 additions & 1 deletion cmd/copy/copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ func runCopy(config copyConfig) error {
return err
}

project, err := queries.NewProject(config.client, sourceOwner, config.opts.number)
project, err := queries.NewProject(config.client, sourceOwner, config.opts.number, false)
if err != nil {
return err
}
Expand Down Expand Up @@ -148,6 +148,10 @@ func copyArgs(config copyConfig) (*copyProjectMutation, map[string]interface{})
Title: githubv4.String(config.opts.title),
IncludeDraftIssues: githubv4.NewBoolean(githubv4.Boolean(config.opts.includeDraftIssues)),
},
"firstItems": githubv4.Int(0),
"afterItems": (*githubv4.String)(nil),
"firstFields": githubv4.Int(0),
"afterFields": (*githubv4.String)(nil),
}
}

Expand Down
28 changes: 20 additions & 8 deletions cmd/copy/copy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,12 @@ func TestRunCopy_User(t *testing.T) {
JSON(map[string]interface{}{
"query": "query UserProject.*",
"variables": map[string]interface{}{
"login": "monalisa",
"number": 1,
"login": "monalisa",
"number": 1,
"firstItems": 0,
"afterItems": nil,
"firstFields": 0,
"afterFields": nil,
},
}).
Reply(200).
Expand Down Expand Up @@ -80,7 +84,7 @@ func TestRunCopy_User(t *testing.T) {
// Copy project
gock.New("https://api.github.com").
Post("/graphql").
BodyString(`{"query":"mutation CopyProjectV2.*","variables":{"input":{"projectId":"an ID","ownerId":"an ID","title":"a title","includeDraftIssues":false}}}`).
BodyString(`{"query":"mutation CopyProjectV2.*","variables":{"afterFields":null,"afterItems":null,"firstFields":0,"firstItems":0,"input":{"projectId":"an ID","ownerId":"an ID","title":"a title","includeDraftIssues":false}}}`).
Reply(200).
JSON(map[string]interface{}{
"data": map[string]interface{}{
Expand Down Expand Up @@ -130,8 +134,12 @@ func TestRunCopy_Org(t *testing.T) {
JSON(map[string]interface{}{
"query": "query OrgProject.*",
"variables": map[string]interface{}{
"login": "github",
"number": 1,
"login": "github",
"number": 1,
"firstItems": 0,
"afterItems": nil,
"firstFields": 0,
"afterFields": nil,
},
}).
Reply(200).
Expand Down Expand Up @@ -187,7 +195,7 @@ func TestRunCopy_Org(t *testing.T) {
// Copy project
gock.New("https://api.github.com").
Post("/graphql").
BodyString(`{"query":"mutation CopyProjectV2.*","variables":{"input":{"projectId":"an ID","ownerId":"an ID","title":"a title","includeDraftIssues":false}}}`).
BodyString(`{"query":"mutation CopyProjectV2.*","variables":{"afterFields":null,"afterItems":null,"firstFields":0,"firstItems":0,"input":{"projectId":"an ID","ownerId":"an ID","title":"a title","includeDraftIssues":false}}}`).
Reply(200).
JSON(map[string]interface{}{
"data": map[string]interface{}{
Expand Down Expand Up @@ -236,7 +244,11 @@ func TestRunCopy_Me(t *testing.T) {
JSON(map[string]interface{}{
"query": "query ViewerProject.*",
"variables": map[string]interface{}{
"number": 1,
"number": 1,
"firstItems": 0,
"afterItems": nil,
"firstFields": 0,
"afterFields": nil,
},
}).
Reply(200).
Expand Down Expand Up @@ -287,7 +299,7 @@ func TestRunCopy_Me(t *testing.T) {
// Copy project
gock.New("https://api.github.com").
Post("/graphql").
BodyString(`{"query":"mutation CopyProjectV2.*","variables":{"input":{"projectId":"an ID","ownerId":"an ID","title":"a title","includeDraftIssues":false}}}`).Reply(200).
BodyString(`{"query":"mutation CopyProjectV2.*","variables":{"afterFields":null,"afterItems":null,"firstFields":0,"firstItems":0,"input":{"projectId":"an ID","ownerId":"an ID","title":"a title","includeDraftIssues":false}}}`).Reply(200).
JSON(map[string]interface{}{
"data": map[string]interface{}{
"copyProjectV2": map[string]interface{}{
Expand Down
4 changes: 4 additions & 0 deletions cmd/create/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,10 @@ func createArgs(config createConfig) (*createProjectMutation, map[string]interfa
OwnerID: githubv4.ID(config.opts.ownerID),
Title: githubv4.String(config.opts.title),
},
"firstItems": githubv4.Int(0),
"afterItems": (*githubv4.String)(nil),
"firstFields": githubv4.Int(0),
"afterFields": (*githubv4.String)(nil),
}
}

Expand Down
6 changes: 3 additions & 3 deletions cmd/create/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func TestRunCreate_User(t *testing.T) {
// create project
gock.New("https://api.github.com").
Post("/graphql").
BodyString(`{"query":"mutation CreateProjectV2.*"variables":{"input":{"ownerId":"an ID","title":"a title"}}}`).
BodyString(`{"query":"mutation CreateProjectV2.*"variables":{"afterFields":null,"afterItems":null,"firstFields":0,"firstItems":0,"input":{"ownerId":"an ID","title":"a title"}}}`).
Reply(200).
JSON(map[string]interface{}{
"data": map[string]interface{}{
Expand Down Expand Up @@ -101,7 +101,7 @@ func TestRunCreate_Org(t *testing.T) {
// create project
gock.New("https://api.github.com").
Post("/graphql").
BodyString(`{"query":"mutation CreateProjectV2.*"variables":{"input":{"ownerId":"an ID","title":"a title"}}}`).Reply(200).
BodyString(`{"query":"mutation CreateProjectV2.*"variables":{"afterFields":null,"afterItems":null,"firstFields":0,"firstItems":0,"input":{"ownerId":"an ID","title":"a title"}}}`).Reply(200).
JSON(map[string]interface{}{
"data": map[string]interface{}{
"createProjectV2": map[string]interface{}{
Expand Down Expand Up @@ -160,7 +160,7 @@ func TestRunCreate_Me(t *testing.T) {
// create project
gock.New("https://api.github.com").
Post("/graphql").
BodyString(`{"query":"mutation CreateProjectV2.*"variables":{"input":{"ownerId":"an ID","title":"a title"}}}`).Reply(200).
BodyString(`{"query":"mutation CreateProjectV2.*"variables":{"afterFields":null,"afterItems":null,"firstFields":0,"firstItems":0,"input":{"ownerId":"an ID","title":"a title"}}}`).Reply(200).
JSON(map[string]interface{}{
"data": map[string]interface{}{
"createProjectV2": map[string]interface{}{
Expand Down
6 changes: 5 additions & 1 deletion cmd/delete/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func runDelete(config deleteConfig) error {
return err
}

project, err := queries.NewProject(config.client, owner, config.opts.number)
project, err := queries.NewProject(config.client, owner, config.opts.number, false)
if err != nil {
return err
}
Expand All @@ -127,6 +127,10 @@ func deleteItemArgs(config deleteConfig) (*deleteProjectMutation, map[string]int
"input": githubv4.DeleteProjectV2Input{
ProjectID: githubv4.ID(config.opts.projectID),
},
"firstItems": githubv4.Int(0),
"afterItems": (*githubv4.String)(nil),
"firstFields": githubv4.Int(0),
"afterFields": (*githubv4.String)(nil),
}
}

Expand Down
28 changes: 20 additions & 8 deletions cmd/delete/delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,12 @@ func TestRunDelete_User(t *testing.T) {
JSON(map[string]interface{}{
"query": "query UserProject.*",
"variables": map[string]interface{}{
"login": "monalisa",
"number": 1,
"login": "monalisa",
"number": 1,
"firstItems": 0,
"afterItems": nil,
"firstFields": 0,
"afterFields": nil,
},
}).
Reply(200).
Expand All @@ -59,7 +63,7 @@ func TestRunDelete_User(t *testing.T) {
// delete project
gock.New("https://api.github.com").
Post("/graphql").
BodyString(`{"query":"mutation DeleteProject.*","variables":{"input":{"projectId":"an ID"}}}`).
BodyString(`{"query":"mutation DeleteProject.*","variables":{"afterFields":null,"afterItems":null,"firstFields":0,"firstItems":0,"input":{"projectId":"an ID"}}}`).
Reply(200).
JSON(map[string]interface{}{
"data": map[string]interface{}{
Expand Down Expand Up @@ -122,8 +126,12 @@ func TestRunDelete_Org(t *testing.T) {
JSON(map[string]interface{}{
"query": "query OrgProject.*",
"variables": map[string]interface{}{
"login": "github",
"number": 1,
"login": "github",
"number": 1,
"firstItems": 0,
"afterItems": nil,
"firstFields": 0,
"afterFields": nil,
},
}).
Reply(200).
Expand All @@ -140,7 +148,7 @@ func TestRunDelete_Org(t *testing.T) {
// delete project
gock.New("https://api.github.com").
Post("/graphql").
BodyString(`{"query":"mutation DeleteProject.*","variables":{"input":{"projectId":"an ID"}}}`).
BodyString(`{"query":"mutation DeleteProject.*","variables":{"afterFields":null,"afterItems":null,"firstFields":0,"firstItems":0,"input":{"projectId":"an ID"}}}`).
Reply(200).
JSON(map[string]interface{}{
"data": map[string]interface{}{
Expand Down Expand Up @@ -200,7 +208,11 @@ func TestRunDelete_Me(t *testing.T) {
JSON(map[string]interface{}{
"query": "query ViewerProject.*",
"variables": map[string]interface{}{
"number": 1,
"number": 1,
"firstItems": 0,
"afterItems": nil,
"firstFields": 0,
"afterFields": nil,
},
}).
Reply(200).
Expand All @@ -217,7 +229,7 @@ func TestRunDelete_Me(t *testing.T) {
// delete project
gock.New("https://api.github.com").
Post("/graphql").
BodyString(`{"query":"mutation DeleteProject.*","variables":{"input":{"projectId":"an ID"}}}`).
BodyString(`{"query":"mutation DeleteProject.*","variables":{"afterFields":null,"afterItems":null,"firstFields":0,"firstItems":0,"input":{"projectId":"an ID"}}}`).
Reply(200).
JSON(map[string]interface{}{
"data": map[string]interface{}{
Expand Down
8 changes: 6 additions & 2 deletions cmd/edit/edit.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ func runEdit(config editConfig) error {
return err
}

project, err := queries.NewProject(config.client, owner, config.opts.number)
project, err := queries.NewProject(config.client, owner, config.opts.number, false)
if err != nil {
return err
}
Expand Down Expand Up @@ -160,7 +160,11 @@ func editArgs(config editConfig) (*updateProjectMutation, map[string]interface{}
}

return &updateProjectMutation{}, map[string]interface{}{
"input": variables,
"input": variables,
"firstItems": githubv4.Int(0),
"afterItems": (*githubv4.String)(nil),
"firstFields": githubv4.Int(0),
"afterFields": (*githubv4.String)(nil),
}
}

Expand Down
Loading

0 comments on commit 4507ef5

Please sign in to comment.