Skip to content

Commit

Permalink
Merge branch 'trunk'
Browse files Browse the repository at this point in the history
  • Loading branch information
samcoe committed Oct 5, 2023
2 parents ed46e5a + af8dbfa commit ad010c5
Show file tree
Hide file tree
Showing 17 changed files with 172 additions and 177 deletions.
19 changes: 19 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
name: Lint
on: [push, pull_request]
jobs:
lint:
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: '1.16'

- name: Lint
uses: golangci/golangci-lint-action@v3
with:
version: latest
22 changes: 22 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: Test
on: [push, pull_request]
jobs:
test:
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest, windows-latest, macos-latest]
go: [1.16]
runs-on: ${{ matrix.os }}

steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: ${{ matrix.go }}

- name: Run tests
run: go test -v ./...
11 changes: 11 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
linters:
enable:
- gofmt
- godot

linters-settings:
godot:
# comments to be checked: `declarations`, `toplevel`, or `all`
scope: declarations
# check that each sentence starts with a capital letter
capital: true
18 changes: 3 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
graphql
=======

[![Go Reference](https://pkg.go.dev/badge/github.com/shurcooL/graphql.svg)](https://pkg.go.dev/github.com/shurcooL/graphql)

Package `graphql` provides a GraphQL client implementation.

For more information, see package [`github.com/shurcooL/githubv4`](https://github.com/shurcooL/githubv4), which is a specialized version targeting GitHub GraphQL API v4. That package is driving the feature development.
Package `graphql` provides a GraphQL client implementation, and is forked from `https://github.com/shurcooL/graphql`.

Installation
------------

```sh
go get github.com/shurcooL/graphql
```bash
go get -u github.com/cli/shurcooL-graphql
```

Usage
Expand Down Expand Up @@ -274,14 +270,6 @@ fmt.Printf("Created a %v star review: %v\n", m.CreateReview.Stars, m.CreateRevie
// Created a 5 star review: This is a great movie!
```
Directories
-----------
| Path | Synopsis |
|---------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------|
| [ident](https://pkg.go.dev/github.com/shurcooL/graphql/ident) | Package ident provides functions for parsing and converting identifier names between various naming convention. |
| [internal/jsonutil](https://pkg.go.dev/github.com/shurcooL/graphql/internal/jsonutil) | Package jsonutil provides a function for decoding JSON into a GraphQL query data structure. |
License
-------
Expand Down
8 changes: 0 additions & 8 deletions doc.go

This file was deleted.

107 changes: 0 additions & 107 deletions example/graphqldev/main.go

This file was deleted.

2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module github.com/shurcooL/graphql
module github.com/cli/shurcooL-graphql

go 1.19
40 changes: 40 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.13.0/go.mod h1:y6Z2r+Rw4iayiXXAIxJIDAJ1zMW4yaTpebo8fPOliYc=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg=
golang.org/x/net v0.15.0 h1:ugBLEUaxABaB5AJqW9enI0ACdci2RUd4eP51NTBvuJ8=
golang.org/x/net v0.15.0/go.mod h1:idbUs1IY1+zTqbi8yxTbhexhEEk5ur9LInksu6HrEpk=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
golang.org/x/term v0.8.0/go.mod h1:xPskH00ivmX89bAKVGSKKtLOWNx2+17Eiy94tnKShWo=
golang.org/x/term v0.12.0/go.mod h1:owVbMEjm3cBLCHdkQu9b1opXd4ETQWc3BhuQGKgXgvU=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
49 changes: 35 additions & 14 deletions graphql.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ import (
"fmt"
"io"
"net/http"
"strings"

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

// Client is a GraphQL client.
Expand All @@ -31,26 +32,36 @@ func NewClient(url string, httpClient *http.Client) *Client {

// 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 GraphQL schema.
// Argument q should be a pointer to struct that corresponds to the GraphQL schema.
func (c *Client) Query(ctx context.Context, q any, variables map[string]any) error {
return c.do(ctx, queryOperation, q, variables)
return c.do(ctx, queryOperation, q, variables, "")
}

// QueryNamed is the same as Query but allows a name to be specified for the query.
func (c *Client) QueryNamed(ctx context.Context, queryName string, q any, variables map[string]any) error {
return c.do(ctx, queryOperation, q, variables, queryName)
}

// 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 GraphQL schema.
// Argument m should be a pointer to struct that corresponds to the GraphQL schema.
func (c *Client) Mutate(ctx context.Context, m any, variables map[string]any) error {
return c.do(ctx, mutationOperation, m, variables)
return c.do(ctx, mutationOperation, m, variables, "")
}

// MutateNamed is the same as Mutate but allows a name to be specified for the mutation.
func (c *Client) MutateNamed(ctx context.Context, queryName string, m any, variables map[string]any) error {
return c.do(ctx, mutationOperation, m, variables, queryName)
}

// do executes a single GraphQL operation.
func (c *Client) do(ctx context.Context, op operationType, v any, variables map[string]any) error {
func (c *Client) do(ctx context.Context, op operationType, v any, variables map[string]any, queryName string) error {
var query string
switch op {
case queryOperation:
query = constructQuery(v, variables)
query = constructQuery(v, variables, queryName)
case mutationOperation:
query = constructMutation(v, variables)
query = constructMutation(v, variables, queryName)
}
in := struct {
Query string `json:"query"`
Expand Down Expand Up @@ -80,7 +91,7 @@ func (c *Client) do(ctx context.Context, op operationType, v any, variables map[
}
var out struct {
Data *json.RawMessage
Errors errors
Errors Errors
//Extensions any // Unused.
}
err = json.NewDecoder(resp.Body).Decode(&out)
Expand All @@ -101,27 +112,37 @@ func (c *Client) do(ctx context.Context, op operationType, v any, variables map[
return nil
}

// errors represents the "errors" array in a response from a GraphQL server.
// Errors represents the "errors" array in a response from a GraphQL server.
// If returned via error interface, the slice is expected to contain at least 1 element.
//
// Specification: https://spec.graphql.org/October2021/#sec-Errors.
type errors []struct {
type Errors []struct {
Message string
Locations []struct {
Line int
Column int
}
Path []any
Extensions map[string]any
Type string
}

// Error implements error interface.
func (e errors) Error() string {
return e[0].Message
func (e Errors) Error() string {
b := strings.Builder{}
l := len(e)
for i, err := range e {
b.WriteString(fmt.Sprintf("Message: %s, Locations: %+v", err.Message, err.Locations))
if i != l-1 {
b.WriteString("\n")
}
}
return b.String()
}

type operationType uint8

const (
queryOperation operationType = iota
mutationOperation
//subscriptionOperation // Unused.
)
6 changes: 3 additions & 3 deletions graphql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"net/http/httptest"
"testing"

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

func TestClient_Query_partialDataWithErrorResponse(t *testing.T) {
Expand Down Expand Up @@ -52,7 +52,7 @@ func TestClient_Query_partialDataWithErrorResponse(t *testing.T) {
if err == nil {
t.Fatal("got error: nil, want: non-nil")
}
if got, want := err.Error(), "Could not resolve to a node with the global id of 'NotExist'"; got != want {
if got, want := err.Error(), "Message: Could not resolve to a node with the global id of 'NotExist', Locations: [{Line:10 Column:4}]"; got != want {
t.Errorf("got error: %v, want: %v", got, want)
}
if q.Node1 == nil || q.Node1.ID != "MDEyOklzc3VlQ29tbWVudDE2OTQwNzk0Ng==" {
Expand Down Expand Up @@ -92,7 +92,7 @@ func TestClient_Query_noDataWithErrorResponse(t *testing.T) {
if err == nil {
t.Fatal("got error: nil, want: non-nil")
}
if got, want := err.Error(), "Field 'user' is missing required arguments: login"; got != want {
if got, want := err.Error(), "Message: Field 'user' is missing required arguments: login, Locations: [{Line:7 Column:3}]"; got != want {
t.Errorf("got error: %v, want: %v", got, want)
}
if q.User.Name != "" {
Expand Down
2 changes: 1 addition & 1 deletion ident/ident_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"reflect"
"testing"

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

func Example_lowerCamelCaseToMixedCaps() {
Expand Down
Loading

0 comments on commit ad010c5

Please sign in to comment.