Skip to content

Commit

Permalink
docs, tests
Browse files Browse the repository at this point in the history
  • Loading branch information
cgetzen committed Dec 20, 2023
1 parent 5ed1e3d commit 2bc14db
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 1 deletion.
1 change: 1 addition & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
13 changes: 13 additions & 0 deletions docs/FAQ.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
1 change: 1 addition & 0 deletions internal/integration/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
}

Expand Down
16 changes: 15 additions & 1 deletion internal/integration/roundtrip.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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}
Expand Down

0 comments on commit 2bc14db

Please sign in to comment.