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

Please make "constructQuery" exported #78

Open
aleksei-krotov opened this issue Oct 28, 2021 · 3 comments
Open

Please make "constructQuery" exported #78

aleksei-krotov opened this issue Oct 28, 2021 · 3 comments

Comments

@aleksei-krotov
Copy link

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.

@donatj
Copy link

donatj commented Apr 7, 2022

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

@donatj
Copy link

donatj commented Apr 7, 2022

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
})

@aleksei-krotov
Copy link
Author

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

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

No branches or pull requests

2 participants