Skip to content

Commit

Permalink
Merge branch 'upstream-v1.3.0-for-merge' into update-upstream-v1.3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
anufant21 committed Feb 5, 2024
2 parents fd2970f + a41fe07 commit 4ab2040
Show file tree
Hide file tree
Showing 32 changed files with 1,544 additions and 114 deletions.
35 changes: 35 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
run:
timeout: 5m

linters-settings:
gofmt:
simplify: true
govet:
check-shadowing: true
enable-all: true
disable:
- fieldalignment
- deepequalerrors # remove later

linters:
disable-all: true
enable:
- deadcode
- gofmt
- gosimple
- govet
- ineffassign
- exportloopref
- structcheck
- staticcheck
- unconvert
- unused
- varcheck
- misspell
- goimports

issues:
exclude-rules:
- linters:
- unused
path: "graphql_test.go"
35 changes: 35 additions & 0 deletions .semaphore/semaphore.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
version: v1.0
name: Go
agent:
machine:
type: e1-standard-2
os_image: ubuntu2004
blocks:
- name: Style Check
task:
jobs:
- name: fmt
commands:
- sem-version go 1.17
- checkout
- ./scripts/golangci_install.sh -b $(go env GOPATH)/bin v1.42.1
- export PATH=$(go env GOPATH)/bin:$PATH
- golangci-lint run ./...

- name: Test & Build
task:
prologue:
commands:
- sem-version go 1.17
- export PATH=$(go env GOPATH)/bin:$PATH
- checkout
- go version

jobs:
- name: Test
commands:
- go test ./...

- name: Build
commands:
- go build -v .
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
CHANGELOG

[v1.1.0](https://github.com/graph-gophers/graphql-go/releases/tag/v1.1.0) Release v1.1.0
* [FEATURE] Add types package #437
* [FEATURE] Expose `packer.Unmarshaler` as `decode.Unmarshaler` to the public #450
* [FEATURE] Add location fields to type definitions #454
* [FEATURE] `errors.Errorf` preserves original error similar to `fmt.Errorf` #456
* [BUGFIX] Fix duplicated __typename in response (fixes #369) #443

[v1.0.0](https://github.com/graph-gophers/graphql-go/releases/tag/v1.0.0) Initial release
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# graphql-go [![Sourcegraph](https://sourcegraph.com/github.com/tribunadigital/graphql-go/-/badge.svg)](https://sourcegraph.com/github.com/tribunadigital/graphql-go?badge) [![Build Status](https://semaphoreci.com/api/v1/tribunadigital/graphql-go/branches/master/badge.svg)](https://semaphoreci.com/tribunadigital/graphql-go) [![GoDoc](https://godoc.org/github.com/tribunadigital/graphql-go?status.svg)](https://godoc.org/github.com/tribunadigital/graphql-go)
# graphql-go [![Sourcegraph](https://sourcegraph.com/github.com/graph-gophers/graphql-go/-/badge.svg)](https://sourcegraph.com/github.com/graph-gophers/graphql-go?badge) [![Build Status](https://graph-gophers.semaphoreci.com/badges/graphql-go/branches/master.svg?style=shields)](https://graph-gophers.semaphoreci.com/projects/graphql-go) [![GoDoc](https://godoc.org/github.com/graph-gophers/graphql-go?status.svg)](https://godoc.org/github.com/graph-gophers/graphql-go)

<p align="center"><img src="docs/img/logo.png" width="300"></p>

Expand Down Expand Up @@ -56,8 +56,9 @@ func main() {
```

To test:

```sh
$ curl -XPOST -d '{"query": "{ hello }"}' localhost:8080/query
curl -XPOST -d '{"query": "{ hello }"}' localhost:8080/query
```

### Resolvers
Expand All @@ -67,7 +68,7 @@ You can use struct fields as resolvers by using `SchemaOpt: UseFieldResolvers()`
```
opts := []graphql.SchemaOpt{graphql.UseFieldResolvers()}
schema := graphql.MustParseSchema(s, &query{}, opts...)
```
```

When using `UseFieldResolvers` schema option, a struct field will be used *only* when:
- there is no method for a struct field
Expand Down Expand Up @@ -109,6 +110,7 @@ func (r *helloWorldResolver) Hello(ctx context.Context) (string, error) {
- `Tracer(tracer trace.Tracer)` is used to trace queries and fields. It defaults to `trace.OpenTracingTracer`.
- `ValidationTracer(tracer trace.ValidationTracer)` is used to trace validation errors. It defaults to `trace.NoopValidationTracer`.
- `Logger(logger log.Logger)` is used to log panics during query execution. It defaults to `exec.DefaultLogger`.
- `PanicHandler(panicHandler errors.PanicHandler)` is used to transform panics into errors during query execution. It defaults to `errors.DefaultPanicHandler`.
- `DisableIntrospection()` disables introspection queries.

### Custom Errors
Expand Down
18 changes: 18 additions & 0 deletions errors/panic_handler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package errors

import (
"context"
)

// PanicHandler is the interface used to create custom panic errors that occur during query execution
type PanicHandler interface {
MakePanicError(ctx context.Context, value interface{}) *QueryError
}

// DefaultPanicHandler is the default PanicHandler
type DefaultPanicHandler struct{}

// MakePanicError creates a new QueryError from a panic that occurred during execution
func (h *DefaultPanicHandler) MakePanicError(ctx context.Context, value interface{}) *QueryError {
return Errorf("panic occurred: %v", value)
}
24 changes: 24 additions & 0 deletions errors/panic_handler_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package errors

import (
"context"
"testing"
)

func TestDefaultPanicHandler(t *testing.T) {
handler := &DefaultPanicHandler{}
qErr := handler.MakePanicError(context.Background(), "foo")
if qErr == nil {
t.Fatal("Panic error must not be nil")
}
const (
expectedMessage = "panic occurred: foo"
expectedError = "graphql: " + expectedMessage
)
if qErr.Error() != expectedError {
t.Errorf("Unexpected panic error message: %q != %q", qErr.Error(), expectedError)
}
if qErr.Message != expectedMessage {
t.Errorf("Unexpected panic QueryError.Message: %q != %q", qErr.Message, expectedMessage)
}
}
45 changes: 45 additions & 0 deletions example/scalar_map/server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package main

import (
"fmt"
"log"
"net/http"

graphql "github.com/tribunadigital/graphql-go"
"github.com/tribunadigital/graphql-go/example/scalar_map/types"
"github.com/tribunadigital/graphql-go/relay"
)

type Args struct {
Name string
Data types.Map
}

type mutation struct{}

func (_ *mutation) Hello(args Args) string {

fmt.Println(args)

return "Args accept!"
}

func main() {
s := `
scalar Map
type Query {}
type Mutation {
hello(
name: String!
data: Map!
): String!
}
`
schema := graphql.MustParseSchema(s, &mutation{})
http.Handle("/query", &relay.Handler{Schema: schema})

log.Println("Listen in port :8080")
log.Fatal(http.ListenAndServe(":8080", nil))
}
19 changes: 19 additions & 0 deletions example/scalar_map/types/map.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package types

import "fmt"

type Map map[string]interface{}

func (Map) ImplementsGraphQLType(name string) bool {
return name == "Map"
}

func (j *Map) UnmarshalGraphQL(input interface{}) error {
json, ok := input.(map[string]interface{})
if !ok {
return fmt.Errorf("wrong type")
}

*j = json
return nil
}
24 changes: 17 additions & 7 deletions graphql.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"encoding/json"
"fmt"
"reflect"
"time"

"github.com/tribunadigital/graphql-go/errors"
Expand All @@ -30,6 +29,7 @@ func ParseSchema(schemaString string, resolver interface{}, opts ...SchemaOpt) (
maxParallelism: 10,
tracer: trace.OpenTracingTracer{},
logger: &log.DefaultLogger{},
panicHandler: &errors.DefaultPanicHandler{},
}
for _, opt := range opts {
opt(s)
Expand Down Expand Up @@ -78,6 +78,7 @@ type Schema struct {
tracer trace.Tracer
validationTracer trace.ValidationTracerContext
logger log.Logger
panicHandler errors.PanicHandler
useStringDescriptions bool
disableIntrospection bool
subscribeResolverTimeout time.Duration
Expand Down Expand Up @@ -138,7 +139,7 @@ func Tracer(tracer trace.Tracer) SchemaOpt {

// ValidationTracer is used to trace validation errors. It defaults to trace.NoopValidationTracer.
// Deprecated: context is needed to support tracing correctly. Use a Tracer which implements trace.ValidationTracerContext.
func ValidationTracer(tracer trace.ValidationTracer) SchemaOpt {
func ValidationTracer(tracer trace.ValidationTracer) SchemaOpt { //nolint:staticcheck
return func(s *Schema) {
s.validationTracer = &validationBridgingTracer{tracer: tracer}
}
Expand All @@ -151,6 +152,14 @@ func Logger(logger log.Logger) SchemaOpt {
}
}

// PanicHandler is used to customize the panic errors during query execution.
// It defaults to errors.DefaultPanicHandler.
func PanicHandler(panicHandler errors.PanicHandler) SchemaOpt {
return func(s *Schema) {
s.panicHandler = panicHandler
}
}

// DisableIntrospection disables introspection queries.
func DisableIntrospection() SchemaOpt {
return func(s *Schema) {
Expand Down Expand Up @@ -195,7 +204,7 @@ func (s *Schema) ValidateWithVariables(queryString string, variables map[string]
// without a resolver. If the context get cancelled, no further resolvers will be called and a
// the context error will be returned as soon as possible (not immediately).
func (s *Schema) Exec(ctx context.Context, queryString string, operationName string, variables map[string]interface{}) *Response {
if s.res.Resolver == (reflect.Value{}) {
if !s.res.Resolver.IsValid() {
panic("schema created without resolver, can not exec")
}
return s.exec(ctx, queryString, operationName, variables, s.res)
Expand Down Expand Up @@ -252,9 +261,10 @@ func (s *Schema) exec(ctx context.Context, queryString string, operationName str
Schema: s.schema,
DisableIntrospection: s.disableIntrospection,
},
Limiter: make(chan struct{}, s.maxParallelism),
Tracer: s.tracer,
Logger: s.logger,
Limiter: make(chan struct{}, s.maxParallelism),
Tracer: s.tracer,
Logger: s.logger,
PanicHandler: s.panicHandler,
}
varTypes := make(map[string]*introspection.Type)
for _, v := range op.Vars {
Expand Down Expand Up @@ -294,7 +304,7 @@ func (s *Schema) validateSchema() error {
}

type validationBridgingTracer struct {
tracer trace.ValidationTracer
tracer trace.ValidationTracer //nolint:staticcheck
}

func (t *validationBridgingTracer) TraceValidation(context.Context) trace.TraceValidationFinishFunc {
Expand Down
Loading

0 comments on commit 4ab2040

Please sign in to comment.