Skip to content

Commit

Permalink
feat: changed map of extensions in ExecRawWithExtensions to raw json …
Browse files Browse the repository at this point in the history
…message
  • Loading branch information
Rafał Kałuski committed Jun 28, 2024
1 parent b1ae09d commit 5a7113d
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 61 deletions.
3 changes: 0 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -822,9 +822,6 @@ Additionally, if you need information about the extensions returned in the respo
```Go
query := `query{something(where: { foo: { _eq: "bar" }}){id}}`
var res struct {
Somethings []Something `json:"something"`
}

data, extensions, err := client.ExecRawWithExtensions(ctx, query, map[string]any{})
if err != nil {
Expand Down
19 changes: 6 additions & 13 deletions graphql.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ func (c *Client) buildAndRequest(ctx context.Context, op operationType, v interf
}

// Request the common method that send graphql request
func (c *Client) request(ctx context.Context, query string, variables map[string]interface{}, options *constructOptionsOutput) ([]byte, map[string][]byte, *http.Response, io.Reader, Errors) {
func (c *Client) request(ctx context.Context, query string, variables map[string]interface{}, options *constructOptionsOutput) ([]byte, []byte, *http.Response, io.Reader, Errors) {
in := GraphQLRequestPayload{
Query: query,
Variables: variables,
Expand Down Expand Up @@ -193,7 +193,7 @@ func (c *Client) request(ctx context.Context, query string, variables map[string

var out struct {
Data *json.RawMessage
Extensions map[string]*json.RawMessage
Extensions *json.RawMessage
Errors Errors
}

Expand Down Expand Up @@ -228,16 +228,9 @@ func (c *Client) request(ctx context.Context, query string, variables map[string
rawData = []byte(*out.Data)
}

var extensions map[string][]byte
if out.Extensions != nil {
extensions = make(map[string][]byte, len(out.Extensions))
for k, v := range out.Extensions {
if v != nil {
extensions[k] = []byte(*v)
} else {
extensions[k] = nil
}
}
var extensions []byte
if out.Extensions != nil && len(*out.Extensions) > 0 {
extensions = []byte(*out.Extensions)
}

if len(out.Errors) > 0 {
Expand Down Expand Up @@ -299,7 +292,7 @@ func (c *Client) ExecRaw(ctx context.Context, query string, variables map[string
// Executes a pre-built query and returns the raw json message and a map with extensions (values also as raw json objects). Unlike the
// Query method you have to specify in the query the fields that you want to receive as they are not inferred from the interface. This method
// is useful if you need to build the query dynamically.
func (c *Client) ExecRawWithExtensions(ctx context.Context, query string, variables map[string]interface{}, options ...Option) ([]byte, map[string][]byte, error) {
func (c *Client) ExecRawWithExtensions(ctx context.Context, query string, variables map[string]interface{}, options ...Option) ([]byte, []byte, error) {
optionsOutput, err := constructOptions(options)
if err != nil {
return nil, nil, err
Expand Down
56 changes: 11 additions & 45 deletions graphql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -480,68 +480,34 @@ func TestClient_Exec_QueryRawWithExtensions(t *testing.T) {
t.Errorf("got body: %v, want %v", got, want)
}
w.Header().Set("Content-Type", "application/json")
mustWrite(w, `{"data": {"user": {"name": "Gopher"}}, "extensions": {"domain": "users", "database": {"id": 1, "name": "users_db"}}}`)
mustWrite(w, `{"data": {"user": {"name": "Gopher"}}, "extensions": {"id": 1, "domain": "users"}}`)
})
client := graphql.NewClient("/graphql", &http.Client{Transport: localRoundTripper{handler: mux}})

var database struct {
ID int `graphql:"id"`
Name string `graphql:"name"`
var ext struct {
ID int `graphql:"id"`
Domain string `graphql:"domain"`
}

_, extensions, err := client.ExecRawWithExtensions(context.Background(), "{user{id,name}}", map[string]interface{}{})
if err != nil {
t.Fatal(err)
}

if got, want := len(extensions), 2; got != want {
t.Errorf("got len(extensions): %q, want: %q", got, want)
if got := extensions; got == nil {
t.Errorf("got nil extensions: %q, want: non-nil", got)
}

var domain string
err = json.Unmarshal(extensions["domain"], &domain)
err = json.Unmarshal(extensions, &ext)
if err != nil {
t.Fatal(err)
}

if got, want := domain, "users"; got != want {
t.Errorf("got domain: %q, want: %q", got, want)
if got, want := ext.ID, 1; got != want {
t.Errorf("got ext.ID: %q, want: %q", got, want)
}

err = json.Unmarshal(extensions["database"], &database)
if err != nil {
t.Fatal(err)
}

if got, want := database.ID, 1; got != want {
t.Errorf("got database.ID: %q, want: %q", got, want)
}
if got, want := database.Name, "users_db"; got != want {
t.Errorf("got database.Name: %q, want: %q", got, want)
}
}

// Test exec pre-built query, return raw json string and map
// with extension set to `null`
func TestClient_Exec_QueryRawWithNullExtension(t *testing.T) {
mux := http.NewServeMux()
mux.HandleFunc("/graphql", func(w http.ResponseWriter, req *http.Request) {
body := mustRead(req.Body)
if got, want := body, `{"query":"{user{id,name}}"}`+"\n"; got != want {
t.Errorf("got body: %v, want %v", got, want)
}
w.Header().Set("Content-Type", "application/json")
mustWrite(w, `{"data": {"user": {"name": "Gopher"}}, "extensions": {"domain": null}}`)
})
client := graphql.NewClient("/graphql", &http.Client{Transport: localRoundTripper{handler: mux}})

_, extensions, err := client.ExecRawWithExtensions(context.Background(), "{user{id,name}}", map[string]interface{}{})
if err != nil {
t.Fatal(err)
}

if got := extensions["domain"]; got != nil {
t.Errorf("got non-nil extensions[\"domain\"]: %q, want: nil", got)
if got, want := ext.Domain, "users"; got != want {
t.Errorf("got ext.Domain: %q, want: %q", got, want)
}
}

Expand Down

0 comments on commit 5a7113d

Please sign in to comment.