File: graphql_client.go

package info (click to toggle)
golang-github-cli-go-gh-v2 2.6.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 496 kB
  • sloc: makefile: 2
file content (182 lines) | stat: -rw-r--r-- 5,139 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package api

import (
	"bytes"
	"context"
	"encoding/json"
	"errors"
	"fmt"
	"io"
	"net/http"
	"strings"

	graphql "github.com/cli/shurcooL-graphql"
)

// GraphQLClient wraps methods for the different types of
// API requests that are supported by the server.
type GraphQLClient struct {
	client     *graphql.Client
	host       string
	httpClient *http.Client
}

func DefaultGraphQLClient() (*GraphQLClient, error) {
	return NewGraphQLClient(ClientOptions{})
}

// GraphQLClient builds a client to send requests to GitHub GraphQL API endpoints.
// As part of the configuration a hostname, auth token, default set of headers,
// and unix domain socket are resolved from the gh environment configuration.
// These behaviors can be overridden using the opts argument.
func NewGraphQLClient(opts ClientOptions) (*GraphQLClient, error) {
	if optionsNeedResolution(opts) {
		var err error
		opts, err = resolveOptions(opts)
		if err != nil {
			return nil, err
		}
	}

	httpClient, err := NewHTTPClient(opts)
	if err != nil {
		return nil, err
	}

	endpoint := graphQLEndpoint(opts.Host)

	return &GraphQLClient{
		client:     graphql.NewClient(endpoint, httpClient),
		host:       endpoint,
		httpClient: httpClient,
	}, nil
}

// DoWithContext executes a GraphQL query request.
// The response is populated into the response argument.
func (c *GraphQLClient) DoWithContext(ctx context.Context, query string, variables map[string]interface{}, response interface{}) error {
	reqBody, err := json.Marshal(map[string]interface{}{"query": query, "variables": variables})
	if err != nil {
		return err
	}

	req, err := http.NewRequestWithContext(ctx, "POST", c.host, bytes.NewBuffer(reqBody))
	if err != nil {
		return err
	}

	resp, err := c.httpClient.Do(req)
	if err != nil {
		return err
	}
	defer resp.Body.Close()

	success := resp.StatusCode >= 200 && resp.StatusCode < 300
	if !success {
		return HandleHTTPError(resp)
	}

	if resp.StatusCode == http.StatusNoContent {
		return nil
	}

	body, err := io.ReadAll(resp.Body)
	if err != nil {
		return err
	}

	gr := graphQLResponse{Data: response}
	err = json.Unmarshal(body, &gr)
	if err != nil {
		return err
	}

	if len(gr.Errors) > 0 {
		return &GraphQLError{Errors: gr.Errors}
	}

	return nil
}

// Do wraps DoWithContext using context.Background.
func (c *GraphQLClient) Do(query string, variables map[string]interface{}, response interface{}) error {
	return c.DoWithContext(context.Background(), query, variables, response)
}

// MutateWithContext executes a GraphQL mutation request.
// The mutation string is derived from the mutation argument, and the
// response is populated into it.
// The mutation argument should be a pointer to struct that corresponds
// to the GitHub GraphQL schema.
// Provided input will be set as a variable named input.
func (c *GraphQLClient) MutateWithContext(ctx context.Context, name string, m interface{}, variables map[string]interface{}) error {
	err := c.client.MutateNamed(ctx, name, m, variables)
	var graphQLErrs graphql.Errors
	if err != nil && errors.As(err, &graphQLErrs) {
		items := make([]GraphQLErrorItem, len(graphQLErrs))
		for i, e := range graphQLErrs {
			items[i] = GraphQLErrorItem{
				Message:    e.Message,
				Locations:  e.Locations,
				Path:       e.Path,
				Extensions: e.Extensions,
				Type:       e.Type,
			}
		}
		err = &GraphQLError{items}
	}
	return err
}

// Mutate wraps MutateWithContext using context.Background.
func (c *GraphQLClient) Mutate(name string, m interface{}, variables map[string]interface{}) error {
	return c.MutateWithContext(context.Background(), name, m, variables)
}

// QueryWithContext executes a GraphQL query request,
// The query string is derived from the query argument, and the
// response is populated into it.
// The query argument should be a pointer to struct that corresponds
// to the GitHub GraphQL schema.
func (c *GraphQLClient) QueryWithContext(ctx context.Context, name string, q interface{}, variables map[string]interface{}) error {
	err := c.client.QueryNamed(ctx, name, q, variables)
	var graphQLErrs graphql.Errors
	if err != nil && errors.As(err, &graphQLErrs) {
		items := make([]GraphQLErrorItem, len(graphQLErrs))
		for i, e := range graphQLErrs {
			items[i] = GraphQLErrorItem{
				Message:    e.Message,
				Locations:  e.Locations,
				Path:       e.Path,
				Extensions: e.Extensions,
				Type:       e.Type,
			}
		}
		err = &GraphQLError{items}
	}
	return err
}

// Query wraps QueryWithContext using context.Background.
func (c *GraphQLClient) Query(name string, q interface{}, variables map[string]interface{}) error {
	return c.QueryWithContext(context.Background(), name, q, variables)
}

type graphQLResponse struct {
	Data   interface{}
	Errors []GraphQLErrorItem
}

func graphQLEndpoint(host string) string {
	if isGarage(host) {
		return fmt.Sprintf("https://%s/api/graphql", host)
	}
	host = normalizeHostname(host)
	if isEnterprise(host) {
		return fmt.Sprintf("https://%s/api/graphql", host)
	}
	if strings.EqualFold(host, localhost) {
		return fmt.Sprintf("http://api.%s/graphql", host)
	}
	return fmt.Sprintf("https://api.%s/graphql", host)
}