Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Downgrade go #80

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
71 changes: 71 additions & 0 deletions .github/workflows/gobuild.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
name: build

on:
pull_request:
branch:
- master

jobs:
review:
name: Review code
runs-on: ubuntu-latest
steps:
- name: Check out code
uses: actions/checkout@v3

- name: Set up Go base on go.mod
uses: actions/setup-go@v4
with:
go-version-file: ${{ github.workspace }}/go.mod
id: go

- name: Check suspicious constructs (vet)
run: |
go vet ./...

- name: Staticcheck
uses: dominikh/[email protected]
with:
install-go: false
checks: all, -ST1000, -ST1003, -ST1005

security:
name: Review security
runs-on: ubuntu-latest
steps:
- name: Check out code
uses: actions/checkout@v3

- name: Set up Go base on go.mod
uses: actions/setup-go@v4
with:
go-version-file: ${{ github.workspace }}/go.mod
id: go

- name: Security Scanner
uses: securego/gosec@master
with:
args: -exclude=G505 ./...

- name: WriteGoList for dependency check tool
run: go list -json -test ./... > go.list

- name: Dependency vulnerability check
uses: sonatype-nexus-community/nancy-github-action@main

test:
name: Test
runs-on: ubuntu-latest
steps:
- name: Check out code
uses: actions/checkout@v3

- name: Set up Go base on go.mod
uses: actions/setup-go@v4
with:
go-version-file: ${{ github.workspace }}/go.mod
id: go

- name: test
run: |
go test -v ./... -cover -coverprofile=cover.out
16 changes: 0 additions & 16 deletions .travis.yml

This file was deleted.

8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
# graphql [![GoDoc](https://godoc.org/github.com/machinebox/graphql?status.png)](http://godoc.org/github.com/machinebox/graphql) [![Build Status](https://travis-ci.org/machinebox/graphql.svg?branch=master)](https://travis-ci.org/machinebox/graphql) [![Go Report Card](https://goreportcard.com/badge/github.com/machinebox/graphql)](https://goreportcard.com/report/github.com/machinebox/graphql)
# graphql

![Actions Status](https://github.com/duncancrawford/graphql/workflows/build/badge.svg)

Forked from https://github.com/machinebox/graphql with a little refresh of libs

Low-level GraphQL client for Go.

Expand All @@ -13,7 +17,7 @@ Low-level GraphQL client for Go.
Make sure you have a working Go environment. To install graphql, simply run:

```
$ go get github.com/machinebox/graphql
$ go get github.com/duncancrawford/graphql
```

## Usage
Expand Down
14 changes: 14 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module github.com/duncancrawford/graphql

go 1.20

require (
github.com/pkg/errors v0.9.1
github.com/stretchr/testify v1.8.4
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
12 changes: 12 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
83 changes: 37 additions & 46 deletions graphql_json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,23 @@ package graphql

import (
"context"
"github.com/stretchr/testify/assert"
"io"
"io/ioutil"
"net/http"
"net/http/httptest"
"testing"
"time"

"github.com/matryer/is"
)

func TestDoJSON(t *testing.T) {
is := is.New(t)
var calls int
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
calls++
is.Equal(r.Method, http.MethodPost)
b, err := ioutil.ReadAll(r.Body)
is.NoErr(err)
is.Equal(string(b), `{"query":"query {}","variables":null}`+"\n")
io.WriteString(w, `{
assert.Equal(t, r.Method, http.MethodPost)
b, err := io.ReadAll(r.Body)
assert.NoError(t, err)
assert.Equal(t, string(b), `{"query":"query {}","variables":null}`+"\n")
_, _ = io.WriteString(w, `{
"data": {
"something": "yes"
}
Expand All @@ -36,22 +33,21 @@ func TestDoJSON(t *testing.T) {
defer cancel()
var responseData map[string]interface{}
err := client.Run(ctx, &Request{q: "query {}"}, &responseData)
is.NoErr(err)
is.Equal(calls, 1) // calls
is.Equal(responseData["something"], "yes")
assert.NoError(t, err)
assert.Equal(t, calls, 1) // calls
assert.Equal(t, responseData["something"], "yes")
}

func TestDoJSONServerError(t *testing.T) {
is := is.New(t)
var calls int
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
calls++
is.Equal(r.Method, http.MethodPost)
b, err := ioutil.ReadAll(r.Body)
is.NoErr(err)
is.Equal(string(b), `{"query":"query {}","variables":null}`+"\n")
assert.Equal(t, r.Method, http.MethodPost)
b, err := io.ReadAll(r.Body)
assert.NoError(t, err)
assert.Equal(t, string(b), `{"query":"query {}","variables":null}`+"\n")
w.WriteHeader(http.StatusInternalServerError)
io.WriteString(w, `Internal Server Error`)
_, _ = io.WriteString(w, `Internal Server Error`)
}))
defer srv.Close()

Expand All @@ -62,21 +58,20 @@ func TestDoJSONServerError(t *testing.T) {
defer cancel()
var responseData map[string]interface{}
err := client.Run(ctx, &Request{q: "query {}"}, &responseData)
is.Equal(calls, 1) // calls
is.Equal(err.Error(), "graphql: server returned a non-200 status code: 500")
assert.Equal(t, calls, 1) // calls
assert.Equal(t, err.Error(), "graphql: server returned a non-200 status code: 500")
}

func TestDoJSONBadRequestErr(t *testing.T) {
is := is.New(t)
var calls int
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
calls++
is.Equal(r.Method, http.MethodPost)
b, err := ioutil.ReadAll(r.Body)
is.NoErr(err)
is.Equal(string(b), `{"query":"query {}","variables":null}`+"\n")
assert.Equal(t, http.MethodPost, r.Method)
b, err := io.ReadAll(r.Body)
assert.NoError(t, err)
assert.Equal(t, `{"query":"query {}","variables":null}`+"\n", string(b))
w.WriteHeader(http.StatusBadRequest)
io.WriteString(w, `{
_, _ = io.WriteString(w, `{
"errors": [{
"message": "miscellaneous message as to why the the request was bad"
}]
Expand All @@ -91,21 +86,19 @@ func TestDoJSONBadRequestErr(t *testing.T) {
defer cancel()
var responseData map[string]interface{}
err := client.Run(ctx, &Request{q: "query {}"}, &responseData)
is.Equal(calls, 1) // calls
is.Equal(err.Error(), "graphql: miscellaneous message as to why the the request was bad")
assert.Equal(t, calls, 1) // calls
assert.Equal(t, "graphql: miscellaneous message as to why the the request was bad", err.Error())
}

func TestQueryJSON(t *testing.T) {
is := is.New(t)

var calls int
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
calls++
b, err := ioutil.ReadAll(r.Body)
is.NoErr(err)
is.Equal(string(b), `{"query":"query {}","variables":{"username":"matryer"}}`+"\n")
b, err := io.ReadAll(r.Body)
assert.NoError(t, err)
assert.Equal(t, `{"query":"query {}","variables":{"username":"matryer"}}`+"\n", string(b))
_, err = io.WriteString(w, `{"data":{"value":"some data"}}`)
is.NoErr(err)
assert.NoError(t, err)
}))
defer srv.Close()
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
Expand All @@ -117,29 +110,27 @@ func TestQueryJSON(t *testing.T) {
req.Var("username", "matryer")

// check variables
is.True(req != nil)
is.Equal(req.vars["username"], "matryer")
assert.NotNil(t, req)
assert.Equal(t, "matryer", req.vars["username"])

var resp struct {
Value string
}
err := client.Run(ctx, req, &resp)
is.NoErr(err)
is.Equal(calls, 1)
assert.NoError(t, err)
assert.Equal(t, calls, 1)

is.Equal(resp.Value, "some data")
assert.Equal(t, "some data", resp.Value)
}

func TestHeader(t *testing.T) {
is := is.New(t)

var calls int
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
calls++
is.Equal(r.Header.Get("X-Custom-Header"), "123")
assert.Equal(t, "123", r.Header.Get("X-Custom-Header"))

_, err := io.WriteString(w, `{"data":{"value":"some data"}}`)
is.NoErr(err)
assert.NoError(t, err)
}))
defer srv.Close()
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
Expand All @@ -154,8 +145,8 @@ func TestHeader(t *testing.T) {
Value string
}
err := client.Run(ctx, req, &resp)
is.NoErr(err)
is.Equal(calls, 1)
assert.NoError(t, err)
assert.Equal(t, calls, 1)

is.Equal(resp.Value, "some data")
assert.Equal(t, "some data", resp.Value)
}
Loading