X Tutup
Skip to content

Commit 59ddb56

Browse files
committed
Use func var to override GraphQL calls
1 parent fb29031 commit 59ddb56

File tree

3 files changed

+6
-12
lines changed

3 files changed

+6
-12
lines changed

api/client.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ type graphQLResponse struct {
2121
}
2222

2323
/*
24-
graphQL usage
24+
GraphQL: Declared as an external variable so it can be mocked in tests
2525
2626
type repoResponse struct {
2727
Repository struct {
@@ -45,7 +45,7 @@ if err != nil {
4545
4646
fmt.Printf("%+v\n", resp)
4747
*/
48-
func graphQL(query string, variables map[string]string, v interface{}) error {
48+
var GraphQL = func(query string, variables map[string]string, v interface{}) error {
4949
url := "https://api.github.com/graphql"
5050
reqBody, err := json.Marshal(map[string]interface{}{"query": query, "variables": variables})
5151
if err != nil {

api/queries.go

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@ type PullRequest struct {
2121
HeadRefName string
2222
}
2323

24-
var OverriddenQueryFunction func(query string, variables map[string]string, v interface{}) error
25-
2624
func PullRequests() (PullRequestsPayload, error) {
2725
type edges struct {
2826
Edges []struct {
@@ -100,12 +98,7 @@ func PullRequests() (PullRequestsPayload, error) {
10098
}
10199

102100
var resp response
103-
var err error
104-
if OverriddenQueryFunction != nil {
105-
err = OverriddenQueryFunction(query, variables, &resp)
106-
} else {
107-
err = graphQL(query, variables, &resp)
108-
}
101+
err := GraphQL(query, variables, &resp)
109102
if err != nil {
110103
return PullRequestsPayload{}, err
111104
}

test/helpers.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,8 @@ func MockGraphQLResponse(fixtureName string) (teardown func()) {
7474
pwd, _ := os.Getwd()
7575
fixturePath := filepath.Join(pwd, "..", "test", "fixtures", fixtureName)
7676

77-
api.OverriddenQueryFunction = func(query string, variables map[string]string, v interface{}) error {
77+
originalGraphQL := api.GraphQL
78+
api.GraphQL = func(query string, variables map[string]string, v interface{}) error {
7879
contents, err := ioutil.ReadFile(fixturePath)
7980
if err != nil {
8081
return err
@@ -89,7 +90,7 @@ func MockGraphQLResponse(fixtureName string) (teardown func()) {
8990
}
9091

9192
return func() {
92-
api.OverriddenQueryFunction = nil
93+
api.GraphQL = originalGraphQL
9394
}
9495
}
9596

0 commit comments

Comments
 (0)
X Tutup