Use func var to override GraphQL calls
This commit is contained in:
parent
fb290313e3
commit
59ddb5607d
3 changed files with 6 additions and 12 deletions
|
|
@ -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 {
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue