Skip to content

Commit

Permalink
exposing Path in ResolveInfo to follow the reference implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
branden-blackline committed Oct 2, 2018
1 parent cc8e9a9 commit 17eefea
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 14 deletions.
11 changes: 6 additions & 5 deletions definition.go
Expand Up @@ -582,6 +582,7 @@ type FieldResolveFn func(p ResolveParams) (interface{}, error)
type ResolveInfo struct {
FieldName string
FieldASTs []*ast.Field
Path *ResponsePath
ReturnType Output
ParentType Composite
Schema Schema
Expand Down Expand Up @@ -1289,21 +1290,21 @@ func assertValidName(name string) error {

}

type responsePath struct {
Prev *responsePath
type ResponsePath struct {
Prev *ResponsePath
Key interface{}
}

// WithKey returns a new responsePath containing the new key.
func (p *responsePath) WithKey(key interface{}) *responsePath {
return &responsePath{
func (p *ResponsePath) WithKey(key interface{}) *ResponsePath {
return &ResponsePath{
Prev: p,
Key: key,
}
}

// AsArray returns an array of path keys.
func (p *responsePath) AsArray() []interface{} {
func (p *ResponsePath) AsArray() []interface{} {
if p == nil {
return nil
}
Expand Down
19 changes: 10 additions & 9 deletions executor.go
Expand Up @@ -224,7 +224,7 @@ type executeFieldsParams struct {
ParentType *Object
Source interface{}
Fields map[string][]*ast.Field
Path *responsePath
Path *ResponsePath
}

// Implements the "Evaluating selection sets" section of the spec for "write" mode.
Expand Down Expand Up @@ -559,7 +559,7 @@ type resolveFieldResultState struct {
hasNoFieldDefs bool
}

func handleFieldError(r interface{}, fieldNodes []ast.Node, path *responsePath, returnType Output, eCtx *executionContext) {
func handleFieldError(r interface{}, fieldNodes []ast.Node, path *ResponsePath, returnType Output, eCtx *executionContext) {
err := NewLocatedErrorWithPath(r, fieldNodes, path.AsArray())
// send panic upstream
if _, ok := returnType.(*NonNull); ok {
Expand All @@ -572,7 +572,7 @@ func handleFieldError(r interface{}, fieldNodes []ast.Node, path *responsePath,
// figures out the value that the field returns by calling its resolve function,
// then calls completeValue to complete promises, serialize scalars, or execute
// the sub-selection-set for objects.
func resolveField(eCtx *executionContext, parentType *Object, source interface{}, fieldASTs []*ast.Field, path *responsePath) (result interface{}, resultState resolveFieldResultState) {
func resolveField(eCtx *executionContext, parentType *Object, source interface{}, fieldASTs []*ast.Field, path *ResponsePath) (result interface{}, resultState resolveFieldResultState) {
// catch panic from resolveFn
var returnType Output
defer func() (interface{}, resolveFieldResultState) {
Expand Down Expand Up @@ -608,6 +608,7 @@ func resolveField(eCtx *executionContext, parentType *Object, source interface{}
info := ResolveInfo{
FieldName: fieldName,
FieldASTs: fieldASTs,
Path: path,
ReturnType: returnType,
ParentType: parentType,
Schema: eCtx.Schema,
Expand All @@ -634,7 +635,7 @@ func resolveField(eCtx *executionContext, parentType *Object, source interface{}
return completed, resultState
}

func completeValueCatchingError(eCtx *executionContext, returnType Type, fieldASTs []*ast.Field, info ResolveInfo, path *responsePath, result interface{}) (completed interface{}) {
func completeValueCatchingError(eCtx *executionContext, returnType Type, fieldASTs []*ast.Field, info ResolveInfo, path *ResponsePath, result interface{}) (completed interface{}) {
// catch panic
defer func() interface{} {
if r := recover(); r != nil {
Expand All @@ -652,7 +653,7 @@ func completeValueCatchingError(eCtx *executionContext, returnType Type, fieldAS
return completed
}

func completeValue(eCtx *executionContext, returnType Type, fieldASTs []*ast.Field, info ResolveInfo, path *responsePath, result interface{}) interface{} {
func completeValue(eCtx *executionContext, returnType Type, fieldASTs []*ast.Field, info ResolveInfo, path *ResponsePath, result interface{}) interface{} {

resultVal := reflect.ValueOf(result)
if resultVal.IsValid() && resultVal.Kind() == reflect.Func {
Expand Down Expand Up @@ -719,7 +720,7 @@ func completeValue(eCtx *executionContext, returnType Type, fieldASTs []*ast.Fie
return nil
}

func completeThunkValueCatchingError(eCtx *executionContext, returnType Type, fieldASTs []*ast.Field, info ResolveInfo, path *responsePath, result interface{}) (completed interface{}) {
func completeThunkValueCatchingError(eCtx *executionContext, returnType Type, fieldASTs []*ast.Field, info ResolveInfo, path *ResponsePath, result interface{}) (completed interface{}) {

// catch any panic invoked from the propertyFn (thunk)
defer func() {
Expand Down Expand Up @@ -751,7 +752,7 @@ func completeThunkValueCatchingError(eCtx *executionContext, returnType Type, fi

// completeAbstractValue completes value of an Abstract type (Union / Interface) by determining the runtime type
// of that value, then completing based on that type.
func completeAbstractValue(eCtx *executionContext, returnType Abstract, fieldASTs []*ast.Field, info ResolveInfo, path *responsePath, result interface{}) interface{} {
func completeAbstractValue(eCtx *executionContext, returnType Abstract, fieldASTs []*ast.Field, info ResolveInfo, path *ResponsePath, result interface{}) interface{} {

var runtimeType *Object

Expand Down Expand Up @@ -788,7 +789,7 @@ func completeAbstractValue(eCtx *executionContext, returnType Abstract, fieldAST
}

// completeObjectValue complete an Object value by executing all sub-selections.
func completeObjectValue(eCtx *executionContext, returnType *Object, fieldASTs []*ast.Field, info ResolveInfo, path *responsePath, result interface{}) interface{} {
func completeObjectValue(eCtx *executionContext, returnType *Object, fieldASTs []*ast.Field, info ResolveInfo, path *ResponsePath, result interface{}) interface{} {

// If there is an isTypeOf predicate function, call it with the
// current result. If isTypeOf returns false, then raise an error rather
Expand Down Expand Up @@ -845,7 +846,7 @@ func completeLeafValue(returnType Leaf, result interface{}) interface{} {
}

// completeListValue complete a list value by completing each item in the list with the inner type
func completeListValue(eCtx *executionContext, returnType *List, fieldASTs []*ast.Field, info ResolveInfo, path *responsePath, result interface{}) interface{} {
func completeListValue(eCtx *executionContext, returnType *List, fieldASTs []*ast.Field, info ResolveInfo, path *ResponsePath, result interface{}) interface{} {
resultVal := reflect.ValueOf(result)
if resultVal.Kind() == reflect.Ptr {
resultVal = resultVal.Elem()
Expand Down

0 comments on commit 17eefea

Please sign in to comment.