Skip to content

Commit

Permalink
updates to match new FieldResolveFn returning params - graphql-go#44
Browse files Browse the repository at this point in the history
  • Loading branch information
chris-ramon committed Nov 25, 2015
1 parent 149040d commit 67d621a
Show file tree
Hide file tree
Showing 14 changed files with 207 additions and 207 deletions.
70 changes: 35 additions & 35 deletions abstract_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,20 +48,20 @@ func TestIsTypeOfUsedToResolveRuntimeTypeForInterface(t *testing.T) {
Fields: graphql.Fields{
"name": &graphql.Field{
Type: graphql.String,
Resolve: func(p graphql.ResolveParams) interface{} {
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
if dog, ok := p.Source.(*testDog); ok {
return dog.Name
return dog.Name, nil
}
return nil
return nil, nil
},
},
"woofs": &graphql.Field{
Type: graphql.Boolean,
Resolve: func(p graphql.ResolveParams) interface{} {
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
if dog, ok := p.Source.(*testDog); ok {
return dog.Woofs
return dog.Woofs, nil
}
return nil
return nil, nil
},
},
},
Expand All @@ -79,20 +79,20 @@ func TestIsTypeOfUsedToResolveRuntimeTypeForInterface(t *testing.T) {
Fields: graphql.Fields{
"name": &graphql.Field{
Type: graphql.String,
Resolve: func(p graphql.ResolveParams) interface{} {
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
if cat, ok := p.Source.(*testCat); ok {
return cat.Name
return cat.Name, nil
}
return nil
return nil, nil
},
},
"meows": &graphql.Field{
Type: graphql.Boolean,
Resolve: func(p graphql.ResolveParams) interface{} {
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
if cat, ok := p.Source.(*testCat); ok {
return cat.Meows
return cat.Meows, nil
}
return nil
return nil, nil
},
},
},
Expand All @@ -103,11 +103,11 @@ func TestIsTypeOfUsedToResolveRuntimeTypeForInterface(t *testing.T) {
Fields: graphql.Fields{
"pets": &graphql.Field{
Type: graphql.NewList(petType),
Resolve: func(p graphql.ResolveParams) interface{} {
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
return []interface{}{
&testDog{"Odie", true},
&testCat{"Garfield", false},
}
}, nil
},
},
},
Expand Down Expand Up @@ -202,11 +202,11 @@ func TestIsTypeOfUsedToResolveRuntimeTypeForUnion(t *testing.T) {
Fields: graphql.Fields{
"pets": &graphql.Field{
Type: graphql.NewList(petType),
Resolve: func(p graphql.ResolveParams) interface{} {
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
return []interface{}{
&testDog{"Odie", true},
&testCat{"Garfield", false},
}
}, nil
},
},
},
Expand Down Expand Up @@ -288,11 +288,11 @@ func TestResolveTypeOnInterfaceYieldsUsefulError(t *testing.T) {
Fields: graphql.Fields{
"name": &graphql.Field{
Type: graphql.String,
Resolve: func(p graphql.ResolveParams) interface{} {
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
if human, ok := p.Source.(*testHuman); ok {
return human.Name
return human.Name, nil
}
return nil
return nil, nil
},
},
},
Expand All @@ -309,20 +309,20 @@ func TestResolveTypeOnInterfaceYieldsUsefulError(t *testing.T) {
Fields: graphql.Fields{
"name": &graphql.Field{
Type: graphql.String,
Resolve: func(p graphql.ResolveParams) interface{} {
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
if dog, ok := p.Source.(*testDog); ok {
return dog.Name
return dog.Name, nil
}
return nil
return nil, nil
},
},
"woofs": &graphql.Field{
Type: graphql.Boolean,
Resolve: func(p graphql.ResolveParams) interface{} {
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
if dog, ok := p.Source.(*testDog); ok {
return dog.Woofs
return dog.Woofs, nil
}
return nil
return nil, nil
},
},
},
Expand All @@ -339,20 +339,20 @@ func TestResolveTypeOnInterfaceYieldsUsefulError(t *testing.T) {
Fields: graphql.Fields{
"name": &graphql.Field{
Type: graphql.String,
Resolve: func(p graphql.ResolveParams) interface{} {
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
if cat, ok := p.Source.(*testCat); ok {
return cat.Name
return cat.Name, nil
}
return nil
return nil, nil
},
},
"meows": &graphql.Field{
Type: graphql.Boolean,
Resolve: func(p graphql.ResolveParams) interface{} {
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
if cat, ok := p.Source.(*testCat); ok {
return cat.Meows
return cat.Meows, nil
}
return nil
return nil, nil
},
},
},
Expand All @@ -363,12 +363,12 @@ func TestResolveTypeOnInterfaceYieldsUsefulError(t *testing.T) {
Fields: graphql.Fields{
"pets": &graphql.Field{
Type: graphql.NewList(petType),
Resolve: func(p graphql.ResolveParams) interface{} {
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
return []interface{}{
&testDog{"Odie", true},
&testCat{"Garfield", false},
&testHuman{"Jon"},
}
}, nil
},
},
},
Expand Down Expand Up @@ -480,12 +480,12 @@ func TestResolveTypeOnUnionYieldsUsefulError(t *testing.T) {
Fields: graphql.Fields{
"pets": &graphql.Field{
Type: graphql.NewList(petType),
Resolve: func(p graphql.ResolveParams) interface{} {
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
return []interface{}{
&testDog{"Odie", true},
&testCat{"Garfield", false},
&testHuman{"Jon"},
}
}, nil
},
},
},
Expand Down
24 changes: 12 additions & 12 deletions enum_type_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,17 @@ var enumTypeTestQueryType = graphql.NewObject(graphql.ObjectConfig{
Type: graphql.String,
},
},
Resolve: func(p graphql.ResolveParams) interface{} {
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
if fromInt, ok := p.Args["fromInt"]; ok {
return fromInt
return fromInt, nil
}
if fromString, ok := p.Args["fromString"]; ok {
return fromString
return fromString, nil
}
if fromEnum, ok := p.Args["fromEnum"]; ok {
return fromEnum
return fromEnum, nil
}
return nil
return nil, nil
},
},
"colorInt": &graphql.Field{
Expand All @@ -62,14 +62,14 @@ var enumTypeTestQueryType = graphql.NewObject(graphql.ObjectConfig{
Type: graphql.Int,
},
},
Resolve: func(p graphql.ResolveParams) interface{} {
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
if fromInt, ok := p.Args["fromInt"]; ok {
return fromInt
return fromInt, nil
}
if fromEnum, ok := p.Args["fromEnum"]; ok {
return fromEnum
return fromEnum, nil
}
return nil
return nil, nil
},
},
},
Expand All @@ -84,11 +84,11 @@ var enumTypeTestMutationType = graphql.NewObject(graphql.ObjectConfig{
Type: enumTypeTestColorType,
},
},
Resolve: func(p graphql.ResolveParams) interface{} {
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
if color, ok := p.Args["color"]; ok {
return color
return color, nil
}
return nil
return nil, nil
},
},
},
Expand Down
4 changes: 2 additions & 2 deletions examples/hello-world/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ func main() {
fields := graphql.Fields{
"hello": &graphql.Field{
Type: graphql.String,
Resolve: func(p graphql.ResolveParams) interface{} {
return "world"
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
return "world", nil
},
},
}
Expand Down
6 changes: 3 additions & 3 deletions examples/http/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,12 @@ var queryType = graphql.NewObject(
Type: graphql.String,
},
},
Resolve: func(p graphql.ResolveParams) interface{} {
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
idQuery, isOK := p.Args["id"].(string)
if isOK {
return data[idQuery]
return data[idQuery], nil
}
return nil
return nil, nil
},
},
},
Expand Down
18 changes: 9 additions & 9 deletions executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -498,7 +498,7 @@ func resolveField(eCtx *ExecutionContext, parentType *Object, source interface{}
// it is wrapped as a Error with locations. Log this error and return
// null if allowed, otherwise throw the error so the parent field can handle
// it.
result = resolveFn(ResolveParams{
result, _ = resolveFn(ResolveParams{
Source: source,
Args: args,
Info: info,
Expand Down Expand Up @@ -666,14 +666,14 @@ func completeValue(eCtx *ExecutionContext, returnType Type, fieldASTs []*ast.Fie

}

func defaultResolveFn(p ResolveParams) interface{} {
func defaultResolveFn(p ResolveParams) (interface{}, error) {
// try to resolve p.Source as a struct first
sourceVal := reflect.ValueOf(p.Source)
if sourceVal.IsValid() && sourceVal.Type().Kind() == reflect.Ptr {
sourceVal = sourceVal.Elem()
}
if !sourceVal.IsValid() {
return nil
return nil, nil
}
if sourceVal.Type().Kind() == reflect.Struct {
// find field based on struct's json tag
Expand All @@ -685,7 +685,7 @@ func defaultResolveFn(p ResolveParams) interface{} {
typeField := sourceVal.Type().Field(i)
// try matching the field name first
if typeField.Name == p.Info.FieldName {
return valueField.Interface()
return valueField.Interface(), nil
}
tag := typeField.Tag
jsonTag := tag.Get("json")
Expand All @@ -696,9 +696,9 @@ func defaultResolveFn(p ResolveParams) interface{} {
if jsonOptions[0] != p.Info.FieldName {
continue
}
return valueField.Interface()
return valueField.Interface(), nil
}
return nil
return nil, nil
}

// try p.Source as a map[string]interface
Expand All @@ -709,14 +709,14 @@ func defaultResolveFn(p ResolveParams) interface{} {
// try type casting the func to the most basic func signature
// for more complex signatures, user have to define ResolveFn
if propertyFn, ok := property.(func() interface{}); ok {
return propertyFn()
return propertyFn(), nil
}
}
return property
return property, nil
}

// last resort, return nil
return nil
return nil, nil
}

/**
Expand Down
14 changes: 7 additions & 7 deletions executor_schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,13 +106,13 @@ func TestExecutesUsingAComplexSchema(t *testing.T) {
Type: graphql.Int,
},
},
Resolve: func(p graphql.ResolveParams) interface{} {
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
if author, ok := p.Source.(*testAuthor); ok {
width := fmt.Sprintf("%v", p.Args["width"])
height := fmt.Sprintf("%v", p.Args["height"])
return author.Pic(width, height)
return author.Pic(width, height), nil
}
return nil
return nil, nil
},
},
"recentArticle": &graphql.Field{},
Expand Down Expand Up @@ -156,14 +156,14 @@ func TestExecutesUsingAComplexSchema(t *testing.T) {
Type: graphql.ID,
},
},
Resolve: func(p graphql.ResolveParams) interface{} {
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
id := p.Args["id"]
return article(id)
return article(id), nil
},
},
"feed": &graphql.Field{
Type: graphql.NewList(blogArticle),
Resolve: func(p graphql.ResolveParams) interface{} {
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
return []*testArticle{
article(1),
article(2),
Expand All @@ -175,7 +175,7 @@ func TestExecutesUsingAComplexSchema(t *testing.T) {
article(8),
article(9),
article(10),
}
}, nil
},
},
},
Expand Down
Loading

0 comments on commit 67d621a

Please sign in to comment.