Skip to content

Commit

Permalink
Change all references of GQL to GraphQL and references of gql to graphQL
Browse files Browse the repository at this point in the history
  • Loading branch information
samcoe committed Apr 15, 2023
1 parent f35ae2c commit c3775e4
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 82 deletions.
24 changes: 12 additions & 12 deletions example_gh_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,9 @@ func ExampleRESTClient_pagination() {
}
}

// Query tags from cli/cli repository using GQL API.
func ExampleDefaultGQLClient() {
client, err := api.DefaultGQLClient()
// Query tags from cli/cli repository using GraphQL API.
func ExampleDefaultGraphQLClient() {
client, err := api.DefaultGraphQLClient()
if err != nil {
log.Fatal(err)
}
Expand All @@ -161,14 +161,14 @@ func ExampleDefaultGQLClient() {
fmt.Println(query)
}

// Query tags from cli/cli repository using GQL API.
// Query tags from cli/cli repository using GraphQL API.
// Enable caching and request timeout.
func ExampleGQLClient() {
func ExampleGraphQLClient() {
opts := api.ClientOptions{
EnableCache: true,
Timeout: 5 * time.Second,
}
client, err := api.NewGQLClient(opts)
client, err := api.NewGraphQLClient(opts)
if err != nil {
log.Fatal(err)
}
Expand All @@ -194,9 +194,9 @@ func ExampleGQLClient() {
fmt.Println(query)
}

// Add a star to the cli/go-gh repository using the GQL API.
func ExampleGQLClient_mutate() {
client, err := api.DefaultGQLClient()
// Add a star to the cli/go-gh repository using the GraphQL API.
func ExampleGraphQLClient_mutate() {
client, err := api.DefaultGraphQLClient()
if err != nil {
log.Fatal(err)
}
Expand Down Expand Up @@ -229,9 +229,9 @@ func ExampleGQLClient_mutate() {
fmt.Println(mutation.AddStar.Starrable.Repository.StargazerCount)
}

// Query releases from cli/cli repository using GQL API with paginated results.
func ExampleGQLClient_pagination() {
client, err := api.DefaultGQLClient()
// Query releases from cli/cli repository using GraphQL API with paginated results.
func ExampleGraphQLClient_pagination() {
client, err := api.DefaultGraphQLClient()
if err != nil {
log.Fatal(err)
}
Expand Down
20 changes: 10 additions & 10 deletions pkg/api/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,14 @@ func (err *HTTPError) Error() string {
return fmt.Sprintf("HTTP %d (%s)", err.StatusCode, err.RequestURL)
}

// GQLError represents an error response from GitHub GraphQL API.
type GQLError struct {
Errors []GQLErrorItem
// GraphQLError represents an error response from GitHub GraphQL API.
type GraphQLError struct {
Errors []GraphQLErrorItem
}

// GQLErrorItem stores additional information about an error response
// GraphQLErrorItem stores additional information about an error response
// returned from the GitHub GraphQL API.
type GQLErrorItem struct {
type GraphQLErrorItem struct {
Message string
Locations []struct {
Line int
Expand All @@ -55,8 +55,8 @@ type GQLErrorItem struct {
Type string
}

// Allow GQLError to satisfy error interface.
func (gr *GQLError) Error() string {
// Allow GraphQLError to satisfy error interface.
func (gr *GraphQLError) Error() string {
errorMessages := make([]string, 0, len(gr.Errors))
for _, e := range gr.Errors {
msg := e.Message
Expand All @@ -68,9 +68,9 @@ func (gr *GQLError) Error() string {
return fmt.Sprintf("GraphQL: %s", strings.Join(errorMessages, ", "))
}

// Match determines if the GQLError is about a specific type on a specific path.
// Match determines if the GraphQLError is about a specific type on a specific path.
// If the path argument ends with a ".", it will match all its subpaths.
func (gr *GQLError) Match(expectType, expectPath string) bool {
func (gr *GraphQLError) Match(expectType, expectPath string) bool {
for _, e := range gr.Errors {
if e.Type != expectType || !matchPath(e.pathString(), expectPath) {
return false
Expand All @@ -79,7 +79,7 @@ func (gr *GQLError) Match(expectType, expectPath string) bool {
return true
}

func (ge GQLErrorItem) pathString() string {
func (ge GraphQLErrorItem) pathString() string {
var res strings.Builder
for i, v := range ge.Path {
if i > 0 {
Expand Down
12 changes: 6 additions & 6 deletions pkg/api/errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@ import (
"github.com/stretchr/testify/assert"
)

func TestGQLErrorMatch(t *testing.T) {
func TestGraphQLErrorMatch(t *testing.T) {
tests := []struct {
name string
error GQLError
error GraphQLError
kind string
path string
wantMatch bool
}{
{
name: "matches path and type",
error: GQLError{Errors: []GQLErrorItem{
error: GraphQLError{Errors: []GraphQLErrorItem{
{Path: []interface{}{"repository", "issue"}, Type: "NOT_FOUND"},
}},
kind: "NOT_FOUND",
Expand All @@ -25,7 +25,7 @@ func TestGQLErrorMatch(t *testing.T) {
},
{
name: "matches base path and type",
error: GQLError{Errors: []GQLErrorItem{
error: GraphQLError{Errors: []GraphQLErrorItem{
{Path: []interface{}{"repository", "issue"}, Type: "NOT_FOUND"},
}},
kind: "NOT_FOUND",
Expand All @@ -34,7 +34,7 @@ func TestGQLErrorMatch(t *testing.T) {
},
{
name: "does not match path but matches type",
error: GQLError{Errors: []GQLErrorItem{
error: GraphQLError{Errors: []GraphQLErrorItem{
{Path: []interface{}{"repository", "issue"}, Type: "NOT_FOUND"},
}},
kind: "NOT_FOUND",
Expand All @@ -43,7 +43,7 @@ func TestGQLErrorMatch(t *testing.T) {
},
{
name: "matches path but not type",
error: GQLError{Errors: []GQLErrorItem{
error: GraphQLError{Errors: []GraphQLErrorItem{
{Path: []interface{}{"repository", "issue"}, Type: "NOT_FOUND"},
}},
kind: "UNKNOWN",
Expand Down
62 changes: 31 additions & 31 deletions pkg/api/gql_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,23 @@ import (
graphql "github.com/cli/shurcooL-graphql"
)

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

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

// GQLClient builds a client to send requests to GitHub GraphQL API endpoints.
// 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 NewGQLClient(opts ClientOptions) (*GQLClient, error) {
func NewGraphQLClient(opts ClientOptions) (*GraphQLClient, error) {
if optionsNeedResolution(opts) {
var err error
opts, err = resolveOptions(opts)
Expand All @@ -43,9 +43,9 @@ func NewGQLClient(opts ClientOptions) (*GQLClient, error) {
return nil, err
}

endpoint := gqlEndpoint(opts.Host)
endpoint := graphQLEndpoint(opts.Host)

return &GQLClient{
return &GraphQLClient{
client: graphql.NewClient(endpoint, httpClient),
host: endpoint,
httpClient: httpClient,
Expand All @@ -54,7 +54,7 @@ func NewGQLClient(opts ClientOptions) (*GQLClient, error) {

// DoWithContext executes a GraphQL query request.
// The response is populated into the response argument.
func (c *GQLClient) DoWithContext(ctx context.Context, query string, variables map[string]interface{}, response interface{}) error {
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
Expand Down Expand Up @@ -85,21 +85,21 @@ func (c *GQLClient) DoWithContext(ctx context.Context, query string, variables m
return err
}

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

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

return nil
}

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

Expand All @@ -109,27 +109,27 @@ func (c *GQLClient) Do(query string, variables map[string]interface{}, response
// 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 *GQLClient) MutateWithContext(ctx context.Context, name string, m interface{}, variables map[string]interface{}) error {
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 gqlErrs graphql.Errors
if err != nil && errors.As(err, &gqlErrs) {
items := make([]GQLErrorItem, len(gqlErrs))
for i, e := range gqlErrs {
items[i] = GQLErrorItem{
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 = &GQLError{items}
err = &GraphQLError{items}
}
return err
}

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

Expand All @@ -138,36 +138,36 @@ func (c *GQLClient) Mutate(name string, m interface{}, variables map[string]inte
// response is populated into it.
// The query argument should be a pointer to struct that corresponds
// to the GitHub GraphQL schema.
func (c *GQLClient) QueryWithContext(ctx context.Context, name string, q interface{}, variables map[string]interface{}) error {
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 gqlErrs graphql.Errors
if err != nil && errors.As(err, &gqlErrs) {
items := make([]GQLErrorItem, len(gqlErrs))
for i, e := range gqlErrs {
items[i] = GQLErrorItem{
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 = &GQLError{items}
err = &GraphQLError{items}
}
return err
}

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

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

func gqlEndpoint(host string) string {
func graphQLEndpoint(host string) string {
if isGarage(host) {
return fmt.Sprintf("https://%s/api/graphql", host)
}
Expand Down
Loading

0 comments on commit c3775e4

Please sign in to comment.