From 59ddb5607ddb518abfb5b58c20fbc16730460663 Mon Sep 17 00:00:00 2001 From: Corey Johnson Date: Tue, 15 Oct 2019 14:19:56 -0700 Subject: [PATCH] Use func var to override GraphQL calls --- api/client.go | 4 ++-- api/queries.go | 9 +-------- test/helpers.go | 5 +++-- 3 files changed, 6 insertions(+), 12 deletions(-) diff --git a/api/client.go b/api/client.go index fc6895ca1..fa96dbf0b 100644 --- a/api/client.go +++ b/api/client.go @@ -21,7 +21,7 @@ type graphQLResponse struct { } /* -graphQL usage +GraphQL: Declared as an external variable so it can be mocked in tests type repoResponse struct { Repository struct { @@ -45,7 +45,7 @@ if err != nil { fmt.Printf("%+v\n", resp) */ -func graphQL(query string, variables map[string]string, v interface{}) error { +var GraphQL = func(query string, variables map[string]string, v interface{}) error { url := "https://api.github.com/graphql" reqBody, err := json.Marshal(map[string]interface{}{"query": query, "variables": variables}) if err != nil { diff --git a/api/queries.go b/api/queries.go index 00908ae54..616be409d 100644 --- a/api/queries.go +++ b/api/queries.go @@ -21,8 +21,6 @@ type PullRequest struct { HeadRefName string } -var OverriddenQueryFunction func(query string, variables map[string]string, v interface{}) error - func PullRequests() (PullRequestsPayload, error) { type edges struct { Edges []struct { @@ -100,12 +98,7 @@ func PullRequests() (PullRequestsPayload, error) { } var resp response - var err error - if OverriddenQueryFunction != nil { - err = OverriddenQueryFunction(query, variables, &resp) - } else { - err = graphQL(query, variables, &resp) - } + err := GraphQL(query, variables, &resp) if err != nil { return PullRequestsPayload{}, err } diff --git a/test/helpers.go b/test/helpers.go index a92ebb3b3..0b6997fb0 100644 --- a/test/helpers.go +++ b/test/helpers.go @@ -74,7 +74,8 @@ func MockGraphQLResponse(fixtureName string) (teardown func()) { pwd, _ := os.Getwd() fixturePath := filepath.Join(pwd, "..", "test", "fixtures", fixtureName) - api.OverriddenQueryFunction = func(query string, variables map[string]string, v interface{}) error { + originalGraphQL := api.GraphQL + api.GraphQL = func(query string, variables map[string]string, v interface{}) error { contents, err := ioutil.ReadFile(fixturePath) if err != nil { return err @@ -89,7 +90,7 @@ func MockGraphQLResponse(fixtureName string) (teardown func()) { } return func() { - api.OverriddenQueryFunction = nil + api.GraphQL = originalGraphQL } }