2017-08-08 10:40:09 +00:00
|
|
|
package githubql
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"net/http"
|
|
|
|
|
2017-08-08 12:12:45 +00:00
|
|
|
"github.com/shurcooL/graphql"
|
2017-08-08 10:40:09 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Client is a GitHub GraphQL API v4 client.
|
|
|
|
type Client struct {
|
2017-08-08 12:12:45 +00:00
|
|
|
client *graphql.Client
|
2017-08-08 10:40:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewClient creates a new GitHub GraphQL API v4 client with the provided http.Client.
|
|
|
|
// If it's nil, then http.DefaultClient is used.
|
|
|
|
//
|
|
|
|
// Note that GitHub GraphQL API v4 requires authentication, so
|
|
|
|
// the provided http.Client is expected to take care of that.
|
|
|
|
func NewClient(httpClient *http.Client) *Client {
|
|
|
|
return &Client{
|
2017-08-08 12:12:45 +00:00
|
|
|
client: graphql.NewClient("https://api.github.com/graphql", httpClient, scalars),
|
2017-08-08 10:40:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Query executes a single GraphQL query request,
|
|
|
|
// with a query derived from q, populating the response into it.
|
|
|
|
// q should be a pointer to struct that corresponds to the GitHub GraphQL schema.
|
|
|
|
func (c *Client) Query(ctx context.Context, q interface{}, variables map[string]interface{}) error {
|
2017-08-08 12:12:45 +00:00
|
|
|
return c.client.Query(ctx, q, variables)
|
2017-08-08 10:40:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Mutate executes a single GraphQL mutation request,
|
|
|
|
// with a mutation derived from m, populating the response into it.
|
|
|
|
// m 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 *Client) Mutate(ctx context.Context, m interface{}, input Input, variables map[string]interface{}) error {
|
|
|
|
if variables == nil {
|
|
|
|
variables = map[string]interface{}{"input": input}
|
|
|
|
} else {
|
|
|
|
variables["input"] = input
|
|
|
|
}
|
2017-08-08 12:12:45 +00:00
|
|
|
return c.client.Mutate(ctx, m, variables)
|
2017-08-08 10:40:09 +00:00
|
|
|
}
|