Skip to content

Commit

Permalink
fix linter
Browse files Browse the repository at this point in the history
  • Loading branch information
hgiasac committed Feb 3, 2024
1 parent 4df2563 commit 43a3a89
Show file tree
Hide file tree
Showing 16 changed files with 118 additions and 82 deletions.
10 changes: 10 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ jobs:
runs-on: ubuntu-20.04
permissions:
pull-requests: write
# Required: allow read access to the content for analysis.
contents: read
# Optional: Allow write access to checks to allow the action to annotate code in the PR.
checks: write
steps:
- name: Checkout
uses: actions/checkout@v3
Expand All @@ -42,6 +46,12 @@ jobs:
run: |
cd ./example/hasura
docker-compose up -d
- name: Lint
uses: golangci/golangci-lint-action@v3
with:
version: latest
only-new-issues: true
skip-cache: false
- name: Run Go unit tests
run: go test -v -race -timeout 3m -coverprofile=coverage.out ./...
- name: Go coverage format
Expand Down
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -571,6 +571,16 @@ client := graphql.NewSubscriptionClient("wss://example.com/graphql").
})
```
Some servers validate custom auth tokens on the header instead. To authenticate with headers, use `WebsocketOptions`:
```go
client := graphql.NewSubscriptionClient(serverEndpoint).
WithWebSocketOptions(graphql.WebsocketOptions{
HTTPHeader: http.Header{
"Authorization": []string{"Bearer random-secret"},
},
})
```
#### Options
Expand Down
4 changes: 3 additions & 1 deletion example/graphql-ws-bc/client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ func main() {
}
}

startSubscription(protocol)
if err := startSubscription(protocol); err != nil {
panic(err)
}
}

const serverEndpoint = "http://localhost:4000"
Expand Down
4 changes: 3 additions & 1 deletion example/hasura/client/graphql-ws/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ const (

func main() {
go insertUsers()
startSubscription()
if err := startSubscription(); err != nil {
panic(err)
}
}

func startSubscription() error {
Expand Down
6 changes: 4 additions & 2 deletions example/hasura/client/subscriptions-transport-ws/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ const (

func main() {
go insertUsers()
startSubscription()
if err := startSubscription(); err != nil {
panic(err)
}
}

func startSubscription() error {
Expand Down Expand Up @@ -73,7 +75,7 @@ func startSubscription() error {
// automatically unsubscribe after 10 seconds
go func() {
time.Sleep(10 * time.Second)
client.Unsubscribe(subId)
_ = client.Unsubscribe(subId)
}()

return client.Run()
Expand Down
14 changes: 0 additions & 14 deletions example/realworld/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import (
"encoding/json"
"flag"
"log"
"net/http"
"net/http/httptest"
"os"

graphql "github.com/hasura/go-graphql-client"
Expand Down Expand Up @@ -61,15 +59,3 @@ func print(v interface{}) {
panic(err)
}
}

// localRoundTripper is an http.RoundTripper that executes HTTP transactions
// by using handler directly, instead of going over an HTTP connection.
type localRoundTripper struct {
handler http.Handler
}

func (l localRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
w := httptest.NewRecorder()
l.handler.ServeHTTP(w, req)
return w.Result(), nil
}
2 changes: 1 addition & 1 deletion example/subscription/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func startSubscription() error {
// automatically unsubscribe after 10 seconds
go func() {
time.Sleep(10 * time.Second)
client.Unsubscribe(subId)
_ = client.Unsubscribe(subId)
}()

return client.Run()
Expand Down
4 changes: 3 additions & 1 deletion example/subscription/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,7 @@ package main
func main() {
go startServer()
go startSendHello()
startSubscription()
if err := startSubscription(); err != nil {
panic(err)
}
}
4 changes: 3 additions & 1 deletion example/tibber/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ const (
)

func main() {
startSubscription()
if err := startSubscription(); err != nil {
panic(err)
}
}

// the subscription uses the Real time subscription demo
Expand Down
6 changes: 3 additions & 3 deletions graphql.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ func (c *Client) request(ctx context.Context, query string, variables map[string
resp, err := c.httpClient.Do(request)

if c.debug {
reqReader.Seek(0, io.SeekStart)
_, _ = reqReader.Seek(0, io.SeekStart)
}

if err != nil {
Expand Down Expand Up @@ -206,7 +206,7 @@ func (c *Client) request(ctx context.Context, query string, variables map[string
err = json.NewDecoder(r).Decode(&out)

if c.debug {
respReader.Seek(0, io.SeekStart)
_, _ = respReader.Seek(0, io.SeekStart)
}

if err != nil {
Expand Down Expand Up @@ -344,7 +344,7 @@ func (e Error) Error() string {
func (e Errors) Error() string {
b := strings.Builder{}
for _, err := range e {
b.WriteString(err.Error())
_, _ = b.WriteString(err.Error())
}
return b.String()
}
Expand Down
6 changes: 5 additions & 1 deletion pkg/jsonutil/graphql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -416,13 +416,17 @@ func TestUnmarshalGraphQL_unexportedField(t *testing.T) {
type query struct {
foo *string
}
err := jsonutil.UnmarshalGraphQL([]byte(`{"foo": "bar"}`), new(query))
q := new(query)
err := jsonutil.UnmarshalGraphQL([]byte(`{"foo": "bar"}`), q)
if err == nil {
t.Fatal("got error: nil, want: non-nil")
}
if got, want := err.Error(), "struct field for \"foo\" doesn't exist in any of 1 places to unmarshal"; got != want {
t.Errorf("got error: %v, want: %v", got, want)
}
if q.foo != nil {
t.Errorf("expected foo = nil, got: %v", q.foo)
}
}

func TestUnmarshalGraphQL_multipleValues(t *testing.T) {
Expand Down
34 changes: 17 additions & 17 deletions query.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,9 @@ func queryArguments(variables map[string]interface{}) string {

var buf bytes.Buffer
for _, k := range keys {
io.WriteString(&buf, "$")
io.WriteString(&buf, k)
io.WriteString(&buf, ":")
_, _ = io.WriteString(&buf, "$")
_, _ = io.WriteString(&buf, k)
_, _ = io.WriteString(&buf, ":")
writeArgumentType(&buf, reflect.TypeOf(variables[k]), variables[k], true)
// Don't insert a comma here.
// Commas in GraphQL are insignificant, and we want minified output.
Expand All @@ -168,10 +168,10 @@ func writeArgumentType(w io.Writer, t reflect.Type, v interface{}, value bool) {
graphqlType, ok = reflect.Zero(t).Interface().(GraphQLType)
}
if ok {
io.WriteString(w, graphqlType.GetGraphQLType())
_, _ = io.WriteString(w, graphqlType.GetGraphQLType())
if value {
// Value is a required type, so add "!" to the end.
io.WriteString(w, "!")
_, _ = io.WriteString(w, "!")
}
return
}
Expand All @@ -186,27 +186,27 @@ func writeArgumentType(w io.Writer, t reflect.Type, v interface{}, value bool) {
switch t.Kind() {
case reflect.Slice, reflect.Array:
// List. E.g., "[Int]".
io.WriteString(w, "[")
_, _ = io.WriteString(w, "[")
writeArgumentType(w, t.Elem(), nil, true)
io.WriteString(w, "]")
_, _ = io.WriteString(w, "]")
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
io.WriteString(w, "Int")
_, _ = io.WriteString(w, "Int")
case reflect.Float32, reflect.Float64:
io.WriteString(w, "Float")
_, _ = io.WriteString(w, "Float")
case reflect.Bool:
io.WriteString(w, "Boolean")
_, _ = io.WriteString(w, "Boolean")
default:
n := t.Name()
if n == "string" {
n = "String"
}
io.WriteString(w, n)
_, _ = io.WriteString(w, n)
}

if value {
// Value is a required type, so add "!" to the end.
io.WriteString(w, "!")
_, _ = io.WriteString(w, "!")
}
}

Expand Down Expand Up @@ -241,7 +241,7 @@ func writeQuery(w io.Writer, t reflect.Type, v reflect.Value, inline bool) error
return nil
}
if !inline {
io.WriteString(w, "{")
_, _ = io.WriteString(w, "{")
}
iter := 0
for i := 0; i < t.NumField(); i++ {
Expand All @@ -252,16 +252,16 @@ func writeQuery(w io.Writer, t reflect.Type, v reflect.Value, inline bool) error
continue
}
if iter != 0 {
io.WriteString(w, ",")
_, _ = io.WriteString(w, ",")
}
iter++

inlineField := f.Anonymous && !ok
if !inlineField {
if ok {
io.WriteString(w, value)
_, _ = io.WriteString(w, value)
} else {
io.WriteString(w, ident.ParseMixedCaps(f.Name).ToLowerCamelCase())
_, _ = io.WriteString(w, ident.ParseMixedCaps(f.Name).ToLowerCamelCase())
}
}
// Skip writeQuery if the GraphQL type associated with the filed is scalar
Expand All @@ -274,7 +274,7 @@ func writeQuery(w io.Writer, t reflect.Type, v reflect.Value, inline bool) error
}
}
if !inline {
io.WriteString(w, "}")
_, _ = io.WriteString(w, "}")
}
case reflect.Slice:
if t.Elem().Kind() != reflect.Array {
Expand Down
2 changes: 1 addition & 1 deletion subscription.go
Original file line number Diff line number Diff line change
Expand Up @@ -885,7 +885,7 @@ func (sc *SubscriptionClient) reset() {
continue
}
if sub.status == SubscriptionRunning {
sc.protocol.Unsubscribe(subContext, sub)
_ = sc.protocol.Unsubscribe(subContext, sub)
}

// should restart subscriptions with new id
Expand Down
10 changes: 5 additions & 5 deletions subscription_graphql_ws_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ func TestGraphqlWS_Subscription(t *testing.T) {

go func() {
if err := subscriptionClient.Run(); err == nil || err.Error() != "exit" {
(*t).Fatalf("got error: %v, want: exit", err)
t.Errorf("got error: %v, want: exit", err)
}
stop <- true
}()
Expand Down Expand Up @@ -236,7 +236,7 @@ func TestGraphqlWS_SubscriptionRerun(t *testing.T) {

go func() {
if err := subscriptionClient.Run(); err != nil {
(*t).Fatalf("got error: %v, want: nil", err)
t.Errorf("got error: %v, want: nil", err)
}
}()

Expand Down Expand Up @@ -280,11 +280,11 @@ func TestGraphqlWS_SubscriptionRerun(t *testing.T) {
time.Sleep(2 * time.Second)
go func() {
time.Sleep(2 * time.Second)
subscriptionClient.Unsubscribe(subId1)
_ = subscriptionClient.Unsubscribe(subId1)
}()

if err := subscriptionClient.Run(); err != nil {
(*t).Fatalf("got error: %v, want: nil", err)
t.Fatalf("got error: %v, want: nil", err)
}
}

Expand Down Expand Up @@ -351,7 +351,7 @@ func TestGraphQLWS_OnError(t *testing.T) {

go func() {
if err := subscriptionClient.Run(); err == nil || websocket.CloseStatus(err) != 4400 {
(*t).Fatalf("got error: %v, want: 4400", err)
t.Errorf("got error: %v, want: 4400", err)
}
stop <- true
}()
Expand Down
Loading

0 comments on commit 43a3a89

Please sign in to comment.