Skip to content

Commit

Permalink
adding support for sending extension with gqlgen client (#1633)
Browse files Browse the repository at this point in the history
Co-authored-by: Suraj Chafle <suraj.chafle@crowdstrike.com>
  • Loading branch information
schafle and Suraj Chafle authored Oct 11, 2021
1 parent 589a774 commit fd1bd7c
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 0 deletions.
1 change: 1 addition & 0 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ type (
Query string `json:"query"`
Variables map[string]interface{} `json:"variables,omitempty"`
OperationName string `json:"operationName,omitempty"`
Extensions map[string]interface{} `json:"extensions,omitempty"`
HTTP *http.Request `json:"-"`
}

Expand Down
27 changes: 27 additions & 0 deletions client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,30 @@ func TestAddCookie(t *testing.T) {
client.AddCookie(&http.Cookie{Name: "foo", Value: "value"}),
)
}

func TestAddExtensions(t *testing.T) {
h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
b, err := ioutil.ReadAll(r.Body)
if err != nil {
panic(err)
}
require.Equal(t, `{"query":"user(id:1){name}","extensions":{"persistedQuery":{"sha256Hash":"ceec2897e2da519612279e63f24658c3e91194cbb2974744fa9007a7e1e9f9e7","version":1}}}`, string(b))
err = json.NewEncoder(w).Encode(map[string]interface{}{
"data": map[string]interface{}{
"Name": "Bob",
},
})
if err != nil {
panic(err)
}
})

c := client.New(h)

var resp struct {
Name string
}
c.MustPost("user(id:1){name}", &resp,
client.Extensions(map[string]interface{}{"persistedQuery": map[string]interface{}{"version": 1, "sha256Hash": "ceec2897e2da519612279e63f24658c3e91194cbb2974744fa9007a7e1e9f9e7"}}),
)
}
7 changes: 7 additions & 0 deletions client/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ func Operation(name string) Option {
}
}

// Extensions sets the extensions to be sent with the outgoing request
func Extensions(extensions map[string]interface{}) Option {
return func(bd *Request) {
bd.Extensions = extensions
}
}

// Path sets the url that this request will be made against, useful if you are mounting your entire router
// and need to specify the url to the graphql endpoint.
func Path(url string) Option {
Expand Down

0 comments on commit fd1bd7c

Please sign in to comment.