Skip to content

Commit

Permalink
Merge branch 'master' into sogko/0.6.0
Browse files Browse the repository at this point in the history
* master:
  gofmt -s
  executor: add graphql tag
  Fix ObjectConfig duplicate json tag. Name had a 'description' json tag which was duplicate
  Minor fix to stop `go vet` from complaining
  Add 1.7 and tip to build matrix for Go
  Minor fix to stop `go vet` from complaining
  README: add Documentation section
  Fixes tests that was broken with enhancements made to the `lexer` with commit #137
  Improve lexer performance by using a byte array as source
  Updated `graphql.Float` coercion - if value type is explicitly `float32`, return `float32` - if value type is explicitly `float64`, return `float64` - if value type is `string`, return `float64` - for everything else, use system-default
  • Loading branch information
sogko committed Jan 27, 2017
2 parents 6fecefe + 22050ee commit e13376c
Show file tree
Hide file tree
Showing 22 changed files with 561 additions and 236 deletions.
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ language: go

go:
- 1.4
- 1.7
- tip

before_install:
- go get github.com/axw/gocov/gocov
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

A *work-in-progress* implementation of GraphQL for Go.

### Documentation

godoc: https://godoc.org/github.com/graphql-go/graphql

### Getting Started

To install the library, run:
Expand Down
2 changes: 1 addition & 1 deletion definition.go
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,7 @@ type IsTypeOfFn func(p IsTypeOfParams) bool
type InterfacesThunk func() []*Interface

type ObjectConfig struct {
Name string `json:"description"`
Name string `json:"name"`
Interfaces interface{} `json:"interfaces"`
Fields interface{} `json:"fields"`
IsTypeOf IsTypeOfFn `json:"isTypeOf"`
Expand Down
23 changes: 13 additions & 10 deletions executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -802,10 +802,6 @@ func defaultResolveFn(p ResolveParams) (interface{}, error) {
return nil, nil
}
if sourceVal.Type().Kind() == reflect.Struct {
// find field based on struct's json tag
// we could potentially create a custom `graphql` tag, but its unnecessary at this point
// since graphql speaks to client in a json-like way anyway
// so json tags are a good way to start with
for i := 0; i < sourceVal.NumField(); i++ {
valueField := sourceVal.Field(i)
typeField := sourceVal.Type().Field(i)
Expand All @@ -814,15 +810,22 @@ func defaultResolveFn(p ResolveParams) (interface{}, error) {
return valueField.Interface(), nil
}
tag := typeField.Tag
jsonTag := tag.Get("json")
jsonOptions := strings.Split(jsonTag, ",")
if len(jsonOptions) == 0 {
continue
checkTag := func(tagName string) bool {
t := tag.Get(tagName)
tOptions := strings.Split(t, ",")
if len(tOptions) == 0 {
return false
}
if tOptions[0] != p.Info.FieldName {
return false
}
return true
}
if jsonOptions[0] != p.Info.FieldName {
if checkTag("json") || checkTag("graphql") {
return valueField.Interface(), nil
} else {
continue
}
return valueField.Interface(), nil
}
return nil, nil
}
Expand Down
47 changes: 47 additions & 0 deletions executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1592,3 +1592,50 @@ func TestMutation_ExecutionDoesNotAddErrorsFromFieldResolveFn(t *testing.T) {
t.Fatalf("wrong result, unexpected errors: %+v", result.Errors)
}
}

func TestGraphqlTag(t *testing.T) {
typeObjectType := graphql.NewObject(graphql.ObjectConfig{
Name: "Type",
Fields: graphql.Fields{
"fooBar": &graphql.Field{Type: graphql.String},
},
})
var baz = &graphql.Field{
Type: typeObjectType,
Description: "typeObjectType",
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
t := struct {
FooBar string `graphql:"fooBar"`
}{"foo bar value"}
return t, nil
},
}
q := graphql.NewObject(graphql.ObjectConfig{
Name: "Query",
Fields: graphql.Fields{
"baz": baz,
},
})
schema, err := graphql.NewSchema(graphql.SchemaConfig{
Query: q,
})
if err != nil {
t.Fatalf("unexpected error, got: %v", err)
}
query := "{ baz { fooBar } }"
result := graphql.Do(graphql.Params{
Schema: schema,
RequestString: query,
})
if len(result.Errors) != 0 {
t.Fatalf("wrong result, unexpected errors: %+v", result.Errors)
}
expectedData := map[string]interface{}{
"baz": map[string]interface{}{
"fooBar": "foo bar value",
},
}
if !reflect.DeepEqual(result.Data, expectedData) {
t.Fatalf("unexpected result, got: %+v, expected: %+v", expectedData, result.Data)
}
}
4 changes: 2 additions & 2 deletions gqlerrors/syntax.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ package gqlerrors
import (
"fmt"
"regexp"
"strings"

"github.com/graphql-go/graphql/language/ast"
"github.com/graphql-go/graphql/language/location"
"github.com/graphql-go/graphql/language/source"
"strings"
)

func NewSyntaxError(s *source.Source, position int, description string) *Error {
Expand Down Expand Up @@ -44,7 +44,7 @@ func highlightSourceAtLocation(s *source.Source, l location.SourceLocation) stri
lineNum := fmt.Sprintf("%d", line)
nextLineNum := fmt.Sprintf("%d", (line + 1))
padLen := len(nextLineNum)
lines := regexp.MustCompile("\r\n|[\n\r]").Split(s.Body, -1)
lines := regexp.MustCompile("\r\n|[\n\r]").Split(string(s.Body), -1)
var highlight string
if line >= 2 {
highlight += fmt.Sprintf("%s: %s\n", lpad(padLen, prevLineNum), printLine(lines[line-2]))
Expand Down
2 changes: 1 addition & 1 deletion graphql.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ type Params struct {

func Do(p Params) *Result {
source := source.NewSource(&source.Source{
Body: p.RequestString,
Body: []byte(p.RequestString),
Name: "GraphQL request",
})
AST, err := parser.Parse(parser.ParseParams{Source: source})
Expand Down
Loading

0 comments on commit e13376c

Please sign in to comment.