machinebox / graphql

Simple low-level GraphQL HTTP client for Go
https://blog.machinebox.io/a-graphql-client-library-for-go-5bffd0455878
Apache License 2.0
933 stars 217 forks source link

Take an interface for http.Client #33

Open jlordiales opened 5 years ago

jlordiales commented 5 years ago

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.

sunfmin commented 5 years ago

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)),
    }
jlordiales commented 5 years ago

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

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