go-graphql-client icon indicating copy to clipboard operation
go-graphql-client copied to clipboard

Retrieve response body and status code

Open nicumicle opened this issue 1 year ago • 1 comments

Hi,

I'm trying to fetch the HTTP request-response in order to display custom error messages when an error occurred.

I checked the code, and I found this:

https://github.com/hasura/go-graphql-client/blob/master/graphql.go#L175

if resp.StatusCode != http.StatusOK {
		body, _ := ioutil.ReadAll(resp.Body)
		err := newError(ErrRequestError, fmt.Errorf("%v; body: %q", resp.Status, body))

		if c.debug {
			err = err.withRequest(request, reqReader)
		}
		return nil, nil, nil, Errors{err}
	}

It would be possible to have a way to retrieve the response resp so that I can get the response body and the status code?

        err := client.Mutate(ctx,  m, variables)
	if err != nil {
		 // TODO: extract the status code and response body
                // resp := client.GetResponse()) ?
	}

Thank you.

nicumicle avatar May 12 '23 09:05 nicumicle

Hi,

It's impossible for now, but I'll add the solution soon.

hgiasac avatar May 13 '23 04:05 hgiasac