From 2bc14db5c0b0312d1cd51dc810284618c14aecab Mon Sep 17 00:00:00 2001 From: Charlie Getzen Date: Wed, 20 Dec 2023 01:19:12 -0600 Subject: [PATCH] docs, tests --- docs/CHANGELOG.md | 1 + docs/FAQ.md | 13 +++++++++++++ internal/integration/integration_test.go | 1 + internal/integration/roundtrip.go | 16 +++++++++++++++- 4 files changed, 30 insertions(+), 1 deletion(-) diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index bec20ef4..33505a3c 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -24,6 +24,7 @@ When releasing a new version: ### New features: +- The new `graphql.WithOperationNameParam` allows clients to be created that include `operationName` as a query parameter. - The new `optional: generic` allows using a generic type to represent optionality. See the [documentation](genqlient.yaml) for details. - For schemas with enum values that differ only in casing, it's now possible to disable smart-casing in genqlient.yaml; see the [documentation](genqlient.yaml) for `casing` for details. diff --git a/docs/FAQ.md b/docs/FAQ.md index 35b084c1..0f267fb7 100644 --- a/docs/FAQ.md +++ b/docs/FAQ.md @@ -36,6 +36,19 @@ The URL requested will be: The client does not support mutations, and will return an error if passed a request that attempts one. +### … include operationName as a query parameter? + +`graphql.NewClient` support the option `graphql.WithOperationNameParam`, which will add `?operationName=...` to the request's query parameters. + +```go +ctx := context.Background() +client := graphql.NewClient("https://api.github.com/graphql", http.DefaultClient, graphql.WithOperationNameParam) +resp, err := getUser(ctx, client, "benjaminjkraft") +fmt.Println(resp.User.Name, err) +``` + + + ### … use an API that requires authentication? When you call `graphql.NewClient`, pass in an HTTP client that adds whatever authentication headers you need (typically by wrapping the client's `Transport`). For example: diff --git a/internal/integration/integration_test.go b/internal/integration/integration_test.go index 0e056fab..a99ec371 100644 --- a/internal/integration/integration_test.go +++ b/internal/integration/integration_test.go @@ -107,6 +107,7 @@ func TestVariables(t *testing.T) { // worry about it. clients := []graphql.Client{ graphql.NewClient(server.URL, http.DefaultClient), + graphql.NewClient(server.URL, http.DefaultClient, graphql.WithOperationNameParam), graphql.NewClientUsingGet(server.URL, http.DefaultClient), } diff --git a/internal/integration/roundtrip.go b/internal/integration/roundtrip.go index 1be9996a..831978ef 100644 --- a/internal/integration/roundtrip.go +++ b/internal/integration/roundtrip.go @@ -104,7 +104,11 @@ func (c *roundtripClient) MakeRequest(ctx context.Context, req *graphql.Request, } func newRoundtripClients(t *testing.T, endpoint string) []graphql.Client { - return []graphql.Client{newRoundtripClient(t, endpoint), newRoundtripGetClient(t, endpoint)} + return []graphql.Client{ + newRoundtripClient(t, endpoint), + newRoundtripClientWithOptions(t, endpoint), + newRoundtripGetClient(t, endpoint), + } } func newRoundtripClient(t *testing.T, endpoint string) graphql.Client { @@ -117,6 +121,16 @@ func newRoundtripClient(t *testing.T, endpoint string) graphql.Client { } } +func newRoundtripClientWithOptions(t *testing.T, endpoint string) graphql.Client { + transport := &lastResponseTransport{wrapped: http.DefaultTransport} + httpClient := &http.Client{Transport: transport} + return &roundtripClient{ + wrapped: graphql.NewClient(endpoint, httpClient, graphql.WithOperationNameParam), + transport: transport, + t: t, + } +} + func newRoundtripGetClient(t *testing.T, endpoint string) graphql.Client { transport := &lastResponseTransport{wrapped: http.DefaultTransport} httpClient := &http.Client{Transport: transport}