Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
bynov opened this issue Oct 5, 2021 · 2 comments
Open

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

bynov opened this issue Oct 5, 2021 · 2 comments

Comments

@bynov
Copy link

bynov commented Oct 5, 2021

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?

@adambabik
Copy link

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
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants