diff --git a/executor/abstract_test.go b/executor/abstract_test.go index f272a3e6..a1f6ba78 100644 --- a/executor/abstract_test.go +++ b/executor/abstract_test.go @@ -6,6 +6,7 @@ import ( "github.com/chris-ramon/graphql-go/language/location" "github.com/chris-ramon/graphql-go/testutil" "github.com/chris-ramon/graphql-go/types" + "golang.org/x/net/context" "reflect" "testing" ) @@ -48,7 +49,7 @@ func TestIsTypeOfUsedToResolveRuntimeTypeForInterface(t *testing.T) { Fields: types.GraphQLFieldConfigMap{ "name": &types.GraphQLFieldConfig{ Type: types.GraphQLString, - Resolve: func(p types.GQLFRParams) interface{} { + Resolve: func(ctx context.Context, p types.GQLFRParams) interface{} { if dog, ok := p.Source.(*testDog); ok { return dog.Name } @@ -57,7 +58,7 @@ func TestIsTypeOfUsedToResolveRuntimeTypeForInterface(t *testing.T) { }, "woofs": &types.GraphQLFieldConfig{ Type: types.GraphQLBoolean, - Resolve: func(p types.GQLFRParams) interface{} { + Resolve: func(ctx context.Context, p types.GQLFRParams) interface{} { if dog, ok := p.Source.(*testDog); ok { return dog.Woofs } @@ -79,7 +80,7 @@ func TestIsTypeOfUsedToResolveRuntimeTypeForInterface(t *testing.T) { Fields: types.GraphQLFieldConfigMap{ "name": &types.GraphQLFieldConfig{ Type: types.GraphQLString, - Resolve: func(p types.GQLFRParams) interface{} { + Resolve: func(ctx context.Context, p types.GQLFRParams) interface{} { if cat, ok := p.Source.(*testCat); ok { return cat.Name } @@ -88,7 +89,7 @@ func TestIsTypeOfUsedToResolveRuntimeTypeForInterface(t *testing.T) { }, "meows": &types.GraphQLFieldConfig{ Type: types.GraphQLBoolean, - Resolve: func(p types.GQLFRParams) interface{} { + Resolve: func(ctx context.Context, p types.GQLFRParams) interface{} { if cat, ok := p.Source.(*testCat); ok { return cat.Meows } @@ -103,7 +104,7 @@ func TestIsTypeOfUsedToResolveRuntimeTypeForInterface(t *testing.T) { Fields: types.GraphQLFieldConfigMap{ "pets": &types.GraphQLFieldConfig{ Type: types.NewGraphQLList(petType), - Resolve: func(p types.GQLFRParams) interface{} { + Resolve: func(ctx context.Context, p types.GQLFRParams) interface{} { return []interface{}{ &testDog{"Odie", true}, &testCat{"Garfield", false}, @@ -171,7 +172,7 @@ func TestIsTypeOfUsedToResolveRuntimeTypeForUnion(t *testing.T) { Fields: types.GraphQLFieldConfigMap{ "name": &types.GraphQLFieldConfig{ Type: types.GraphQLString, - Resolve: func(p types.GQLFRParams) interface{} { + Resolve: func(ctx context.Context, p types.GQLFRParams) interface{} { if dog, ok := p.Source.(*testDog); ok { return dog.Name } @@ -180,7 +181,7 @@ func TestIsTypeOfUsedToResolveRuntimeTypeForUnion(t *testing.T) { }, "woofs": &types.GraphQLFieldConfig{ Type: types.GraphQLBoolean, - Resolve: func(p types.GQLFRParams) interface{} { + Resolve: func(ctx context.Context, p types.GQLFRParams) interface{} { if dog, ok := p.Source.(*testDog); ok { return dog.Woofs } @@ -198,7 +199,7 @@ func TestIsTypeOfUsedToResolveRuntimeTypeForUnion(t *testing.T) { Fields: types.GraphQLFieldConfigMap{ "name": &types.GraphQLFieldConfig{ Type: types.GraphQLString, - Resolve: func(p types.GQLFRParams) interface{} { + Resolve: func(ctx context.Context, p types.GQLFRParams) interface{} { if cat, ok := p.Source.(*testCat); ok { return cat.Name } @@ -207,7 +208,7 @@ func TestIsTypeOfUsedToResolveRuntimeTypeForUnion(t *testing.T) { }, "meows": &types.GraphQLFieldConfig{ Type: types.GraphQLBoolean, - Resolve: func(p types.GQLFRParams) interface{} { + Resolve: func(ctx context.Context, p types.GQLFRParams) interface{} { if cat, ok := p.Source.(*testCat); ok { return cat.Meows } @@ -238,7 +239,7 @@ func TestIsTypeOfUsedToResolveRuntimeTypeForUnion(t *testing.T) { Fields: types.GraphQLFieldConfigMap{ "pets": &types.GraphQLFieldConfig{ Type: types.NewGraphQLList(petType), - Resolve: func(p types.GQLFRParams) interface{} { + Resolve: func(ctx context.Context, p types.GQLFRParams) interface{} { return []interface{}{ &testDog{"Odie", true}, &testCat{"Garfield", false}, @@ -327,7 +328,7 @@ func TestResolveTypeOnInterfaceYieldsUsefulError(t *testing.T) { Fields: types.GraphQLFieldConfigMap{ "name": &types.GraphQLFieldConfig{ Type: types.GraphQLString, - Resolve: func(p types.GQLFRParams) interface{} { + Resolve: func(ctx context.Context, p types.GQLFRParams) interface{} { if human, ok := p.Source.(*testHuman); ok { return human.Name } @@ -348,7 +349,7 @@ func TestResolveTypeOnInterfaceYieldsUsefulError(t *testing.T) { Fields: types.GraphQLFieldConfigMap{ "name": &types.GraphQLFieldConfig{ Type: types.GraphQLString, - Resolve: func(p types.GQLFRParams) interface{} { + Resolve: func(ctx context.Context, p types.GQLFRParams) interface{} { if dog, ok := p.Source.(*testDog); ok { return dog.Name } @@ -357,7 +358,7 @@ func TestResolveTypeOnInterfaceYieldsUsefulError(t *testing.T) { }, "woofs": &types.GraphQLFieldConfig{ Type: types.GraphQLBoolean, - Resolve: func(p types.GQLFRParams) interface{} { + Resolve: func(ctx context.Context, p types.GQLFRParams) interface{} { if dog, ok := p.Source.(*testDog); ok { return dog.Woofs } @@ -378,7 +379,7 @@ func TestResolveTypeOnInterfaceYieldsUsefulError(t *testing.T) { Fields: types.GraphQLFieldConfigMap{ "name": &types.GraphQLFieldConfig{ Type: types.GraphQLString, - Resolve: func(p types.GQLFRParams) interface{} { + Resolve: func(ctx context.Context, p types.GQLFRParams) interface{} { if cat, ok := p.Source.(*testCat); ok { return cat.Name } @@ -387,7 +388,7 @@ func TestResolveTypeOnInterfaceYieldsUsefulError(t *testing.T) { }, "meows": &types.GraphQLFieldConfig{ Type: types.GraphQLBoolean, - Resolve: func(p types.GQLFRParams) interface{} { + Resolve: func(ctx context.Context, p types.GQLFRParams) interface{} { if cat, ok := p.Source.(*testCat); ok { return cat.Meows } @@ -402,7 +403,7 @@ func TestResolveTypeOnInterfaceYieldsUsefulError(t *testing.T) { Fields: types.GraphQLFieldConfigMap{ "pets": &types.GraphQLFieldConfig{ Type: types.NewGraphQLList(petType), - Resolve: func(p types.GQLFRParams) interface{} { + Resolve: func(ctx context.Context, p types.GQLFRParams) interface{} { return []interface{}{ &testDog{"Odie", true}, &testCat{"Garfield", false}, @@ -473,7 +474,7 @@ func TestResolveTypeOnUnionYieldsUsefulError(t *testing.T) { Fields: types.GraphQLFieldConfigMap{ "name": &types.GraphQLFieldConfig{ Type: types.GraphQLString, - Resolve: func(p types.GQLFRParams) interface{} { + Resolve: func(ctx context.Context, p types.GQLFRParams) interface{} { if human, ok := p.Source.(*testHuman); ok { return human.Name } @@ -491,7 +492,7 @@ func TestResolveTypeOnUnionYieldsUsefulError(t *testing.T) { Fields: types.GraphQLFieldConfigMap{ "name": &types.GraphQLFieldConfig{ Type: types.GraphQLString, - Resolve: func(p types.GQLFRParams) interface{} { + Resolve: func(ctx context.Context, p types.GQLFRParams) interface{} { if dog, ok := p.Source.(*testDog); ok { return dog.Name } @@ -500,7 +501,7 @@ func TestResolveTypeOnUnionYieldsUsefulError(t *testing.T) { }, "woofs": &types.GraphQLFieldConfig{ Type: types.GraphQLBoolean, - Resolve: func(p types.GQLFRParams) interface{} { + Resolve: func(ctx context.Context, p types.GQLFRParams) interface{} { if dog, ok := p.Source.(*testDog); ok { return dog.Woofs } @@ -518,7 +519,7 @@ func TestResolveTypeOnUnionYieldsUsefulError(t *testing.T) { Fields: types.GraphQLFieldConfigMap{ "name": &types.GraphQLFieldConfig{ Type: types.GraphQLString, - Resolve: func(p types.GQLFRParams) interface{} { + Resolve: func(ctx context.Context, p types.GQLFRParams) interface{} { if cat, ok := p.Source.(*testCat); ok { return cat.Name } @@ -527,7 +528,7 @@ func TestResolveTypeOnUnionYieldsUsefulError(t *testing.T) { }, "meows": &types.GraphQLFieldConfig{ Type: types.GraphQLBoolean, - Resolve: func(p types.GQLFRParams) interface{} { + Resolve: func(ctx context.Context, p types.GQLFRParams) interface{} { if cat, ok := p.Source.(*testCat); ok { return cat.Meows } @@ -560,7 +561,7 @@ func TestResolveTypeOnUnionYieldsUsefulError(t *testing.T) { Fields: types.GraphQLFieldConfigMap{ "pets": &types.GraphQLFieldConfig{ Type: types.NewGraphQLList(petType), - Resolve: func(p types.GQLFRParams) interface{} { + Resolve: func(ctx context.Context, p types.GQLFRParams) interface{} { return []interface{}{ &testDog{"Odie", true}, &testCat{"Garfield", false}, diff --git a/executor/executor.go b/executor/executor.go index 9d706932..e3580dac 100644 --- a/executor/executor.go +++ b/executor/executor.go @@ -6,6 +6,7 @@ import ( "github.com/chris-ramon/graphql-go/errors" "github.com/chris-ramon/graphql-go/language/ast" "github.com/chris-ramon/graphql-go/types" + "golang.org/x/net/context" "reflect" "strings" ) @@ -13,6 +14,7 @@ import ( type ExecuteParams struct { Schema types.GraphQLSchema Root interface{} + Ctx context.Context AST *ast.Document OperationName string Args map[string]interface{} @@ -25,6 +27,7 @@ func Execute(p ExecuteParams, resultChan chan *types.GraphQLResult) { Schema: p.Schema, Root: p.Root, AST: p.AST, + Ctx: p.Ctx, OperationName: p.OperationName, Args: p.Args, Errors: errors, @@ -58,6 +61,7 @@ type BuildExecutionCtxParams struct { Schema types.GraphQLSchema Root interface{} AST *ast.Document + Ctx context.Context OperationName string Args map[string]interface{} Errors []graphqlerrors.GraphQLFormattedError @@ -66,6 +70,7 @@ type BuildExecutionCtxParams struct { } type ExecutionContext struct { Schema types.GraphQLSchema + Ctx context.Context Fragments map[string]ast.Definition Root interface{} Operation ast.Definition @@ -129,6 +134,7 @@ func buildExecutionContext(p BuildExecutionCtxParams) *ExecutionContext { } eCtx.Schema = p.Schema + eCtx.Ctx = p.Ctx eCtx.Fragments = fragments eCtx.Root = p.Root eCtx.Operation = operation @@ -516,7 +522,7 @@ func resolveField(eCtx *ExecutionContext, parentType *types.GraphQLObjectType, s // it is wrapped as a GraphQLError with locations. Log this error and return // null if allowed, otherwise throw the error so the parent field can handle // it. - result = resolveFn(types.GQLFRParams{ + result = resolveFn(eCtx.Ctx, types.GQLFRParams{ Source: source, Args: args, Info: info, @@ -692,7 +698,7 @@ func completeValue(eCtx *ExecutionContext, returnType types.GraphQLType, fieldAS } -func defaultResolveFn(p types.GQLFRParams) interface{} { +func defaultResolveFn(ctx context.Context, p types.GQLFRParams) interface{} { // try to resolve p.Source as a struct first sourceVal := reflect.ValueOf(p.Source) if sourceVal.IsValid() && sourceVal.Type().Kind() == reflect.Ptr { diff --git a/executor/executor_schema_test.go b/executor/executor_schema_test.go index d2f23c09..98be2776 100644 --- a/executor/executor_schema_test.go +++ b/executor/executor_schema_test.go @@ -5,6 +5,7 @@ import ( "github.com/chris-ramon/graphql-go/executor" "github.com/chris-ramon/graphql-go/testutil" "github.com/chris-ramon/graphql-go/types" + "golang.org/x/net/context" "reflect" "testing" ) @@ -106,7 +107,7 @@ func TestExecutesUsingAComplexSchema(t *testing.T) { Type: types.GraphQLInt, }, }, - Resolve: func(p types.GQLFRParams) interface{} { + Resolve: func(ctx context.Context, p types.GQLFRParams) interface{} { if author, ok := p.Source.(*testAuthor); ok { width := fmt.Sprintf("%v", p.Args["width"]) height := fmt.Sprintf("%v", p.Args["height"]) @@ -156,14 +157,14 @@ func TestExecutesUsingAComplexSchema(t *testing.T) { Type: types.GraphQLID, }, }, - Resolve: func(p types.GQLFRParams) interface{} { + Resolve: func(ctx context.Context, p types.GQLFRParams) interface{} { id := p.Args["id"] return article(id) }, }, "feed": &types.GraphQLFieldConfig{ Type: types.NewGraphQLList(blogArticle), - Resolve: func(p types.GQLFRParams) interface{} { + Resolve: func(ctx context.Context, p types.GQLFRParams) interface{} { return []*testArticle{ article(1), article(2), diff --git a/executor/executor_test.go b/executor/executor_test.go index 0e414c00..cb91e1db 100644 --- a/executor/executor_test.go +++ b/executor/executor_test.go @@ -8,6 +8,7 @@ import ( "github.com/chris-ramon/graphql-go/language/location" "github.com/chris-ramon/graphql-go/testutil" "github.com/chris-ramon/graphql-go/types" + "golang.org/x/net/context" "reflect" "testing" ) @@ -103,7 +104,7 @@ func TestExecutesArbitraryCode(t *testing.T) { } // Schema Definitions - picResolverFn := func(p types.GQLFRParams) interface{} { + picResolverFn := func(ctx context.Context, p types.GQLFRParams) interface{} { // get and type assert ResolveFn for this field picResolver, ok := p.Source.(map[string]interface{})["pic"].(func(size int) string) if !ok { @@ -244,19 +245,19 @@ func TestMergesParallelFragments(t *testing.T) { Fields: types.GraphQLFieldConfigMap{ "a": &types.GraphQLFieldConfig{ Type: types.GraphQLString, - Resolve: func(p types.GQLFRParams) interface{} { + Resolve: func(ctx context.Context, p types.GQLFRParams) interface{} { return "Apple" }, }, "b": &types.GraphQLFieldConfig{ Type: types.GraphQLString, - Resolve: func(p types.GQLFRParams) interface{} { + Resolve: func(ctx context.Context, p types.GQLFRParams) interface{} { return "Banana" }, }, "c": &types.GraphQLFieldConfig{ Type: types.GraphQLString, - Resolve: func(p types.GQLFRParams) interface{} { + Resolve: func(ctx context.Context, p types.GQLFRParams) interface{} { return "Cherry" }, }, @@ -264,7 +265,7 @@ func TestMergesParallelFragments(t *testing.T) { }) deepTypeFieldConfig := &types.GraphQLFieldConfig{ Type: typeObjectType, - Resolve: func(p types.GQLFRParams) interface{} { + Resolve: func(ctx context.Context, p types.GQLFRParams) interface{} { return p.Source }, } @@ -312,7 +313,7 @@ func TestThreadsContextCorrectly(t *testing.T) { Fields: types.GraphQLFieldConfigMap{ "a": &types.GraphQLFieldConfig{ Type: types.GraphQLString, - Resolve: func(p types.GQLFRParams) interface{} { + Resolve: func(ctx context.Context, p types.GQLFRParams) interface{} { resolvedContext = p.Source.(map[string]interface{}) return resolvedContext }, @@ -368,7 +369,7 @@ func TestCorrectlyThreadsArguments(t *testing.T) { }, }, Type: types.GraphQLString, - Resolve: func(p types.GQLFRParams) interface{} { + Resolve: func(ctx context.Context, p types.GQLFRParams) interface{} { resolvedArgs = p.Args return resolvedArgs }, @@ -944,7 +945,7 @@ func TestDoesNotIncludeArgumentsThatWereNotSet(t *testing.T) { Type: types.GraphQLInt, }, }, - Resolve: func(p types.GQLFRParams) interface{} { + Resolve: func(ctx context.Context, p types.GQLFRParams) interface{} { args, _ := json.Marshal(p.Args) return string(args) }, @@ -1019,7 +1020,7 @@ func TestFailsWhenAnIsTypeOfCheckIsNotMet(t *testing.T) { Fields: types.GraphQLFieldConfigMap{ "value": &types.GraphQLFieldConfig{ Type: types.GraphQLString, - Resolve: func(p types.GQLFRParams) interface{} { + Resolve: func(ctx context.Context, p types.GQLFRParams) interface{} { return p.Source.(testSpecialType).Value }, }, @@ -1031,7 +1032,7 @@ func TestFailsWhenAnIsTypeOfCheckIsNotMet(t *testing.T) { Fields: types.GraphQLFieldConfigMap{ "specials": &types.GraphQLFieldConfig{ Type: types.NewGraphQLList(specialType), - Resolve: func(p types.GQLFRParams) interface{} { + Resolve: func(ctx context.Context, p types.GQLFRParams) interface{} { return p.Source.(map[string]interface{})["specials"] }, }, diff --git a/executor/lists_test.go b/executor/lists_test.go index 6a51318c..d64ceee6 100644 --- a/executor/lists_test.go +++ b/executor/lists_test.go @@ -6,6 +6,7 @@ import ( "github.com/chris-ramon/graphql-go/language/location" "github.com/chris-ramon/graphql-go/testutil" "github.com/chris-ramon/graphql-go/types" + "golang.org/x/net/context" "reflect" "testing" ) @@ -25,7 +26,7 @@ func checkList(t *testing.T, testType types.GraphQLType, testData interface{}, e }) dataType.AddFieldConfig("nest", &types.GraphQLFieldConfig{ Type: dataType, - Resolve: func(p types.GQLFRParams) interface{} { + Resolve: func(ctx context.Context, p types.GQLFRParams) interface{} { return data }, }) diff --git a/executor/mutations_test.go b/executor/mutations_test.go index 1564241d..5aef7058 100644 --- a/executor/mutations_test.go +++ b/executor/mutations_test.go @@ -6,6 +6,7 @@ import ( "github.com/chris-ramon/graphql-go/language/location" "github.com/chris-ramon/graphql-go/testutil" "github.com/chris-ramon/graphql-go/types" + "golang.org/x/net/context" "reflect" "testing" ) @@ -66,7 +67,7 @@ var mutationsTestSchema, _ = types.NewGraphQLSchema(types.GraphQLSchemaConfig{ Type: types.GraphQLInt, }, }, - Resolve: func(p types.GQLFRParams) interface{} { + Resolve: func(ctx context.Context, p types.GQLFRParams) interface{} { newNumber := 0 obj, _ := p.Source.(*testRoot) newNumber, _ = p.Args["newNumber"].(int) @@ -80,7 +81,7 @@ var mutationsTestSchema, _ = types.NewGraphQLSchema(types.GraphQLSchemaConfig{ Type: types.GraphQLInt, }, }, - Resolve: func(p types.GQLFRParams) interface{} { + Resolve: func(ctx context.Context, p types.GQLFRParams) interface{} { newNumber := 0 obj, _ := p.Source.(*testRoot) newNumber, _ = p.Args["newNumber"].(int) @@ -94,7 +95,7 @@ var mutationsTestSchema, _ = types.NewGraphQLSchema(types.GraphQLSchemaConfig{ Type: types.GraphQLInt, }, }, - Resolve: func(p types.GQLFRParams) interface{} { + Resolve: func(ctx context.Context, p types.GQLFRParams) interface{} { newNumber := 0 obj, _ := p.Source.(*testRoot) newNumber, _ = p.Args["newNumber"].(int) @@ -108,7 +109,7 @@ var mutationsTestSchema, _ = types.NewGraphQLSchema(types.GraphQLSchemaConfig{ Type: types.GraphQLInt, }, }, - Resolve: func(p types.GQLFRParams) interface{} { + Resolve: func(ctx context.Context, p types.GQLFRParams) interface{} { newNumber := 0 obj, _ := p.Source.(*testRoot) newNumber, _ = p.Args["newNumber"].(int) diff --git a/executor/variables_test.go b/executor/variables_test.go index 843242b5..7772be78 100644 --- a/executor/variables_test.go +++ b/executor/variables_test.go @@ -8,6 +8,7 @@ import ( "github.com/chris-ramon/graphql-go/language/location" "github.com/chris-ramon/graphql-go/testutil" "github.com/chris-ramon/graphql-go/types" + "golang.org/x/net/context" "reflect" "testing" ) @@ -53,7 +54,7 @@ var testInputObject *types.GraphQLInputObjectType = types.NewGraphQLInputObjectT }, }) -func inputResolved(p types.GQLFRParams) interface{} { +func inputResolved(ctx context.Context, p types.GQLFRParams) interface{} { input, ok := p.Args["input"] if !ok { return nil diff --git a/graphql.go b/graphql.go index 1ab19e4d..c8c32801 100644 --- a/graphql.go +++ b/graphql.go @@ -7,11 +7,13 @@ import ( "github.com/chris-ramon/graphql-go/language/source" "github.com/chris-ramon/graphql-go/types" "github.com/chris-ramon/graphql-go/validator" + "golang.org/x/net/context" ) type GraphqlParams struct { Schema types.GraphQLSchema RequestString string + Ctx context.Context RootObject map[string]interface{} VariableValues map[string]interface{} OperationName string @@ -43,6 +45,7 @@ func Graphql(p GraphqlParams, resultChannel chan *types.GraphQLResult) { Schema: p.Schema, Root: p.RootObject, AST: AST, + Ctx: p.Ctx, OperationName: p.OperationName, Args: p.VariableValues, } diff --git a/graphql_test.go b/graphql_test.go index a4579ab6..a0a1fe0f 100644 --- a/graphql_test.go +++ b/graphql_test.go @@ -5,6 +5,7 @@ import ( "testing" "github.com/chris-ramon/graphql-go/types" + "golang.org/x/net/context" "./testutil" ) @@ -95,7 +96,7 @@ func testGraphql(test T, p GraphqlParams, t *testing.T) { func TestBasicGraphQLExample(t *testing.T) { // taken from `graphql-js` README - helloFieldResolved := func(p types.GQLFRParams) interface{} { + helloFieldResolved := func(ctx context.Context, p types.GQLFRParams) interface{} { return "world" } diff --git a/testutil/testutil.go b/testutil/testutil.go index 0b4615a5..5d25f30f 100644 --- a/testutil/testutil.go +++ b/testutil/testutil.go @@ -10,6 +10,7 @@ import ( "github.com/chris-ramon/graphql-go/language/ast" "github.com/chris-ramon/graphql-go/language/parser" "github.com/chris-ramon/graphql-go/types" + "golang.org/x/net/context" "github.com/kr/pretty" ) @@ -157,7 +158,7 @@ func init() { "id": &types.GraphQLFieldConfig{ Type: types.NewGraphQLNonNull(types.GraphQLString), Description: "The id of the human.", - Resolve: func(p types.GQLFRParams) interface{} { + Resolve: func(ctx context.Context, p types.GQLFRParams) interface{} { if human, ok := p.Source.(StarWarsChar); ok { return human.Id } @@ -167,7 +168,7 @@ func init() { "name": &types.GraphQLFieldConfig{ Type: types.GraphQLString, Description: "The name of the human.", - Resolve: func(p types.GQLFRParams) interface{} { + Resolve: func(ctx context.Context, p types.GQLFRParams) interface{} { if human, ok := p.Source.(StarWarsChar); ok { return human.Name } @@ -177,7 +178,7 @@ func init() { "friends": &types.GraphQLFieldConfig{ Type: types.NewGraphQLList(characterInterface), Description: "The friends of the human, or an empty list if they have none.", - Resolve: func(p types.GQLFRParams) interface{} { + Resolve: func(ctx context.Context, p types.GQLFRParams) interface{} { if human, ok := p.Source.(StarWarsChar); ok { return human.Friends } @@ -187,7 +188,7 @@ func init() { "appearsIn": &types.GraphQLFieldConfig{ Type: types.NewGraphQLList(episodeEnum), Description: "Which movies they appear in.", - Resolve: func(p types.GQLFRParams) interface{} { + Resolve: func(ctx context.Context, p types.GQLFRParams) interface{} { if human, ok := p.Source.(StarWarsChar); ok { return human.AppearsIn } @@ -197,7 +198,7 @@ func init() { "homePlanet": &types.GraphQLFieldConfig{ Type: types.GraphQLString, Description: "The home planet of the human, or null if unknown.", - Resolve: func(p types.GQLFRParams) interface{} { + Resolve: func(ctx context.Context, p types.GQLFRParams) interface{} { if human, ok := p.Source.(StarWarsChar); ok { return human.HomePlanet } @@ -216,7 +217,7 @@ func init() { "id": &types.GraphQLFieldConfig{ Type: types.NewGraphQLNonNull(types.GraphQLString), Description: "The id of the droid.", - Resolve: func(p types.GQLFRParams) interface{} { + Resolve: func(ctx context.Context, p types.GQLFRParams) interface{} { if droid, ok := p.Source.(StarWarsChar); ok { return droid.Id } @@ -226,7 +227,7 @@ func init() { "name": &types.GraphQLFieldConfig{ Type: types.GraphQLString, Description: "The name of the droid.", - Resolve: func(p types.GQLFRParams) interface{} { + Resolve: func(ctx context.Context, p types.GQLFRParams) interface{} { if droid, ok := p.Source.(StarWarsChar); ok { return droid.Name } @@ -236,7 +237,7 @@ func init() { "friends": &types.GraphQLFieldConfig{ Type: types.NewGraphQLList(characterInterface), Description: "The friends of the droid, or an empty list if they have none.", - Resolve: func(p types.GQLFRParams) interface{} { + Resolve: func(ctx context.Context, p types.GQLFRParams) interface{} { if droid, ok := p.Source.(StarWarsChar); ok { friends := []map[string]interface{}{} for _, friend := range droid.Friends { @@ -253,7 +254,7 @@ func init() { "appearsIn": &types.GraphQLFieldConfig{ Type: types.NewGraphQLList(episodeEnum), Description: "Which movies they appear in.", - Resolve: func(p types.GQLFRParams) interface{} { + Resolve: func(ctx context.Context, p types.GQLFRParams) interface{} { if droid, ok := p.Source.(StarWarsChar); ok { return droid.AppearsIn } @@ -263,7 +264,7 @@ func init() { "primaryFunction": &types.GraphQLFieldConfig{ Type: types.GraphQLString, Description: "The primary function of the droid.", - Resolve: func(p types.GQLFRParams) interface{} { + Resolve: func(ctx context.Context, p types.GQLFRParams) interface{} { if droid, ok := p.Source.(StarWarsChar); ok { return droid.PrimaryFunction } @@ -288,7 +289,7 @@ func init() { Type: episodeEnum, }, }, - Resolve: func(p types.GQLFRParams) (r interface{}) { + Resolve: func(ctx context.Context, p types.GQLFRParams) (r interface{}) { return GetHero(p.Args["episode"]) }, }, @@ -300,7 +301,7 @@ func init() { Type: types.NewGraphQLNonNull(types.GraphQLString), }, }, - Resolve: func(p types.GQLFRParams) (r interface{}) { + Resolve: func(ctx context.Context, p types.GQLFRParams) (r interface{}) { return GetHuman(p.Args["id"].(int)) }, }, @@ -312,7 +313,7 @@ func init() { Type: types.NewGraphQLNonNull(types.GraphQLString), }, }, - Resolve: func(p types.GQLFRParams) (r interface{}) { + Resolve: func(ctx context.Context, p types.GQLFRParams) (r interface{}) { return GetDroid(p.Args["id"].(int)) }, }, diff --git a/types/definition.go b/types/definition.go index ad7b9e4e..f2b5bb37 100644 --- a/types/definition.go +++ b/types/definition.go @@ -8,6 +8,7 @@ import ( "github.com/chris-ramon/graphql-go/errors" "github.com/chris-ramon/graphql-go/language/ast" + "golang.org/x/net/context" ) // These are all of the possible kinds of types. @@ -499,7 +500,7 @@ type GQLFRParams struct { } // TODO: relook at GraphQLFieldResolveFn params -type GraphQLFieldResolveFn func(p GQLFRParams) interface{} +type GraphQLFieldResolveFn func(ctx context.Context, p GQLFRParams) interface{} type GraphQLResolveInfo struct { FieldName string diff --git a/types/introspection.go b/types/introspection.go index 76261530..f51c9040 100644 --- a/types/introspection.go +++ b/types/introspection.go @@ -4,6 +4,7 @@ import ( "fmt" "github.com/chris-ramon/graphql-go/language/ast" "github.com/chris-ramon/graphql-go/language/printer" + "golang.org/x/net/context" "math" "reflect" ) @@ -86,7 +87,7 @@ func init() { Fields: GraphQLFieldConfigMap{ "kind": &GraphQLFieldConfig{ Type: NewGraphQLNonNull(__TypeKind), - Resolve: func(p GQLFRParams) interface{} { + Resolve: func(ctx context.Context, p GQLFRParams) interface{} { switch p.Source.(type) { case *GraphQLScalarType: return TypeKindScalar @@ -137,7 +138,7 @@ func init() { }, "defaultValue": &GraphQLFieldConfig{ Type: GraphQLString, - Resolve: func(p GQLFRParams) interface{} { + Resolve: func(ctx context.Context, p GQLFRParams) interface{} { if inputVal, ok := p.Source.(*GraphQLArgument); ok { if inputVal.DefaultValue == nil { return nil @@ -169,7 +170,7 @@ func init() { }, "args": &GraphQLFieldConfig{ Type: NewGraphQLNonNull(NewGraphQLList(NewGraphQLNonNull(__InputValue))), - Resolve: func(p GQLFRParams) interface{} { + Resolve: func(ctx context.Context, p GQLFRParams) interface{} { if field, ok := p.Source.(*GraphQLFieldDefinition); ok { return field.Args } @@ -181,7 +182,7 @@ func init() { }, "isDeprecated": &GraphQLFieldConfig{ Type: NewGraphQLNonNull(GraphQLBoolean), - Resolve: func(p GQLFRParams) interface{} { + Resolve: func(ctx context.Context, p GQLFRParams) interface{} { if field, ok := p.Source.(*GraphQLFieldDefinition); ok { return (field.DeprecationReason != "") } @@ -232,7 +233,7 @@ mutation operations.`, Type: NewGraphQLNonNull(NewGraphQLList( NewGraphQLNonNull(__Type), )), - Resolve: func(p GQLFRParams) interface{} { + Resolve: func(ctx context.Context, p GQLFRParams) interface{} { if schema, ok := p.Source.(GraphQLSchema); ok { results := []GraphQLType{} for _, ttype := range schema.GetTypeMap() { @@ -246,7 +247,7 @@ mutation operations.`, "queryType": &GraphQLFieldConfig{ Description: "The type that query operations will be rooted at.", Type: NewGraphQLNonNull(__Type), - Resolve: func(p GQLFRParams) interface{} { + Resolve: func(ctx context.Context, p GQLFRParams) interface{} { if schema, ok := p.Source.(GraphQLSchema); ok { return schema.GetQueryType() } @@ -257,7 +258,7 @@ mutation operations.`, Description: `If this server supports mutation, the type that ` + `mutation operations will be rooted at.`, Type: __Type, - Resolve: func(p GQLFRParams) interface{} { + Resolve: func(ctx context.Context, p GQLFRParams) interface{} { if schema, ok := p.Source.(GraphQLSchema); ok { if schema.GetMutationType() != nil { return schema.GetMutationType() @@ -271,7 +272,7 @@ mutation operations.`, Type: NewGraphQLNonNull(NewGraphQLList( NewGraphQLNonNull(__Directive), )), - Resolve: func(p GQLFRParams) interface{} { + Resolve: func(ctx context.Context, p GQLFRParams) interface{} { if schema, ok := p.Source.(GraphQLSchema); ok { return schema.GetDirectives() } @@ -292,7 +293,7 @@ mutation operations.`, }, "isDeprecated": &GraphQLFieldConfig{ Type: NewGraphQLNonNull(GraphQLBoolean), - Resolve: func(p GQLFRParams) interface{} { + Resolve: func(ctx context.Context, p GQLFRParams) interface{} { if field, ok := p.Source.(*GraphQLEnumValueDefinition); ok { return (field.DeprecationReason != "") } @@ -315,7 +316,7 @@ mutation operations.`, DefaultValue: false, }, }, - Resolve: func(p GQLFRParams) interface{} { + Resolve: func(ctx context.Context, p GQLFRParams) interface{} { includeDeprecated, _ := p.Args["includeDeprecated"].(bool) switch ttype := p.Source.(type) { case *GraphQLObjectType: @@ -348,7 +349,7 @@ mutation operations.`, }) __Type.AddFieldConfig("interfaces", &GraphQLFieldConfig{ Type: NewGraphQLList(NewGraphQLNonNull(__Type)), - Resolve: func(p GQLFRParams) interface{} { + Resolve: func(ctx context.Context, p GQLFRParams) interface{} { switch ttype := p.Source.(type) { case *GraphQLObjectType: return ttype.GetInterfaces() @@ -358,7 +359,7 @@ mutation operations.`, }) __Type.AddFieldConfig("possibleTypes", &GraphQLFieldConfig{ Type: NewGraphQLList(NewGraphQLNonNull(__Type)), - Resolve: func(p GQLFRParams) interface{} { + Resolve: func(ctx context.Context, p GQLFRParams) interface{} { switch ttype := p.Source.(type) { case *GraphQLInterfaceType: return ttype.GetPossibleTypes() @@ -376,7 +377,7 @@ mutation operations.`, DefaultValue: false, }, }, - Resolve: func(p GQLFRParams) interface{} { + Resolve: func(ctx context.Context, p GQLFRParams) interface{} { includeDeprecated, _ := p.Args["includeDeprecated"].(bool) switch ttype := p.Source.(type) { case *GraphQLEnumType: @@ -397,7 +398,7 @@ mutation operations.`, }) __Type.AddFieldConfig("inputFields", &GraphQLFieldConfig{ Type: NewGraphQLList(NewGraphQLNonNull(__InputValue)), - Resolve: func(p GQLFRParams) interface{} { + Resolve: func(ctx context.Context, p GQLFRParams) interface{} { switch ttype := p.Source.(type) { case *GraphQLInputObjectType: fields := []*InputObjectField{} @@ -423,7 +424,7 @@ mutation operations.`, Type: NewGraphQLNonNull(__Schema), Description: "Access the current type schema of this server.", Args: []*GraphQLArgument{}, - Resolve: func(p GQLFRParams) interface{} { + Resolve: func(ctx context.Context, p GQLFRParams) interface{} { return p.Info.Schema }, } @@ -437,7 +438,7 @@ mutation operations.`, Type: NewGraphQLNonNull(GraphQLString), }, }, - Resolve: func(p GQLFRParams) interface{} { + Resolve: func(ctx context.Context, p GQLFRParams) interface{} { name, ok := p.Args["name"].(string) if !ok { return nil @@ -451,7 +452,7 @@ mutation operations.`, Type: NewGraphQLNonNull(GraphQLString), Description: "The name of the current Object type at runtime.", Args: []*GraphQLArgument{}, - Resolve: func(p GQLFRParams) interface{} { + Resolve: func(ctx context.Context, p GQLFRParams) interface{} { return p.Info.ParentType.GetName() }, }