Skip to content

Commit e13376c

Browse files
committed
Merge branch 'master' into sogko/0.6.0
* 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
2 parents 6fecefe + 22050ee commit e13376c

22 files changed

+561
-236
lines changed

.travis.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ language: go
22

33
go:
44
- 1.4
5+
- 1.7
6+
- tip
57

68
before_install:
79
- go get github.com/axw/gocov/gocov

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

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

5+
### Documentation
6+
7+
godoc: https://godoc.org/github.com/graphql-go/graphql
8+
59
### Getting Started
610

711
To install the library, run:

definition.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,7 @@ type IsTypeOfFn func(p IsTypeOfParams) bool
392392
type InterfacesThunk func() []*Interface
393393

394394
type ObjectConfig struct {
395-
Name string `json:"description"`
395+
Name string `json:"name"`
396396
Interfaces interface{} `json:"interfaces"`
397397
Fields interface{} `json:"fields"`
398398
IsTypeOf IsTypeOfFn `json:"isTypeOf"`

executor.go

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -802,10 +802,6 @@ func defaultResolveFn(p ResolveParams) (interface{}, error) {
802802
return nil, nil
803803
}
804804
if sourceVal.Type().Kind() == reflect.Struct {
805-
// find field based on struct's json tag
806-
// we could potentially create a custom `graphql` tag, but its unnecessary at this point
807-
// since graphql speaks to client in a json-like way anyway
808-
// so json tags are a good way to start with
809805
for i := 0; i < sourceVal.NumField(); i++ {
810806
valueField := sourceVal.Field(i)
811807
typeField := sourceVal.Type().Field(i)
@@ -814,15 +810,22 @@ func defaultResolveFn(p ResolveParams) (interface{}, error) {
814810
return valueField.Interface(), nil
815811
}
816812
tag := typeField.Tag
817-
jsonTag := tag.Get("json")
818-
jsonOptions := strings.Split(jsonTag, ",")
819-
if len(jsonOptions) == 0 {
820-
continue
813+
checkTag := func(tagName string) bool {
814+
t := tag.Get(tagName)
815+
tOptions := strings.Split(t, ",")
816+
if len(tOptions) == 0 {
817+
return false
818+
}
819+
if tOptions[0] != p.Info.FieldName {
820+
return false
821+
}
822+
return true
821823
}
822-
if jsonOptions[0] != p.Info.FieldName {
824+
if checkTag("json") || checkTag("graphql") {
825+
return valueField.Interface(), nil
826+
} else {
823827
continue
824828
}
825-
return valueField.Interface(), nil
826829
}
827830
return nil, nil
828831
}

executor_test.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1592,3 +1592,50 @@ func TestMutation_ExecutionDoesNotAddErrorsFromFieldResolveFn(t *testing.T) {
15921592
t.Fatalf("wrong result, unexpected errors: %+v", result.Errors)
15931593
}
15941594
}
1595+
1596+
func TestGraphqlTag(t *testing.T) {
1597+
typeObjectType := graphql.NewObject(graphql.ObjectConfig{
1598+
Name: "Type",
1599+
Fields: graphql.Fields{
1600+
"fooBar": &graphql.Field{Type: graphql.String},
1601+
},
1602+
})
1603+
var baz = &graphql.Field{
1604+
Type: typeObjectType,
1605+
Description: "typeObjectType",
1606+
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
1607+
t := struct {
1608+
FooBar string `graphql:"fooBar"`
1609+
}{"foo bar value"}
1610+
return t, nil
1611+
},
1612+
}
1613+
q := graphql.NewObject(graphql.ObjectConfig{
1614+
Name: "Query",
1615+
Fields: graphql.Fields{
1616+
"baz": baz,
1617+
},
1618+
})
1619+
schema, err := graphql.NewSchema(graphql.SchemaConfig{
1620+
Query: q,
1621+
})
1622+
if err != nil {
1623+
t.Fatalf("unexpected error, got: %v", err)
1624+
}
1625+
query := "{ baz { fooBar } }"
1626+
result := graphql.Do(graphql.Params{
1627+
Schema: schema,
1628+
RequestString: query,
1629+
})
1630+
if len(result.Errors) != 0 {
1631+
t.Fatalf("wrong result, unexpected errors: %+v", result.Errors)
1632+
}
1633+
expectedData := map[string]interface{}{
1634+
"baz": map[string]interface{}{
1635+
"fooBar": "foo bar value",
1636+
},
1637+
}
1638+
if !reflect.DeepEqual(result.Data, expectedData) {
1639+
t.Fatalf("unexpected result, got: %+v, expected: %+v", expectedData, result.Data)
1640+
}
1641+
}

gqlerrors/syntax.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ package gqlerrors
33
import (
44
"fmt"
55
"regexp"
6+
"strings"
67

78
"github.com/graphql-go/graphql/language/ast"
89
"github.com/graphql-go/graphql/language/location"
910
"github.com/graphql-go/graphql/language/source"
10-
"strings"
1111
)
1212

1313
func NewSyntaxError(s *source.Source, position int, description string) *Error {
@@ -44,7 +44,7 @@ func highlightSourceAtLocation(s *source.Source, l location.SourceLocation) stri
4444
lineNum := fmt.Sprintf("%d", line)
4545
nextLineNum := fmt.Sprintf("%d", (line + 1))
4646
padLen := len(nextLineNum)
47-
lines := regexp.MustCompile("\r\n|[\n\r]").Split(s.Body, -1)
47+
lines := regexp.MustCompile("\r\n|[\n\r]").Split(string(s.Body), -1)
4848
var highlight string
4949
if line >= 2 {
5050
highlight += fmt.Sprintf("%s: %s\n", lpad(padLen, prevLineNum), printLine(lines[line-2]))

graphql.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ type Params struct {
3434

3535
func Do(p Params) *Result {
3636
source := source.NewSource(&source.Source{
37-
Body: p.RequestString,
37+
Body: []byte(p.RequestString),
3838
Name: "GraphQL request",
3939
})
4040
AST, err := parser.Parse(parser.ParseParams{Source: source})

0 commit comments

Comments
 (0)