diff --git a/client/client.go b/client/client.go index 9c5f56c8fc3..7a2825c952b 100644 --- a/client/client.go +++ b/client/client.go @@ -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:"-"` } diff --git a/client/client_test.go b/client/client_test.go index 64a75a30c6a..569151cd8a2 100644 --- a/client/client_test.go +++ b/client/client_test.go @@ -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"}}), + ) +} diff --git a/client/options.go b/client/options.go index e600f382852..bf4280ac711 100644 --- a/client/options.go +++ b/client/options.go @@ -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 {