Skip to content

Commit

Permalink
Merge pull request #3603 from ActiveState/DX-3162
Browse files Browse the repository at this point in the history
Unify graphQL client code
  • Loading branch information
MDrakos authored Nov 26, 2024
2 parents bcc739d + f50e832 commit 2243c42
Show file tree
Hide file tree
Showing 9 changed files with 441 additions and 456 deletions.
443 changes: 355 additions & 88 deletions internal/gqlclient/gqlclient.go

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package graphql
package gqlclient

import (
"context"
Expand Down Expand Up @@ -32,12 +32,12 @@ func TestDoJSON(t *testing.T) {
defer srv.Close()

ctx := context.Background()
client := NewClient(srv.URL)
client := newClient(srv.URL)

ctx, cancel := context.WithTimeout(ctx, 1*time.Second)
defer cancel()
var responseData map[string]interface{}
err := client.Run(ctx, &Request{q: "query {}"}, &responseData)
err := client.RunWithContext(ctx, &TestRequest{"query {}", nil, nil}, &responseData)
is.NoErr(err)
is.Equal(calls, 1) // calls
is.Equal(responseData["something"], "yes")
Expand All @@ -59,10 +59,10 @@ func TestQueryJSON(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
defer cancel()

client := NewClient(srv.URL)
client := newClient(srv.URL)

req := NewRequest("query {}")
req.Var("username", "matryer")
req := NewTestRequest("query {}")
req.vars["username"] = "matryer"

// check variables
is.True(req != nil)
Expand All @@ -71,7 +71,7 @@ func TestQueryJSON(t *testing.T) {
var resp struct {
Value string
}
err := client.Run(ctx, req, &resp)
err := client.RunWithContext(ctx, req, &resp)
is.NoErr(err)
is.Equal(calls, 1)

Expand Down Expand Up @@ -99,15 +99,14 @@ func TestHeader(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
defer cancel()

client := NewClient(srv.URL)

req := NewRequest("query {}")
req.Header.Set("X-Custom-Header", "123")
client := newClient(srv.URL)

req := NewTestRequest("query {}")
req.headers["X-Custom-Header"] = []string{"123"}
var resp struct {
Value string
}
err := client.Run(ctx, req, &resp)
err := client.RunWithContext(ctx, req, &resp)
is.NoErr(err)
is.Equal(calls, 1)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package graphql
package gqlclient

import (
"context"
Expand Down Expand Up @@ -27,10 +27,9 @@ func TestWithClient(t *testing.T) {
}

ctx := context.Background()
client := NewClient("", WithHTTPClient(testClient), UseMultipartForm())
client := newClient("", WithHTTPClient(testClient), UseMultipartForm())

req := NewRequest(``)
client.Run(ctx, req, nil)
client.RunWithContext(ctx, NewTestRequest("query {}"), nil)

is.Equal(calls, 1) // calls
}
Expand All @@ -54,12 +53,12 @@ func TestDoUseMultipartForm(t *testing.T) {
defer srv.Close()

ctx := context.Background()
client := NewClient(srv.URL, UseMultipartForm())
client := newClient(srv.URL, UseMultipartForm())

ctx, cancel := context.WithTimeout(ctx, 1*time.Second)
defer cancel()
var responseData map[string]interface{}
err := client.Run(ctx, &Request{q: "query {}"}, &responseData)
err := client.RunWithContext(ctx, NewTestRequest("query {}"), &responseData)
is.NoErr(err)
is.Equal(calls, 1) // calls
is.Equal(responseData["something"], "yes")
Expand All @@ -82,12 +81,12 @@ func TestDoErr(t *testing.T) {
defer srv.Close()

ctx := context.Background()
client := NewClient(srv.URL, UseMultipartForm())
client := newClient(srv.URL, UseMultipartForm())

ctx, cancel := context.WithTimeout(ctx, 1*time.Second)
defer cancel()
var responseData map[string]interface{}
err := client.Run(ctx, &Request{q: "query {}"}, &responseData)
err := client.RunWithContext(ctx, NewTestRequest("query {}"), &responseData)
is.True(err != nil)
is.Equal(err.Error(), "graphql: Something went wrong")
}
Expand All @@ -109,11 +108,11 @@ func TestDoNoResponse(t *testing.T) {
defer srv.Close()

ctx := context.Background()
client := NewClient(srv.URL, UseMultipartForm())
client := newClient(srv.URL, UseMultipartForm())

ctx, cancel := context.WithTimeout(ctx, 1*time.Second)
defer cancel()
err := client.Run(ctx, &Request{q: "query {}"}, nil)
err := client.RunWithContext(ctx, NewTestRequest("query {}"), nil)
is.NoErr(err)
is.Equal(calls, 1) // calls
}
Expand All @@ -134,10 +133,10 @@ func TestQuery(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
defer cancel()

client := NewClient(srv.URL, UseMultipartForm())
client := newClient(srv.URL, UseMultipartForm())

req := NewRequest("query {}")
req.Var("username", "matryer")
req := NewTestRequest("query {}")
req.vars["username"] = "matryer"

// check variables
is.True(req != nil)
Expand All @@ -146,7 +145,7 @@ func TestQuery(t *testing.T) {
var resp struct {
Value string
}
err := client.Run(ctx, req, &resp)
err := client.RunWithContext(ctx, req, &resp)
is.NoErr(err)
is.Equal(calls, 1)

Expand All @@ -160,7 +159,7 @@ func TestFile(t *testing.T) {
var calls int
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
calls++
file, header, err := r.FormFile("file")
file, header, err := r.FormFile("0")
is.NoErr(err)
defer file.Close()
is.Equal(header.Filename, "filename.txt")
Expand All @@ -175,11 +174,12 @@ func TestFile(t *testing.T) {
defer srv.Close()
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
defer cancel()
client := NewClient(srv.URL, UseMultipartForm())
client := newClient(srv.URL, UseMultipartForm())
f := strings.NewReader(`This is a file`)
req := NewRequest("query {}")
req.File("file", "filename.txt", f)
err := client.Run(ctx, req, nil)
req := NewTestRequestWithFiles("query {}")
req.files = append(req.files, File{Field: "file", Name: "filename.txt", R: f})
var resp string
err := client.RunWithContext(ctx, req, &resp)
is.NoErr(err)
}

Expand Down
43 changes: 43 additions & 0 deletions internal/gqlclient/gqlclient_shared_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package gqlclient

type TestRequest struct {
q string
vars map[string]interface{}
headers map[string][]string
}

func NewTestRequest(q string) *TestRequest {
return &TestRequest{
q: q,
vars: make(map[string]interface{}),
headers: make(map[string][]string),
}
}

func (r *TestRequest) Query() string {
return r.q
}

func (r *TestRequest) Vars() (map[string]interface{}, error) {
return r.vars, nil
}

func (r *TestRequest) Headers() map[string][]string {
return r.headers
}

type TestRequestWithFiles struct {
*TestRequest
files []File
}

func NewTestRequestWithFiles(q string) *TestRequestWithFiles {
return &TestRequestWithFiles{
TestRequest: NewTestRequest(q),
files: make([]File, 0),
}
}

func (r *TestRequestWithFiles) Files() []File {
return r.files
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package graphql
package gqlclient

import (
"context"
Expand All @@ -25,7 +25,7 @@ func TestClient_SingleField(t *testing.T) {
}))
defer server.Close()

client := NewClient(server.URL)
client := newClient(server.URL)

tests := []struct {
name string
Expand Down Expand Up @@ -104,10 +104,10 @@ func TestClient_SingleField(t *testing.T) {

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
req := NewRequest(tt.query)
req := NewTestRequest(tt.query)

var resp interface{}
err := client.Run(context.Background(), req, &resp)
err := client.RunWithContext(context.Background(), req, &resp)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
Expand Down Expand Up @@ -145,7 +145,7 @@ func TestClient_MultipleFields(t *testing.T) {
}))
defer server.Close()

client := NewClient(server.URL)
client := newClient(server.URL)

tests := []struct {
name string
Expand Down Expand Up @@ -179,10 +179,10 @@ func TestClient_MultipleFields(t *testing.T) {

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
req := NewRequest(tt.query)
req := NewTestRequest(tt.query)

var resp interface{}
err := client.Run(context.Background(), req, &resp)
err := client.RunWithContext(context.Background(), req, &resp)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
Expand Down
Loading

0 comments on commit 2243c42

Please sign in to comment.