shurcooL/graphql

Please make "constructQuery" exported

aleksei-krotov opened this issue · 3 comments

Hi,
All I need is to use your "constructQuery" for tests. Please make it public.

I got custom types at queries and found that I want to write small assert test to avoid bad constructions.

For example, I converted an input map to custom type:

type codes map[string]string

codesInput := make([]codes, 0, len(filterList))
for _, v := range filterList {
    codesInput = append(codesInput, codes(v))
}
variables := map[string]interface{}{
"filter":    codesInput,
}

It gives me the correct query after construction:
query($filter:[codes!]!){classification(filters: $filter)}

But when I use map[string]string instead of codes, it transforms to:
query($filter:[!]!){classification(filters: $filter)}}
So I need to protect "codes" type name by tests.

I came here to ask for the same thing. I personally just want to get the generated query as a string

For what it's worth, I was able to use a RoundTripper to read the body of the request to get the query.

Not ideal, but it works.

type roundTripFunc func(r *http.Request) (*http.Response, error)

func (s roundTripFunc) RoundTrip(r *http.Request) (*http.Response, error) {
	return s(r)
}
httpClient.Transport = roundTripFunc(func(r *http.Request) (x *http.Response, y error) {
	b, _ := io.ReadAll(r.Body)
	fmt.Println(string(b))

	return
})

For what it's worth, I was able to use a RoundTripper to read the body of the request to get the query.

Not ideal, but it works.

type roundTripFunc func(r *http.Request) (*http.Response, error)

func (s roundTripFunc) RoundTrip(r *http.Request) (*http.Response, error) {
	return s(r)
}
httpClient.Transport = roundTripFunc(func(r *http.Request) (x *http.Response, y error) {
	b, _ := io.ReadAll(r.Body)
	fmt.Println(string(b))

	return
})

We use proprietary header for Authorization, so I used RoundTrip as well.
But please be careful and do not modify request body. https://go.dev/src/net/http/client.go#129

Like this: https://github.com/google/go-github/blob/d23570d44313ca73dbcaadec71fc43eca4d29f8b/github/github.go#L841-L875