1package githubv4
2
3import (
4 "context"
5 "net/http"
6
7 "github.com/shurcooL/graphql"
8)
9
10// Client is a GitHub GraphQL API v4 client.
11type Client struct {
12 client *graphql.Client
13}
14
15// NewClient creates a new GitHub GraphQL API v4 client with the provided http.Client.
16// If httpClient is nil, then http.DefaultClient is used.
17//
18// Note that GitHub GraphQL API v4 requires authentication, so
19// the provided http.Client is expected to take care of that.
20func NewClient(httpClient *http.Client) *Client {
21 return &Client{
22 client: graphql.NewClient("https://api.github.com/graphql", httpClient),
23 }
24}
25
26// NewEnterpriseClient creates a new GitHub GraphQL API v4 client for the GitHub Enterprise
27// instance with the specified GraphQL endpoint URL, using the provided http.Client.
28// If httpClient is nil, then http.DefaultClient is used.
29//
30// Note that GitHub GraphQL API v4 requires authentication, so
31// the provided http.Client is expected to take care of that.
32func NewEnterpriseClient(url string, httpClient *http.Client) *Client {
33 return &Client{
34 client: graphql.NewClient(url, httpClient),
35 }
36}
37
38// Query executes a single GraphQL query request,
39// with a query derived from q, populating the response into it.
40// q should be a pointer to struct that corresponds to the GitHub GraphQL schema.
41func (c *Client) Query(ctx context.Context, q interface{}, variables map[string]interface{}) error {
42 return c.client.Query(ctx, q, variables)
43}
44
45// Mutate executes a single GraphQL mutation request,
46// with a mutation derived from m, populating the response into it.
47// m should be a pointer to struct that corresponds to the GitHub GraphQL schema.
48// Provided input will be set as a variable named "input".
49func (c *Client) Mutate(ctx context.Context, m interface{}, input Input, variables map[string]interface{}) error {
50 if variables == nil {
51 variables = map[string]interface{}{"input": input}
52 } else {
53 variables["input"] = input
54 }
55 return c.client.Mutate(ctx, m, variables)
56}