Skip to content

Adds Context support. #98

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 7, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions definition.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"regexp"

"github.com/graphql-go/graphql/language/ast"
"golang.org/x/net/context"
)

// These are all of the possible kinds of
Expand Down Expand Up @@ -537,6 +538,9 @@ type ResolveParams struct {
Args map[string]interface{}
Info ResolveInfo
Schema Schema
//This can be used to provide per-request state
//from the application.
Context context.Context
}

// TODO: relook at FieldResolveFn params
Expand Down
16 changes: 13 additions & 3 deletions executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/graphql-go/graphql/gqlerrors"
"github.com/graphql-go/graphql/language/ast"
"golang.org/x/net/context"
)

type ExecuteParams struct {
Expand All @@ -16,6 +17,10 @@ type ExecuteParams struct {
AST *ast.Document
OperationName string
Args map[string]interface{}

// Context may be provided to pass application-specific per-request
// information to resolve functions.
Context context.Context
}

func Execute(p ExecuteParams) (result *Result) {
Expand All @@ -29,6 +34,7 @@ func Execute(p ExecuteParams) (result *Result) {
Args: p.Args,
Errors: nil,
Result: result,
Context: p.Context,
})

if err != nil {
Expand Down Expand Up @@ -62,6 +68,7 @@ type BuildExecutionCtxParams struct {
Args map[string]interface{}
Errors []gqlerrors.FormattedError
Result *Result
Context context.Context
}
type ExecutionContext struct {
Schema Schema
Expand All @@ -70,6 +77,7 @@ type ExecutionContext struct {
Operation ast.Definition
VariableValues map[string]interface{}
Errors []gqlerrors.FormattedError
Context context.Context
}

func buildExecutionContext(p BuildExecutionCtxParams) (*ExecutionContext, error) {
Expand Down Expand Up @@ -124,6 +132,7 @@ func buildExecutionContext(p BuildExecutionCtxParams) (*ExecutionContext, error)
eCtx.Operation = operation
eCtx.VariableValues = variableValues
eCtx.Errors = p.Errors
eCtx.Context = p.Context
return eCtx, nil
}

Expand Down Expand Up @@ -501,9 +510,10 @@ func resolveField(eCtx *ExecutionContext, parentType *Object, source interface{}
var resolveFnError error

result, resolveFnError = resolveFn(ResolveParams{
Source: source,
Args: args,
Info: info,
Source: source,
Args: args,
Info: info,
Context: eCtx.Context,
})

if resolveFnError != nil {
Expand Down
64 changes: 56 additions & 8 deletions executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/graphql-go/graphql/gqlerrors"
"github.com/graphql-go/graphql/language/location"
"github.com/graphql-go/graphql/testutil"
"golang.org/x/net/context"
)

func TestExecutesArbitraryCode(t *testing.T) {
Expand Down Expand Up @@ -295,17 +296,17 @@ func TestMergesParallelFragments(t *testing.T) {
}
}

func TestThreadsContextCorrectly(t *testing.T) {
func TestThreadsSourceCorrectly(t *testing.T) {

query := `
query Example { a }
`

data := map[string]interface{}{
"contextThing": "thing",
"key": "value",
}

var resolvedContext map[string]interface{}
var resolvedSource map[string]interface{}

schema, err := graphql.NewSchema(graphql.SchemaConfig{
Query: graphql.NewObject(graphql.ObjectConfig{
Expand All @@ -314,8 +315,8 @@ func TestThreadsContextCorrectly(t *testing.T) {
"a": &graphql.Field{
Type: graphql.String,
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
resolvedContext = p.Source.(map[string]interface{})
return resolvedContext, nil
resolvedSource = p.Source.(map[string]interface{})
return resolvedSource, nil
},
},
},
Expand All @@ -339,9 +340,9 @@ func TestThreadsContextCorrectly(t *testing.T) {
t.Fatalf("wrong result, unexpected errors: %v", result.Errors)
}

expected := "thing"
if resolvedContext["contextThing"] != expected {
t.Fatalf("Expected context.contextThing to equal %v, got %v", expected, resolvedContext["contextThing"])
expected := "value"
if resolvedSource["key"] != expected {
t.Fatalf("Expected context.key to equal %v, got %v", expected, resolvedSource["key"])
}
}

Expand Down Expand Up @@ -404,6 +405,53 @@ func TestCorrectlyThreadsArguments(t *testing.T) {
}
}

func TestThreadsContextCorrectly(t *testing.T) {

query := `
query Example { a }
`

schema, err := graphql.NewSchema(graphql.SchemaConfig{
Query: graphql.NewObject(graphql.ObjectConfig{
Name: "Type",
Fields: graphql.Fields{
"a": &graphql.Field{
Type: graphql.String,
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
return p.Context.Value("foo"), nil
},
},
},
}),
})
if err != nil {
t.Fatalf("Error in schema %v", err.Error())
}

// parse query
ast := testutil.TestParse(t, query)

// execute
ep := graphql.ExecuteParams{
Schema: schema,
AST: ast,
Context: context.WithValue(context.Background(), "foo", "bar"),
}
result := testutil.TestExecute(t, ep)
if len(result.Errors) > 0 {
t.Fatalf("wrong result, unexpected errors: %v", result.Errors)
}

expected := &graphql.Result{
Data: map[string]interface{}{
"a": "bar",
},
}
if !reflect.DeepEqual(expected, result) {
t.Fatalf("Unexpected result, Diff: %v", testutil.Diff(expected, result))
}
}

func TestNullsOutErrorSubtrees(t *testing.T) {

// TODO: TestNullsOutErrorSubtrees test for go-routines if implemented
Expand Down
6 changes: 6 additions & 0 deletions graphql.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"github.com/graphql-go/graphql/gqlerrors"
"github.com/graphql-go/graphql/language/parser"
"github.com/graphql-go/graphql/language/source"
"golang.org/x/net/context"
)

type Params struct {
Expand All @@ -12,6 +13,10 @@ type Params struct {
RootObject map[string]interface{}
VariableValues map[string]interface{}
OperationName string

// Context may be provided to pass application-specific per-request
// information to resolve functions.
Context context.Context
}

func Do(p Params) *Result {
Expand Down Expand Up @@ -39,5 +44,6 @@ func Do(p Params) *Result {
AST: AST,
OperationName: p.OperationName,
Args: p.VariableValues,
Context: p.Context,
})
}
40 changes: 40 additions & 0 deletions graphql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/graphql-go/graphql"
"github.com/graphql-go/graphql/testutil"
"golang.org/x/net/context"
)

type T struct {
Expand Down Expand Up @@ -131,3 +132,42 @@ func TestBasicGraphQLExample(t *testing.T) {
}

}

func TestThreadsContextFromParamsThrough(t *testing.T) {
extractFieldFromContextFn := func(p graphql.ResolveParams) (interface{}, error) {
return p.Context.Value(p.Args["key"]), nil
}

schema, err := graphql.NewSchema(graphql.SchemaConfig{
Query: graphql.NewObject(graphql.ObjectConfig{
Name: "Query",
Fields: graphql.Fields{
"value": &graphql.Field{
Type: graphql.String,
Args: graphql.FieldConfigArgument{
"key": &graphql.ArgumentConfig{Type: graphql.String},
},
Resolve: extractFieldFromContextFn,
},
},
}),
})
if err != nil {
t.Fatalf("wrong result, unexpected errors: %v", err.Error())
}
query := `{ value(key:"a") }`

result := graphql.Do(graphql.Params{
Schema: schema,
RequestString: query,
Context: context.WithValue(context.TODO(), "a", "xyz"),
})
if len(result.Errors) > 0 {
t.Fatalf("wrong result, unexpected errors: %v", result.Errors)
}
expected := map[string]interface{}{"value": "xyz"}
if !reflect.DeepEqual(result.Data, expected) {
t.Fatalf("wrong result, query: %v, graphql result diff: %v", query, testutil.Diff(expected, result))
}

}