-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
320 additions
and
297 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package graphql | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
"github.com/vektah/gqlparser/ast" | ||
) | ||
|
||
func TestGetRequestContext(t *testing.T) { | ||
require.Nil(t, GetRequestContext(context.Background())) | ||
|
||
rc := &RequestContext{} | ||
require.Equal(t, rc, GetRequestContext(WithRequestContext(context.Background(), rc))) | ||
} | ||
|
||
func TestCollectAllFields(t *testing.T) { | ||
t.Run("collect fields", func(t *testing.T) { | ||
ctx := testContext(ast.SelectionSet{ | ||
&ast.Field{ | ||
Name: "field", | ||
}, | ||
}) | ||
s := CollectAllFields(ctx) | ||
require.Equal(t, []string{"field"}, s) | ||
}) | ||
|
||
t.Run("unique field names", func(t *testing.T) { | ||
ctx := testContext(ast.SelectionSet{ | ||
&ast.Field{ | ||
Name: "field", | ||
}, | ||
&ast.Field{ | ||
Name: "field", | ||
Alias: "field alias", | ||
}, | ||
}) | ||
s := CollectAllFields(ctx) | ||
require.Equal(t, []string{"field"}, s) | ||
}) | ||
|
||
t.Run("collect fragments", func(t *testing.T) { | ||
ctx := testContext(ast.SelectionSet{ | ||
&ast.Field{ | ||
Name: "fieldA", | ||
}, | ||
&ast.InlineFragment{ | ||
TypeCondition: "ExampleTypeA", | ||
SelectionSet: ast.SelectionSet{ | ||
&ast.Field{ | ||
Name: "fieldA", | ||
}, | ||
}, | ||
}, | ||
&ast.InlineFragment{ | ||
TypeCondition: "ExampleTypeB", | ||
SelectionSet: ast.SelectionSet{ | ||
&ast.Field{ | ||
Name: "fieldB", | ||
}, | ||
}, | ||
}, | ||
}) | ||
s := CollectAllFields(ctx) | ||
require.Equal(t, []string{"fieldA", "fieldB"}, s) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package graphql | ||
|
||
import ( | ||
"context" | ||
) | ||
|
||
func DefaultResolverMiddleware(ctx context.Context, next Resolver) (res interface{}, err error) { | ||
return next(ctx) | ||
} | ||
|
||
func DefaultDirectiveMiddleware(ctx context.Context, next Resolver) (res interface{}, err error) { | ||
return next(ctx) | ||
} | ||
|
||
type key string | ||
|
||
const resolverCtx key = "resolver_context" | ||
|
||
type ResolverContext struct { | ||
Parent *ResolverContext | ||
// The name of the type this field belongs to | ||
Object string | ||
// These are the args after processing, they can be mutated in middleware to change what the resolver will get. | ||
Args map[string]interface{} | ||
// The raw field | ||
Field CollectedField | ||
// The index of array in path. | ||
Index *int | ||
// The result object of resolver | ||
Result interface{} | ||
// IsMethod indicates if the resolver is a method | ||
IsMethod bool | ||
} | ||
|
||
func (r *ResolverContext) Path() []interface{} { | ||
var path []interface{} | ||
for it := r; it != nil; it = it.Parent { | ||
if it.Index != nil { | ||
path = append(path, *it.Index) | ||
} else if it.Field.Field != nil { | ||
path = append(path, it.Field.Alias) | ||
} | ||
} | ||
|
||
// because we are walking up the chain, all the elements are backwards, do an inplace flip. | ||
for i := len(path)/2 - 1; i >= 0; i-- { | ||
opp := len(path) - 1 - i | ||
path[i], path[opp] = path[opp], path[i] | ||
} | ||
|
||
return path | ||
} | ||
|
||
func GetResolverContext(ctx context.Context) *ResolverContext { | ||
if val, ok := ctx.Value(resolverCtx).(*ResolverContext); ok { | ||
return val | ||
} | ||
return nil | ||
} | ||
|
||
func WithResolverContext(ctx context.Context, rc *ResolverContext) context.Context { | ||
rc.Parent = GetResolverContext(ctx) | ||
return context.WithValue(ctx, resolverCtx, rc) | ||
} | ||
|
||
func equalPath(a []interface{}, b []interface{}) bool { | ||
if len(a) != len(b) { | ||
return false | ||
} | ||
|
||
for i := 0; i < len(a); i++ { | ||
if a[i] != b[i] { | ||
return false | ||
} | ||
} | ||
|
||
return true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package graphql | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
"github.com/vektah/gqlparser/ast" | ||
) | ||
|
||
func TestGetResolverContext(t *testing.T) { | ||
require.Nil(t, GetResolverContext(context.Background())) | ||
|
||
rc := &ResolverContext{} | ||
require.Equal(t, rc, GetResolverContext(WithResolverContext(context.Background(), rc))) | ||
} | ||
|
||
func testContext(sel ast.SelectionSet) context.Context { | ||
|
||
ctx := context.Background() | ||
|
||
rqCtx := &RequestContext{} | ||
ctx = WithRequestContext(ctx, rqCtx) | ||
|
||
root := &ResolverContext{ | ||
Field: CollectedField{ | ||
Selections: sel, | ||
}, | ||
} | ||
ctx = WithResolverContext(ctx, root) | ||
|
||
return ctx | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.