graphql icon indicating copy to clipboard operation
graphql copied to clipboard

Take an interface for http.Client

Open jlordiales opened this issue 5 years ago • 2 comments

It would be nice if https://github.com/machinebox/graphql/blob/master/graphql.go#L48 could take a Doer interface, compatible with the std lib *http.Client so that clients can pass in whatever http client they want:

type Doer interface {
	Do(req *http.Request) (*http.Response, error)
}

This shouldn't break any clients using the std lib today.

I'd be happy to create a PR if you don't see any issue with the change.

jlordiales avatar May 22 '19 20:05 jlordiales

Just FYI, You can pass in a Transport to go HTTP client to do exactly the same. Like in this repo: https://github.com/sunfmin/handlertransport, I make the HTTP client doesn't request a remote address but go through a local HTTP handler.

	client := &http.Client{
	    Transport: handlertransport.New(http.HandlerFunc(hf)),
	}
	

sunfmin avatar May 30 '19 05:05 sunfmin

@sunfmin yeah, RoundTripper is nice for simple use cases but it has some important limitations/expectations for a general http client middleware:

  • RoundTrip should not modify the request, except for consuming and closing the Request's Body
  • RoundTrip should not attempt to handle higher-level protocol details such as redirects, authentication, or cookies

Taking a Doer interface instead of a *http.Client would still allow you to use the std lib client with a custom transport though.

jlordiales avatar May 30 '19 19:05 jlordiales