Skip to content

Commit

Permalink
Add extensions and export error
Browse files Browse the repository at this point in the history
  • Loading branch information
MDrakos committed Jul 19, 2023
1 parent 05b17f3 commit 6949037
Showing 1 changed file with 30 additions and 27 deletions.
57 changes: 30 additions & 27 deletions graphql.go
Original file line number Diff line number Diff line change
@@ -1,33 +1,34 @@
// Package graphql provides a low level GraphQL client.
//
// // create a client (safe to share across requests)
// client := graphql.NewClient("https://machinebox.io/graphql")
// // create a client (safe to share across requests)
// client := graphql.NewClient("https://machinebox.io/graphql")
//
// // make a request
// req := graphql.NewRequest(`
// query ($key: String!) {
// items (id:$key) {
// field1
// field2
// field3
// }
// }
// `)
// // make a request
// req := graphql.NewRequest(`
// query ($key: String!) {
// items (id:$key) {
// field1
// field2
// field3
// }
// }
// `)
//
// // set any variables
// req.Var("key", "value")
// // set any variables
// req.Var("key", "value")
//
// // run it and capture the response
// var respData ResponseStruct
// if err := client.Run(ctx, req, &respData); err != nil {
// log.Fatal(err)
// }
// // run it and capture the response
// var respData ResponseStruct
// if err := client.Run(ctx, req, &respData); err != nil {
// log.Fatal(err)
// }
//
// Specify client
// # Specify client
//
// To specify your own http.Client, use the WithHTTPClient option:
// httpclient := &http.Client{}
// client := graphql.NewClient("https://machinebox.io/graphql", graphql.WithHTTPClient(httpclient))
//
// httpclient := &http.Client{}
// client := graphql.NewClient("https://machinebox.io/graphql", graphql.WithHTTPClient(httpclient))
package graphql

import (
Expand Down Expand Up @@ -212,7 +213,8 @@ func (c *Client) runWithPostFields(ctx context.Context, req *Request, resp inter

// WithHTTPClient specifies the underlying http.Client to use when
// making requests.
// NewClient(endpoint, WithHTTPClient(specificHTTPClient))
//
// NewClient(endpoint, WithHTTPClient(specificHTTPClient))
func WithHTTPClient(httpclient *http.Client) ClientOption {
return func(client *Client) {
client.httpClient = httpclient
Expand All @@ -231,17 +233,18 @@ func UseMultipartForm() ClientOption {
// modify the behaviour of the Client.
type ClientOption func(*Client)

type graphErr struct {
Message string
type GraphErr struct {
Message string `json:"message"`
Extensions map[string]interface{} `json:"extensions"`
}

func (e graphErr) Error() string {
func (e GraphErr) Error() string {
return "graphql: " + e.Message
}

type graphResponse struct {
Data interface{}
Errors []graphErr
Errors []GraphErr
}

// Request is a GraphQL request.
Expand Down

0 comments on commit 6949037

Please sign in to comment.