graphql
graphql copied to clipboard
[QUESTION] How to print/return raw response from graphql server?
Hi!
I see errors
decoding response: json: cannot unmarshal string into Go struct field graphResponse.Errors of type graphql.graphErr
So, how I can easily print raw response without changing the library?
It's not trivial... but it's possible. I use this technique for many libraries that don't provide an interface to do it.
- Create a custom
http.Client
, - Specify
http.RoundTripper
that will log HTTP request and responses, - Create
graphql.Client
with a customhttp.Client
.
The most tricky one is (2):
// "github.com/henvic/httpretty"
logger := &httpretty.Logger{}
httpClient = &http.Client{
Transport: logger.RoundTripper(http.DefaultTransport),
}
gqlClient := graphql.NewClient(endpoint, graphql.WithHTTPClient(httpClient))
Oh, I just discovered there is a simpler way: gqlClient.Log = func(s string) { log.Println(s) }