graphql icon indicating copy to clipboard operation
graphql copied to clipboard

[QUESTION] How to print/return raw response from graphql server?

Open bynov opened this issue 3 years ago • 2 comments

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?

bynov avatar Oct 05 '21 10:10 bynov

It's not trivial... but it's possible. I use this technique for many libraries that don't provide an interface to do it.

  1. Create a custom http.Client,
  2. Specify http.RoundTripper that will log HTTP request and responses,
  3. Create graphql.Client with a custom http.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))

adambabik avatar Oct 27 '21 10:10 adambabik

Oh, I just discovered there is a simpler way: gqlClient.Log = func(s string) { log.Println(s) }

adambabik avatar Oct 27 '21 10:10 adambabik