Skip to content

Commit 09bed07

Browse files
committed
changed the way WithClient works
1 parent ca94881 commit 09bed07

File tree

3 files changed

+11
-22
lines changed

3 files changed

+11
-22
lines changed

client.go

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@ import (
1515

1616
// Client accesses a GraphQL API.
1717
type client struct {
18-
endpoint string
19-
httpclient *http.Client
18+
endpoint string
2019
}
2120

2221
// Do executes a query request and returns the response.
@@ -67,8 +66,8 @@ func (c *client) Do(ctx context.Context, request *Request, response interface{})
6766
}
6867
req.Header.Set("Content-Type", writer.FormDataContentType())
6968
req.Header.Set("Accept", "application/json")
70-
client := c.httpclient
71-
if client == nil {
69+
client, ok := ctx.Value(httpclientContextKey).(*http.Client)
70+
if !ok {
7271
client = http.DefaultClient
7372
}
7473
res, err := ctxhttp.Do(ctx, client, req)
@@ -92,13 +91,7 @@ func (c *client) Do(ctx context.Context, request *Request, response interface{})
9291

9392
// WithClient specifies the http.Client that requests will use.
9493
func WithClient(ctx context.Context, client *http.Client) context.Context {
95-
c, err := fromContext(ctx)
96-
if err != nil {
97-
// can't set it, fail silently
98-
return ctx
99-
}
100-
c.httpclient = client
101-
return ctx
94+
return context.WithValue(ctx, httpclientContextKey, client)
10295
}
10396

10497
type graphErr struct {

client_test.go

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,6 @@ func TestDo(t *testing.T) {
5252
defer srv.Close()
5353
c := &client{
5454
endpoint: srv.URL,
55-
httpclient: &http.Client{
56-
Timeout: 1 * time.Second,
57-
},
5855
}
5956
ctx := context.Background()
6057
ctx, cancel := context.WithTimeout(ctx, 1*time.Second)
@@ -83,9 +80,6 @@ func TestDoErr(t *testing.T) {
8380
defer srv.Close()
8481
c := &client{
8582
endpoint: srv.URL,
86-
httpclient: &http.Client{
87-
Timeout: 1 * time.Second,
88-
},
8983
}
9084
ctx := context.Background()
9185
ctx, cancel := context.WithTimeout(ctx, 1*time.Second)
@@ -113,9 +107,6 @@ func TestDoNoResponse(t *testing.T) {
113107
defer srv.Close()
114108
c := &client{
115109
endpoint: srv.URL,
116-
httpclient: &http.Client{
117-
Timeout: 1 * time.Second,
118-
},
119110
}
120111
ctx := context.Background()
121112
ctx, cancel := context.WithTimeout(ctx, 1*time.Second)

context.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,13 @@ var errInappropriateContext = errors.New("inappropriate context")
1313
// contextKey provides unique keys for context values.
1414
type contextKey string
1515

16-
// clientContextKey is the context value key for the Client.
17-
var clientContextKey = contextKey("graphql client context")
16+
var (
17+
// clientContextKey is the context value key for the Client.
18+
clientContextKey = contextKey("graphql client context key")
19+
// httpclientContextKey is the context value key for the HTTP client to
20+
// use.
21+
httpclientContextKey = contextKey("graphql http client context key")
22+
)
1823

1924
// fromContext gets the client from the specified
2025
// Context.

0 commit comments

Comments
 (0)