Skip to content

Commit

Permalink
feat: Option for including operationName as a query parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
cgetzen committed Dec 20, 2023
1 parent 28aafc7 commit 5ed1e3d
Showing 1 changed file with 39 additions and 8 deletions.
47 changes: 39 additions & 8 deletions graphql/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,15 @@ type Client interface {
}

type client struct {
httpClient Doer
endpoint string
method string
httpClient Doer
endpoint string
method string
withOperationNameParam bool
}

// WithOperationNameParam allows operationName to be included as a query parameter.
func WithOperationNameParam(c *client) {
c.withOperationNameParam = true
}

// NewClient returns a [Client] which makes requests to the given endpoint,
Expand All @@ -58,8 +64,8 @@ type client struct {
// example.
//
// [example/main.go]: https://github.com/Khan/genqlient/blob/main/example/main.go#L12-L20
func NewClient(endpoint string, httpClient Doer) Client {
return newClient(endpoint, httpClient, http.MethodPost)
func NewClient(endpoint string, httpClient Doer, options ...func(*client)) Client {
return newClient(endpoint, httpClient, http.MethodPost, options...)
}

// NewClientUsingGet returns a [Client] which makes GET requests to the given
Expand All @@ -83,11 +89,19 @@ func NewClientUsingGet(endpoint string, httpClient Doer) Client {
return newClient(endpoint, httpClient, http.MethodGet)
}

func newClient(endpoint string, httpClient Doer, method string) Client {
func newClient(endpoint string, httpClient Doer, method string, options ...func(*client)) Client {
if httpClient == nil || httpClient == (*http.Client)(nil) {
httpClient = http.DefaultClient
}
return &client{httpClient, endpoint, method}
c := &client{
httpClient: httpClient,
endpoint: endpoint,
method: method,
}
for _, opt := range options {
opt(c)
}
return c
}

// Doer encapsulates the methods from [*http.Client] needed by [Client].
Expand Down Expand Up @@ -178,14 +192,31 @@ func (c *client) MakeRequest(ctx context.Context, req *Request, resp *Response)
}

func (c *client) createPostRequest(req *Request) (*http.Request, error) {
parsedURL, err := url.Parse(c.endpoint)
if err != nil {
return nil, err
}

queryParams := parsedURL.Query()
queryUpdated := false

if c.withOperationNameParam && req.OpName != "" {
queryParams.Set("operationName", req.OpName)
queryUpdated = true
}

if queryUpdated {
parsedURL.RawQuery = queryParams.Encode()
}

body, err := json.Marshal(req)
if err != nil {
return nil, err
}

httpReq, err := http.NewRequest(
c.method,
c.endpoint,
parsedURL.String(),
bytes.NewReader(body))
if err != nil {
return nil, err
Expand Down

0 comments on commit 5ed1e3d

Please sign in to comment.