2015-10-28 02:33:17 +02:00
|
|
|
|
package graphql
|
2015-07-11 09:27:14 +02:00
|
|
|
|
|
|
|
|
|
|
import (
|
2015-11-03 22:02:13 -08:00
|
|
|
|
"errors"
|
2015-08-15 00:43:24 -05:00
|
|
|
|
"fmt"
|
2015-09-14 09:41:13 +08:00
|
|
|
|
"reflect"
|
2015-09-19 21:51:32 +08:00
|
|
|
|
"strings"
|
2015-10-30 00:49:49 +02:00
|
|
|
|
|
2015-11-05 10:56:35 +08:00
|
|
|
|
"github.com/graphql-go/graphql/gqlerrors"
|
|
|
|
|
|
"github.com/graphql-go/graphql/language/ast"
|
Add context parameter that passes through the API to resolvers.
This adds a net/context.Context parameter that is threaded through from
the calling API to any resolver functions. This allows an application
to provide custom, per-request handling when resolving queries.
For example, when working on App Engine, all interactions with the
datastore require a per-request context. Other examples include
authentication, logging, or auditing of graphql operations.
An alternative that was considered was to use an arbitrary, application-
provided interface{} value -- that is, the application could stick
anything in that field and it would be up to the app to handle it. This
is fairly reasonable, however using context.Context has a few other
advantages:
- It provides a clean way for the graphql execution system to handle
parallelizing and deadlining/cancelling requests. Doing so would
provide a consistent API to developers to also hook into such
operations.
- It fits with a potentially upcoming trend of using context.Context
for most HTTP handlers.
Going with an arbitrary interface{} now, but later using context.Context
for its other uses as well would result in redundant mechanisms to provide
external (application) metadata to requests.
Another potential alternative is to specifically provide just the
*http.Request pointer. Many libraries do this and use a global,
synchronized map[*http.Request]metadata lookup table. This would satisfy
the AppEngine requirements and provide a minimal mechanism to provide
additional metadata, but the global LUT is clumsy and, again, if
context.Context were later used to manage subprocessing it would provide
a redundant metadata mechanism.
2015-11-30 23:02:58 -08:00
|
|
|
|
"golang.org/x/net/context"
|
2015-07-11 09:27:14 +02:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
type ExecuteParams struct {
|
2015-10-28 02:33:17 +02:00
|
|
|
|
Schema Schema
|
2015-09-19 21:51:32 +08:00
|
|
|
|
Root interface{}
|
2015-10-30 00:49:49 +02:00
|
|
|
|
AST *ast.Document
|
2015-07-11 09:27:14 +02:00
|
|
|
|
OperationName string
|
2015-09-11 14:28:48 +08:00
|
|
|
|
Args map[string]interface{}
|
Add context parameter that passes through the API to resolvers.
This adds a net/context.Context parameter that is threaded through from
the calling API to any resolver functions. This allows an application
to provide custom, per-request handling when resolving queries.
For example, when working on App Engine, all interactions with the
datastore require a per-request context. Other examples include
authentication, logging, or auditing of graphql operations.
An alternative that was considered was to use an arbitrary, application-
provided interface{} value -- that is, the application could stick
anything in that field and it would be up to the app to handle it. This
is fairly reasonable, however using context.Context has a few other
advantages:
- It provides a clean way for the graphql execution system to handle
parallelizing and deadlining/cancelling requests. Doing so would
provide a consistent API to developers to also hook into such
operations.
- It fits with a potentially upcoming trend of using context.Context
for most HTTP handlers.
Going with an arbitrary interface{} now, but later using context.Context
for its other uses as well would result in redundant mechanisms to provide
external (application) metadata to requests.
Another potential alternative is to specifically provide just the
*http.Request pointer. Many libraries do this and use a global,
synchronized map[*http.Request]metadata lookup table. This would satisfy
the AppEngine requirements and provide a minimal mechanism to provide
additional metadata, but the global LUT is clumsy and, again, if
context.Context were later used to manage subprocessing it would provide
a redundant metadata mechanism.
2015-11-30 23:02:58 -08:00
|
|
|
|
|
|
|
|
|
|
// Context may be provided to pass application-specific per-request
|
|
|
|
|
|
// information to resolve functions.
|
|
|
|
|
|
Context context.Context
|
2015-07-11 09:27:14 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2015-11-03 22:02:13 -08:00
|
|
|
|
func Execute(p ExecuteParams) (result *Result) {
|
|
|
|
|
|
result = &Result{}
|
|
|
|
|
|
|
|
|
|
|
|
exeContext, err := buildExecutionContext(BuildExecutionCtxParams{
|
2015-07-11 09:27:14 +02:00
|
|
|
|
Schema: p.Schema,
|
|
|
|
|
|
Root: p.Root,
|
|
|
|
|
|
AST: p.AST,
|
|
|
|
|
|
OperationName: p.OperationName,
|
|
|
|
|
|
Args: p.Args,
|
2015-11-03 22:02:13 -08:00
|
|
|
|
Errors: nil,
|
|
|
|
|
|
Result: result,
|
Add context parameter that passes through the API to resolvers.
This adds a net/context.Context parameter that is threaded through from
the calling API to any resolver functions. This allows an application
to provide custom, per-request handling when resolving queries.
For example, when working on App Engine, all interactions with the
datastore require a per-request context. Other examples include
authentication, logging, or auditing of graphql operations.
An alternative that was considered was to use an arbitrary, application-
provided interface{} value -- that is, the application could stick
anything in that field and it would be up to the app to handle it. This
is fairly reasonable, however using context.Context has a few other
advantages:
- It provides a clean way for the graphql execution system to handle
parallelizing and deadlining/cancelling requests. Doing so would
provide a consistent API to developers to also hook into such
operations.
- It fits with a potentially upcoming trend of using context.Context
for most HTTP handlers.
Going with an arbitrary interface{} now, but later using context.Context
for its other uses as well would result in redundant mechanisms to provide
external (application) metadata to requests.
Another potential alternative is to specifically provide just the
*http.Request pointer. Many libraries do this and use a global,
synchronized map[*http.Request]metadata lookup table. This would satisfy
the AppEngine requirements and provide a minimal mechanism to provide
additional metadata, but the global LUT is clumsy and, again, if
context.Context were later used to manage subprocessing it would provide
a redundant metadata mechanism.
2015-11-30 23:02:58 -08:00
|
|
|
|
Context: p.Context,
|
2015-11-03 22:02:13 -08:00
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
result.Errors = append(result.Errors, gqlerrors.FormatError(err))
|
2015-08-15 00:43:24 -05:00
|
|
|
|
return
|
|
|
|
|
|
}
|
2015-11-03 22:02:13 -08:00
|
|
|
|
|
2015-09-19 18:26:46 +08:00
|
|
|
|
defer func() {
|
|
|
|
|
|
if r := recover(); r != nil {
|
|
|
|
|
|
var err error
|
|
|
|
|
|
if r, ok := r.(error); ok {
|
2015-10-30 00:49:49 +02:00
|
|
|
|
err = gqlerrors.FormatError(r)
|
2015-09-19 18:26:46 +08:00
|
|
|
|
}
|
2015-10-30 00:49:49 +02:00
|
|
|
|
exeContext.Errors = append(exeContext.Errors, gqlerrors.FormatError(err))
|
2015-09-19 18:26:46 +08:00
|
|
|
|
result.Errors = exeContext.Errors
|
|
|
|
|
|
}
|
|
|
|
|
|
}()
|
2015-11-03 22:02:13 -08:00
|
|
|
|
|
|
|
|
|
|
return executeOperation(ExecuteOperationParams{
|
2015-07-11 09:27:14 +02:00
|
|
|
|
ExecutionContext: exeContext,
|
|
|
|
|
|
Root: p.Root,
|
|
|
|
|
|
Operation: exeContext.Operation,
|
2015-11-03 22:02:13 -08:00
|
|
|
|
})
|
2015-07-11 09:27:14 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2015-09-16 11:42:48 +08:00
|
|
|
|
type BuildExecutionCtxParams struct {
|
2015-10-28 02:33:17 +02:00
|
|
|
|
Schema Schema
|
2015-09-19 21:51:32 +08:00
|
|
|
|
Root interface{}
|
2015-10-30 00:49:49 +02:00
|
|
|
|
AST *ast.Document
|
2015-09-16 11:42:48 +08:00
|
|
|
|
OperationName string
|
|
|
|
|
|
Args map[string]interface{}
|
2015-10-30 00:49:49 +02:00
|
|
|
|
Errors []gqlerrors.FormattedError
|
2015-10-28 02:33:17 +02:00
|
|
|
|
Result *Result
|
Add context parameter that passes through the API to resolvers.
This adds a net/context.Context parameter that is threaded through from
the calling API to any resolver functions. This allows an application
to provide custom, per-request handling when resolving queries.
For example, when working on App Engine, all interactions with the
datastore require a per-request context. Other examples include
authentication, logging, or auditing of graphql operations.
An alternative that was considered was to use an arbitrary, application-
provided interface{} value -- that is, the application could stick
anything in that field and it would be up to the app to handle it. This
is fairly reasonable, however using context.Context has a few other
advantages:
- It provides a clean way for the graphql execution system to handle
parallelizing and deadlining/cancelling requests. Doing so would
provide a consistent API to developers to also hook into such
operations.
- It fits with a potentially upcoming trend of using context.Context
for most HTTP handlers.
Going with an arbitrary interface{} now, but later using context.Context
for its other uses as well would result in redundant mechanisms to provide
external (application) metadata to requests.
Another potential alternative is to specifically provide just the
*http.Request pointer. Many libraries do this and use a global,
synchronized map[*http.Request]metadata lookup table. This would satisfy
the AppEngine requirements and provide a minimal mechanism to provide
additional metadata, but the global LUT is clumsy and, again, if
context.Context were later used to manage subprocessing it would provide
a redundant metadata mechanism.
2015-11-30 23:02:58 -08:00
|
|
|
|
Context context.Context
|
2015-09-16 11:42:48 +08:00
|
|
|
|
}
|
|
|
|
|
|
type ExecutionContext struct {
|
2015-10-28 02:33:17 +02:00
|
|
|
|
Schema Schema
|
2015-10-30 00:49:49 +02:00
|
|
|
|
Fragments map[string]ast.Definition
|
2015-09-19 21:51:32 +08:00
|
|
|
|
Root interface{}
|
2015-10-30 00:49:49 +02:00
|
|
|
|
Operation ast.Definition
|
2015-09-16 11:42:48 +08:00
|
|
|
|
VariableValues map[string]interface{}
|
2015-10-30 00:49:49 +02:00
|
|
|
|
Errors []gqlerrors.FormattedError
|
Add context parameter that passes through the API to resolvers.
This adds a net/context.Context parameter that is threaded through from
the calling API to any resolver functions. This allows an application
to provide custom, per-request handling when resolving queries.
For example, when working on App Engine, all interactions with the
datastore require a per-request context. Other examples include
authentication, logging, or auditing of graphql operations.
An alternative that was considered was to use an arbitrary, application-
provided interface{} value -- that is, the application could stick
anything in that field and it would be up to the app to handle it. This
is fairly reasonable, however using context.Context has a few other
advantages:
- It provides a clean way for the graphql execution system to handle
parallelizing and deadlining/cancelling requests. Doing so would
provide a consistent API to developers to also hook into such
operations.
- It fits with a potentially upcoming trend of using context.Context
for most HTTP handlers.
Going with an arbitrary interface{} now, but later using context.Context
for its other uses as well would result in redundant mechanisms to provide
external (application) metadata to requests.
Another potential alternative is to specifically provide just the
*http.Request pointer. Many libraries do this and use a global,
synchronized map[*http.Request]metadata lookup table. This would satisfy
the AppEngine requirements and provide a minimal mechanism to provide
additional metadata, but the global LUT is clumsy and, again, if
context.Context were later used to manage subprocessing it would provide
a redundant metadata mechanism.
2015-11-30 23:02:58 -08:00
|
|
|
|
Context context.Context
|
2015-09-16 11:42:48 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2015-11-03 22:02:13 -08:00
|
|
|
|
func buildExecutionContext(p BuildExecutionCtxParams) (*ExecutionContext, error) {
|
2015-09-19 18:26:46 +08:00
|
|
|
|
eCtx := &ExecutionContext{}
|
2016-03-08 10:26:35 +08:00
|
|
|
|
var operation *ast.OperationDefinition
|
2015-10-30 00:49:49 +02:00
|
|
|
|
fragments := map[string]ast.Definition{}
|
2016-03-08 10:26:35 +08:00
|
|
|
|
|
|
|
|
|
|
for _, definition := range p.AST.Definitions {
|
|
|
|
|
|
switch definition := definition.(type) {
|
2015-10-30 00:49:49 +02:00
|
|
|
|
case *ast.OperationDefinition:
|
2016-03-08 10:26:35 +08:00
|
|
|
|
if (p.OperationName == "") && operation != nil {
|
|
|
|
|
|
return nil, errors.New("Must provide operation name if query contains multiple operations.")
|
|
|
|
|
|
}
|
|
|
|
|
|
if p.OperationName == "" || definition.GetName() != nil && definition.GetName().Value == p.OperationName {
|
|
|
|
|
|
operation = definition
|
2015-09-16 11:42:48 +08:00
|
|
|
|
}
|
2015-10-30 00:49:49 +02:00
|
|
|
|
case *ast.FragmentDefinition:
|
2015-09-16 11:42:48 +08:00
|
|
|
|
key := ""
|
2016-03-08 10:26:35 +08:00
|
|
|
|
if definition.GetName() != nil && definition.GetName().Value != "" {
|
|
|
|
|
|
key = definition.GetName().Value
|
2015-09-16 11:42:48 +08:00
|
|
|
|
}
|
2016-03-08 10:26:35 +08:00
|
|
|
|
fragments[key] = definition
|
2015-09-16 11:42:48 +08:00
|
|
|
|
default:
|
2016-03-08 10:26:35 +08:00
|
|
|
|
return nil, fmt.Errorf("GraphQL cannot execute a request containing a %v", definition.GetKind())
|
2015-09-16 11:42:48 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2015-11-03 22:02:13 -08:00
|
|
|
|
|
2016-03-08 10:26:35 +08:00
|
|
|
|
if operation == nil {
|
2016-05-30 12:07:33 +08:00
|
|
|
|
if p.OperationName != "" {
|
2016-03-08 10:26:35 +08:00
|
|
|
|
return nil, fmt.Errorf(`Unknown operation named "%v".`, p.OperationName)
|
2015-09-16 11:42:48 +08:00
|
|
|
|
}
|
2016-05-30 17:21:35 +08:00
|
|
|
|
return nil, fmt.Errorf(`Must provide an operation.`)
|
2015-09-16 11:42:48 +08:00
|
|
|
|
}
|
2015-11-03 22:02:13 -08:00
|
|
|
|
|
2015-09-16 11:42:48 +08:00
|
|
|
|
variableValues, err := getVariableValues(p.Schema, operation.GetVariableDefinitions(), p.Args)
|
|
|
|
|
|
if err != nil {
|
2015-11-03 22:02:13 -08:00
|
|
|
|
return nil, err
|
2015-09-16 11:42:48 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
eCtx.Schema = p.Schema
|
|
|
|
|
|
eCtx.Fragments = fragments
|
|
|
|
|
|
eCtx.Root = p.Root
|
|
|
|
|
|
eCtx.Operation = operation
|
|
|
|
|
|
eCtx.VariableValues = variableValues
|
|
|
|
|
|
eCtx.Errors = p.Errors
|
Add context parameter that passes through the API to resolvers.
This adds a net/context.Context parameter that is threaded through from
the calling API to any resolver functions. This allows an application
to provide custom, per-request handling when resolving queries.
For example, when working on App Engine, all interactions with the
datastore require a per-request context. Other examples include
authentication, logging, or auditing of graphql operations.
An alternative that was considered was to use an arbitrary, application-
provided interface{} value -- that is, the application could stick
anything in that field and it would be up to the app to handle it. This
is fairly reasonable, however using context.Context has a few other
advantages:
- It provides a clean way for the graphql execution system to handle
parallelizing and deadlining/cancelling requests. Doing so would
provide a consistent API to developers to also hook into such
operations.
- It fits with a potentially upcoming trend of using context.Context
for most HTTP handlers.
Going with an arbitrary interface{} now, but later using context.Context
for its other uses as well would result in redundant mechanisms to provide
external (application) metadata to requests.
Another potential alternative is to specifically provide just the
*http.Request pointer. Many libraries do this and use a global,
synchronized map[*http.Request]metadata lookup table. This would satisfy
the AppEngine requirements and provide a minimal mechanism to provide
additional metadata, but the global LUT is clumsy and, again, if
context.Context were later used to manage subprocessing it would provide
a redundant metadata mechanism.
2015-11-30 23:02:58 -08:00
|
|
|
|
eCtx.Context = p.Context
|
2015-11-03 22:02:13 -08:00
|
|
|
|
return eCtx, nil
|
2015-09-16 11:42:48 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2015-07-11 17:59:28 +02:00
|
|
|
|
type ExecuteOperationParams struct {
|
2015-09-19 18:26:46 +08:00
|
|
|
|
ExecutionContext *ExecutionContext
|
2015-09-19 21:51:32 +08:00
|
|
|
|
Root interface{}
|
2015-10-30 00:49:49 +02:00
|
|
|
|
Operation ast.Definition
|
2015-07-11 17:59:28 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2015-11-03 22:02:13 -08:00
|
|
|
|
func executeOperation(p ExecuteOperationParams) *Result {
|
|
|
|
|
|
operationType, err := getOperationRootType(p.ExecutionContext.Schema, p.Operation)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return &Result{Errors: gqlerrors.FormatErrors(err)}
|
|
|
|
|
|
}
|
2015-09-12 11:09:33 +08:00
|
|
|
|
|
2015-11-03 22:02:13 -08:00
|
|
|
|
fields := collectFields(CollectFieldsParams{
|
2016-04-06 17:23:31 +08:00
|
|
|
|
ExeContext: p.ExecutionContext,
|
|
|
|
|
|
RuntimeType: operationType,
|
|
|
|
|
|
SelectionSet: p.Operation.GetSelectionSet(),
|
2015-11-03 22:02:13 -08:00
|
|
|
|
})
|
|
|
|
|
|
|
2015-07-12 00:11:14 +02:00
|
|
|
|
executeFieldsParams := ExecuteFieldsParams{
|
2015-07-16 13:25:46 +02:00
|
|
|
|
ExecutionContext: p.ExecutionContext,
|
|
|
|
|
|
ParentType: operationType,
|
|
|
|
|
|
Source: p.Root,
|
|
|
|
|
|
Fields: fields,
|
2015-07-12 00:11:14 +02:00
|
|
|
|
}
|
2015-11-03 22:02:13 -08:00
|
|
|
|
|
2016-05-30 23:50:59 +08:00
|
|
|
|
if p.Operation.GetOperation() == ast.OperationTypeMutation {
|
2015-11-03 22:02:13 -08:00
|
|
|
|
return executeFieldsSerially(executeFieldsParams)
|
2015-09-15 12:05:34 +08:00
|
|
|
|
}
|
2016-04-15 18:02:57 +08:00
|
|
|
|
return executeFields(executeFieldsParams)
|
|
|
|
|
|
|
2015-07-11 17:59:28 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2015-09-12 00:22:22 +08:00
|
|
|
|
// Extracts the root type of the operation from the schema.
|
2015-11-03 22:02:13 -08:00
|
|
|
|
func getOperationRootType(schema Schema, operation ast.Definition) (*Object, error) {
|
2015-09-14 09:41:13 +08:00
|
|
|
|
if operation == nil {
|
2015-11-03 22:02:13 -08:00
|
|
|
|
return nil, errors.New("Can only execute queries and mutations")
|
2015-09-14 09:41:13 +08:00
|
|
|
|
}
|
2015-11-03 22:02:13 -08:00
|
|
|
|
|
2015-08-15 00:43:24 -05:00
|
|
|
|
switch operation.GetOperation() {
|
2016-05-30 23:50:59 +08:00
|
|
|
|
case ast.OperationTypeQuery:
|
2015-11-07 16:01:14 -08:00
|
|
|
|
return schema.QueryType(), nil
|
2016-05-30 23:50:59 +08:00
|
|
|
|
case ast.OperationTypeMutation:
|
2015-11-07 16:01:14 -08:00
|
|
|
|
mutationType := schema.MutationType()
|
2015-11-07 16:59:40 -08:00
|
|
|
|
if mutationType.PrivateName == "" {
|
2016-03-11 13:29:52 +08:00
|
|
|
|
return nil, gqlerrors.NewError(
|
|
|
|
|
|
"Schema is not configured for mutations",
|
|
|
|
|
|
[]ast.Node{operation},
|
|
|
|
|
|
"",
|
|
|
|
|
|
nil,
|
|
|
|
|
|
[]int{},
|
|
|
|
|
|
nil,
|
|
|
|
|
|
)
|
2015-08-15 00:43:24 -05:00
|
|
|
|
}
|
2015-11-03 22:02:13 -08:00
|
|
|
|
return mutationType, nil
|
2016-05-30 23:50:59 +08:00
|
|
|
|
case ast.OperationTypeSubscription:
|
2016-03-11 13:29:52 +08:00
|
|
|
|
subscriptionType := schema.SubscriptionType()
|
|
|
|
|
|
if subscriptionType.PrivateName == "" {
|
|
|
|
|
|
return nil, gqlerrors.NewError(
|
|
|
|
|
|
"Schema is not configured for subscriptions",
|
|
|
|
|
|
[]ast.Node{operation},
|
|
|
|
|
|
"",
|
|
|
|
|
|
nil,
|
|
|
|
|
|
[]int{},
|
|
|
|
|
|
nil,
|
|
|
|
|
|
)
|
|
|
|
|
|
}
|
|
|
|
|
|
return subscriptionType, nil
|
2015-08-15 00:43:24 -05:00
|
|
|
|
default:
|
2016-03-11 13:29:52 +08:00
|
|
|
|
return nil, gqlerrors.NewError(
|
|
|
|
|
|
"Can only execute queries, mutations and subscription",
|
|
|
|
|
|
[]ast.Node{operation},
|
|
|
|
|
|
"",
|
|
|
|
|
|
nil,
|
|
|
|
|
|
[]int{},
|
|
|
|
|
|
nil,
|
|
|
|
|
|
)
|
2015-08-15 00:43:24 -05:00
|
|
|
|
}
|
2015-07-12 00:11:14 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2015-09-16 11:42:48 +08:00
|
|
|
|
type ExecuteFieldsParams struct {
|
2015-09-19 18:26:46 +08:00
|
|
|
|
ExecutionContext *ExecutionContext
|
2015-10-28 02:33:17 +02:00
|
|
|
|
ParentType *Object
|
2015-09-16 11:42:48 +08:00
|
|
|
|
Source interface{}
|
2015-10-30 00:49:49 +02:00
|
|
|
|
Fields map[string][]*ast.Field
|
2015-09-16 11:42:48 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Implements the "Evaluating selection sets" section of the spec for "write" mode.
|
2015-11-03 22:02:13 -08:00
|
|
|
|
func executeFieldsSerially(p ExecuteFieldsParams) *Result {
|
2015-09-16 11:42:48 +08:00
|
|
|
|
if p.Source == nil {
|
|
|
|
|
|
p.Source = map[string]interface{}{}
|
|
|
|
|
|
}
|
|
|
|
|
|
if p.Fields == nil {
|
2015-10-30 00:49:49 +02:00
|
|
|
|
p.Fields = map[string][]*ast.Field{}
|
2015-09-16 11:42:48 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
finalResults := map[string]interface{}{}
|
|
|
|
|
|
for responseName, fieldASTs := range p.Fields {
|
2015-09-19 21:51:32 +08:00
|
|
|
|
resolved, state := resolveField(p.ExecutionContext, p.ParentType, p.Source, fieldASTs)
|
2015-09-19 18:26:46 +08:00
|
|
|
|
if state.hasNoFieldDefs {
|
2015-09-17 18:32:16 +08:00
|
|
|
|
continue
|
|
|
|
|
|
}
|
|
|
|
|
|
finalResults[responseName] = resolved
|
2015-09-16 11:42:48 +08:00
|
|
|
|
}
|
2015-11-03 22:02:13 -08:00
|
|
|
|
|
|
|
|
|
|
return &Result{
|
|
|
|
|
|
Data: finalResults,
|
|
|
|
|
|
Errors: p.ExecutionContext.Errors,
|
|
|
|
|
|
}
|
2015-09-16 11:42:48 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Implements the "Evaluating selection sets" section of the spec for "read" mode.
|
2015-11-03 22:02:13 -08:00
|
|
|
|
func executeFields(p ExecuteFieldsParams) *Result {
|
2015-09-16 11:42:48 +08:00
|
|
|
|
if p.Source == nil {
|
|
|
|
|
|
p.Source = map[string]interface{}{}
|
|
|
|
|
|
}
|
|
|
|
|
|
if p.Fields == nil {
|
2015-10-30 00:49:49 +02:00
|
|
|
|
p.Fields = map[string][]*ast.Field{}
|
2015-09-16 11:42:48 +08:00
|
|
|
|
}
|
2015-11-03 22:02:13 -08:00
|
|
|
|
|
2015-09-16 11:42:48 +08:00
|
|
|
|
finalResults := map[string]interface{}{}
|
|
|
|
|
|
for responseName, fieldASTs := range p.Fields {
|
2015-09-19 21:51:32 +08:00
|
|
|
|
resolved, state := resolveField(p.ExecutionContext, p.ParentType, p.Source, fieldASTs)
|
2015-09-19 18:26:46 +08:00
|
|
|
|
if state.hasNoFieldDefs {
|
2015-09-17 18:32:16 +08:00
|
|
|
|
continue
|
|
|
|
|
|
}
|
|
|
|
|
|
finalResults[responseName] = resolved
|
2015-09-16 11:42:48 +08:00
|
|
|
|
}
|
2015-11-03 22:02:13 -08:00
|
|
|
|
|
|
|
|
|
|
return &Result{
|
|
|
|
|
|
Data: finalResults,
|
|
|
|
|
|
Errors: p.ExecutionContext.Errors,
|
2015-09-19 18:26:46 +08:00
|
|
|
|
}
|
2015-09-16 11:42:48 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2015-07-12 00:11:14 +02:00
|
|
|
|
type CollectFieldsParams struct {
|
2015-09-19 18:26:46 +08:00
|
|
|
|
ExeContext *ExecutionContext
|
2016-04-06 17:23:31 +08:00
|
|
|
|
RuntimeType *Object // previously known as OperationType
|
2015-10-30 00:49:49 +02:00
|
|
|
|
SelectionSet *ast.SelectionSet
|
|
|
|
|
|
Fields map[string][]*ast.Field
|
2015-07-12 00:11:14 +02:00
|
|
|
|
VisitedFragmentNames map[string]bool
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2015-09-12 00:22:22 +08:00
|
|
|
|
// Given a selectionSet, adds all of the fields in that selection to
|
|
|
|
|
|
// the passed in map of fields, and returns it at the end.
|
2016-04-06 17:23:31 +08:00
|
|
|
|
// CollectFields requires the "runtime type" of an object. For a field which
|
|
|
|
|
|
// returns and Interface or Union type, the "runtime type" will be the actual
|
|
|
|
|
|
// Object type returned by that field.
|
2015-10-30 00:49:49 +02:00
|
|
|
|
func collectFields(p CollectFieldsParams) map[string][]*ast.Field {
|
2015-09-12 00:22:22 +08:00
|
|
|
|
|
|
|
|
|
|
fields := p.Fields
|
|
|
|
|
|
if fields == nil {
|
2015-10-30 00:49:49 +02:00
|
|
|
|
fields = map[string][]*ast.Field{}
|
2015-09-12 00:22:22 +08:00
|
|
|
|
}
|
2015-09-14 15:48:30 +08:00
|
|
|
|
if p.VisitedFragmentNames == nil {
|
|
|
|
|
|
p.VisitedFragmentNames = map[string]bool{}
|
|
|
|
|
|
}
|
2015-09-12 00:22:22 +08:00
|
|
|
|
if p.SelectionSet == nil {
|
|
|
|
|
|
return fields
|
|
|
|
|
|
}
|
|
|
|
|
|
for _, iSelection := range p.SelectionSet.Selections {
|
|
|
|
|
|
switch selection := iSelection.(type) {
|
2015-10-30 00:49:49 +02:00
|
|
|
|
case *ast.Field:
|
2015-09-12 00:22:22 +08:00
|
|
|
|
if !shouldIncludeNode(p.ExeContext, selection.Directives) {
|
|
|
|
|
|
continue
|
|
|
|
|
|
}
|
|
|
|
|
|
name := getFieldEntryKey(selection)
|
|
|
|
|
|
if _, ok := fields[name]; !ok {
|
2015-10-30 00:49:49 +02:00
|
|
|
|
fields[name] = []*ast.Field{}
|
2015-09-12 00:22:22 +08:00
|
|
|
|
}
|
|
|
|
|
|
fields[name] = append(fields[name], selection)
|
2015-10-30 00:49:49 +02:00
|
|
|
|
case *ast.InlineFragment:
|
2015-09-14 15:48:30 +08:00
|
|
|
|
|
2015-09-12 00:22:22 +08:00
|
|
|
|
if !shouldIncludeNode(p.ExeContext, selection.Directives) ||
|
2016-04-06 17:23:31 +08:00
|
|
|
|
!doesFragmentConditionMatch(p.ExeContext, selection, p.RuntimeType) {
|
2015-09-12 00:22:22 +08:00
|
|
|
|
continue
|
|
|
|
|
|
}
|
|
|
|
|
|
innerParams := CollectFieldsParams{
|
2015-09-14 09:41:13 +08:00
|
|
|
|
ExeContext: p.ExeContext,
|
2016-04-06 17:23:31 +08:00
|
|
|
|
RuntimeType: p.RuntimeType,
|
2015-09-14 09:41:13 +08:00
|
|
|
|
SelectionSet: selection.SelectionSet,
|
|
|
|
|
|
Fields: fields,
|
2015-09-12 00:22:22 +08:00
|
|
|
|
VisitedFragmentNames: p.VisitedFragmentNames,
|
|
|
|
|
|
}
|
2015-09-14 15:48:30 +08:00
|
|
|
|
collectFields(innerParams)
|
2015-10-30 00:49:49 +02:00
|
|
|
|
case *ast.FragmentSpread:
|
2015-09-12 00:22:22 +08:00
|
|
|
|
fragName := ""
|
|
|
|
|
|
if selection.Name != nil {
|
|
|
|
|
|
fragName = selection.Name.Value
|
|
|
|
|
|
}
|
2015-09-14 15:48:30 +08:00
|
|
|
|
if visited, ok := p.VisitedFragmentNames[fragName]; (ok && visited) ||
|
2015-09-14 16:03:08 +08:00
|
|
|
|
!shouldIncludeNode(p.ExeContext, selection.Directives) {
|
2015-09-12 00:22:22 +08:00
|
|
|
|
continue
|
|
|
|
|
|
}
|
|
|
|
|
|
p.VisitedFragmentNames[fragName] = true
|
|
|
|
|
|
fragment, hasFragment := p.ExeContext.Fragments[fragName]
|
|
|
|
|
|
if !hasFragment {
|
|
|
|
|
|
continue
|
|
|
|
|
|
}
|
2015-09-14 15:48:30 +08:00
|
|
|
|
|
2015-10-30 00:49:49 +02:00
|
|
|
|
if fragment, ok := fragment.(*ast.FragmentDefinition); ok {
|
2015-09-12 00:22:22 +08:00
|
|
|
|
if !shouldIncludeNode(p.ExeContext, fragment.Directives) ||
|
2016-04-06 17:23:31 +08:00
|
|
|
|
!doesFragmentConditionMatch(p.ExeContext, fragment, p.RuntimeType) {
|
2015-09-12 00:22:22 +08:00
|
|
|
|
continue
|
|
|
|
|
|
}
|
|
|
|
|
|
innerParams := CollectFieldsParams{
|
2015-09-14 09:41:13 +08:00
|
|
|
|
ExeContext: p.ExeContext,
|
2016-04-06 17:23:31 +08:00
|
|
|
|
RuntimeType: p.RuntimeType,
|
2015-09-14 09:41:13 +08:00
|
|
|
|
SelectionSet: fragment.GetSelectionSet(),
|
|
|
|
|
|
Fields: fields,
|
2015-09-12 00:22:22 +08:00
|
|
|
|
VisitedFragmentNames: p.VisitedFragmentNames,
|
|
|
|
|
|
}
|
2015-09-14 15:48:30 +08:00
|
|
|
|
collectFields(innerParams)
|
2015-09-12 00:22:22 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return fields
|
2015-07-12 00:11:14 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2015-09-12 00:22:22 +08:00
|
|
|
|
// Determines if a field should be included based on the @include and @skip
|
|
|
|
|
|
// directives, where @skip has higher precedence than @include.
|
2015-10-30 00:49:49 +02:00
|
|
|
|
func shouldIncludeNode(eCtx *ExecutionContext, directives []*ast.Directive) bool {
|
2015-09-14 15:48:30 +08:00
|
|
|
|
|
|
|
|
|
|
defaultReturnValue := true
|
|
|
|
|
|
|
2015-10-30 00:49:49 +02:00
|
|
|
|
var skipAST *ast.Directive
|
|
|
|
|
|
var includeAST *ast.Directive
|
2015-09-14 15:48:30 +08:00
|
|
|
|
for _, directive := range directives {
|
|
|
|
|
|
if directive == nil || directive.Name == nil {
|
|
|
|
|
|
continue
|
|
|
|
|
|
}
|
2015-10-28 02:33:17 +02:00
|
|
|
|
if directive.Name.Value == SkipDirective.Name {
|
2015-09-14 15:48:30 +08:00
|
|
|
|
skipAST = directive
|
|
|
|
|
|
break
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
if skipAST != nil {
|
|
|
|
|
|
argValues, err := getArgumentValues(
|
2015-10-28 02:33:17 +02:00
|
|
|
|
SkipDirective.Args,
|
2015-09-14 15:48:30 +08:00
|
|
|
|
skipAST.Arguments,
|
|
|
|
|
|
eCtx.VariableValues,
|
|
|
|
|
|
)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return defaultReturnValue
|
|
|
|
|
|
}
|
|
|
|
|
|
if skipIf, ok := argValues["if"]; ok {
|
|
|
|
|
|
if boolSkipIf, ok := skipIf.(bool); ok {
|
|
|
|
|
|
return !boolSkipIf
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return defaultReturnValue
|
|
|
|
|
|
}
|
|
|
|
|
|
for _, directive := range directives {
|
|
|
|
|
|
if directive == nil || directive.Name == nil {
|
|
|
|
|
|
continue
|
|
|
|
|
|
}
|
2015-10-28 02:33:17 +02:00
|
|
|
|
if directive.Name.Value == IncludeDirective.Name {
|
2015-09-14 15:48:30 +08:00
|
|
|
|
includeAST = directive
|
|
|
|
|
|
break
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
if includeAST != nil {
|
|
|
|
|
|
argValues, err := getArgumentValues(
|
2015-10-28 02:33:17 +02:00
|
|
|
|
IncludeDirective.Args,
|
2015-09-17 01:21:35 +08:00
|
|
|
|
includeAST.Arguments,
|
2015-09-14 15:48:30 +08:00
|
|
|
|
eCtx.VariableValues,
|
|
|
|
|
|
)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return defaultReturnValue
|
|
|
|
|
|
}
|
|
|
|
|
|
if includeIf, ok := argValues["if"]; ok {
|
|
|
|
|
|
if boolIncludeIf, ok := includeIf.(bool); ok {
|
2015-09-17 01:21:35 +08:00
|
|
|
|
return boolIncludeIf
|
2015-09-14 15:48:30 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return defaultReturnValue
|
|
|
|
|
|
}
|
|
|
|
|
|
return defaultReturnValue
|
2015-09-12 00:22:22 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Determines if a fragment is applicable to the given type.
|
2015-10-30 00:49:49 +02:00
|
|
|
|
func doesFragmentConditionMatch(eCtx *ExecutionContext, fragment ast.Node, ttype *Object) bool {
|
2015-09-12 11:09:33 +08:00
|
|
|
|
|
2015-09-14 15:48:30 +08:00
|
|
|
|
switch fragment := fragment.(type) {
|
2015-10-30 00:49:49 +02:00
|
|
|
|
case *ast.FragmentDefinition:
|
2016-03-08 09:11:31 +08:00
|
|
|
|
typeConditionAST := fragment.TypeCondition
|
|
|
|
|
|
if typeConditionAST == nil {
|
|
|
|
|
|
return true
|
|
|
|
|
|
}
|
|
|
|
|
|
conditionalType, err := typeFromAST(eCtx.Schema, typeConditionAST)
|
2015-09-14 15:48:30 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
|
return false
|
|
|
|
|
|
}
|
|
|
|
|
|
if conditionalType == ttype {
|
|
|
|
|
|
return true
|
|
|
|
|
|
}
|
2016-03-07 16:40:37 +08:00
|
|
|
|
if conditionalType.Name() == ttype.Name() {
|
2015-12-29 15:42:33 +05:30
|
|
|
|
return true
|
|
|
|
|
|
}
|
2015-10-28 02:33:17 +02:00
|
|
|
|
if conditionalType, ok := conditionalType.(Abstract); ok {
|
2015-09-14 15:48:30 +08:00
|
|
|
|
return conditionalType.IsPossibleType(ttype)
|
|
|
|
|
|
}
|
2015-10-30 00:49:49 +02:00
|
|
|
|
case *ast.InlineFragment:
|
2016-03-08 09:11:31 +08:00
|
|
|
|
typeConditionAST := fragment.TypeCondition
|
|
|
|
|
|
if typeConditionAST == nil {
|
|
|
|
|
|
return true
|
|
|
|
|
|
}
|
|
|
|
|
|
conditionalType, err := typeFromAST(eCtx.Schema, typeConditionAST)
|
2015-09-14 15:48:30 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
|
return false
|
|
|
|
|
|
}
|
|
|
|
|
|
if conditionalType == ttype {
|
|
|
|
|
|
return true
|
|
|
|
|
|
}
|
2016-03-08 09:11:31 +08:00
|
|
|
|
if conditionalType.Name() == ttype.Name() {
|
|
|
|
|
|
return true
|
|
|
|
|
|
}
|
2015-10-28 02:33:17 +02:00
|
|
|
|
if conditionalType, ok := conditionalType.(Abstract); ok {
|
2015-09-14 15:48:30 +08:00
|
|
|
|
return conditionalType.IsPossibleType(ttype)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return false
|
|
|
|
|
|
}
|
2015-09-14 16:03:08 +08:00
|
|
|
|
|
2015-09-16 11:42:48 +08:00
|
|
|
|
// Implements the logic to compute the key of a given field’s entry
|
2015-10-30 00:49:49 +02:00
|
|
|
|
func getFieldEntryKey(node *ast.Field) string {
|
2015-09-16 11:42:48 +08:00
|
|
|
|
|
|
|
|
|
|
if node.Alias != nil && node.Alias.Value != "" {
|
|
|
|
|
|
return node.Alias.Value
|
|
|
|
|
|
}
|
|
|
|
|
|
if node.Name != nil && node.Name.Value != "" {
|
|
|
|
|
|
return node.Name.Value
|
|
|
|
|
|
}
|
|
|
|
|
|
return ""
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2015-09-19 18:26:46 +08:00
|
|
|
|
// Internal resolveField state
|
|
|
|
|
|
type resolveFieldResultState struct {
|
|
|
|
|
|
hasNoFieldDefs bool
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2016-04-15 18:02:57 +08:00
|
|
|
|
// Resolves the field on the given source object. In particular, this
|
|
|
|
|
|
// 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.
|
2015-10-30 00:49:49 +02:00
|
|
|
|
func resolveField(eCtx *ExecutionContext, parentType *Object, source interface{}, fieldASTs []*ast.Field) (result interface{}, resultState resolveFieldResultState) {
|
2015-09-19 18:26:46 +08:00
|
|
|
|
// catch panic from resolveFn
|
2015-10-28 02:33:17 +02:00
|
|
|
|
var returnType Output
|
2015-09-19 21:51:32 +08:00
|
|
|
|
defer func() (interface{}, resolveFieldResultState) {
|
2015-09-15 03:04:36 +08:00
|
|
|
|
if r := recover(); r != nil {
|
2015-09-19 21:51:32 +08:00
|
|
|
|
|
2015-09-19 18:26:46 +08:00
|
|
|
|
var err error
|
|
|
|
|
|
if r, ok := r.(string); ok {
|
2015-10-28 02:33:17 +02:00
|
|
|
|
err = NewLocatedError(
|
2015-09-19 18:26:46 +08:00
|
|
|
|
fmt.Sprintf("%v", r),
|
2015-10-28 02:33:17 +02:00
|
|
|
|
FieldASTsToNodeASTs(fieldASTs),
|
2015-09-19 18:26:46 +08:00
|
|
|
|
)
|
|
|
|
|
|
}
|
|
|
|
|
|
if r, ok := r.(error); ok {
|
2015-10-30 00:49:49 +02:00
|
|
|
|
err = gqlerrors.FormatError(r)
|
2015-09-19 18:26:46 +08:00
|
|
|
|
}
|
|
|
|
|
|
// send panic upstream
|
2015-10-28 02:33:17 +02:00
|
|
|
|
if _, ok := returnType.(*NonNull); ok {
|
2015-10-30 00:49:49 +02:00
|
|
|
|
panic(gqlerrors.FormatError(err))
|
2015-09-19 18:26:46 +08:00
|
|
|
|
}
|
2015-10-30 00:49:49 +02:00
|
|
|
|
eCtx.Errors = append(eCtx.Errors, gqlerrors.FormatError(err))
|
2015-09-19 21:51:32 +08:00
|
|
|
|
return result, resultState
|
2015-09-15 03:04:36 +08:00
|
|
|
|
}
|
2015-09-19 21:51:32 +08:00
|
|
|
|
return result, resultState
|
2015-09-15 03:04:36 +08:00
|
|
|
|
}()
|
2015-09-17 18:32:16 +08:00
|
|
|
|
|
2015-09-12 11:09:33 +08:00
|
|
|
|
fieldAST := fieldASTs[0]
|
2015-09-14 15:48:30 +08:00
|
|
|
|
fieldName := ""
|
|
|
|
|
|
if fieldAST.Name != nil {
|
|
|
|
|
|
fieldName = fieldAST.Name.Value
|
|
|
|
|
|
}
|
2015-09-12 11:09:33 +08:00
|
|
|
|
|
|
|
|
|
|
fieldDef := getFieldDef(eCtx.Schema, parentType, fieldName)
|
2015-09-14 09:41:13 +08:00
|
|
|
|
if fieldDef == nil {
|
2015-09-19 18:26:46 +08:00
|
|
|
|
resultState.hasNoFieldDefs = true
|
2015-09-19 21:51:32 +08:00
|
|
|
|
return nil, resultState
|
2015-09-14 09:41:13 +08:00
|
|
|
|
}
|
2015-09-19 18:26:46 +08:00
|
|
|
|
returnType = fieldDef.Type
|
2015-09-12 11:09:33 +08:00
|
|
|
|
resolveFn := fieldDef.Resolve
|
|
|
|
|
|
if resolveFn == nil {
|
|
|
|
|
|
resolveFn = defaultResolveFn
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2015-09-14 09:41:13 +08:00
|
|
|
|
// Build a map of arguments from the field.arguments AST, using the
|
2015-09-12 11:09:33 +08:00
|
|
|
|
// variables scope to fulfill any variable references.
|
|
|
|
|
|
// TODO: find a way to memoize, in case this field is within a List type.
|
2015-09-14 09:41:13 +08:00
|
|
|
|
args, _ := getArgumentValues(fieldDef.Args, fieldAST.Arguments, eCtx.VariableValues)
|
2015-09-12 11:09:33 +08:00
|
|
|
|
|
2015-09-14 09:41:13 +08:00
|
|
|
|
// The resolve function's optional third argument is a collection of
|
2015-09-12 11:09:33 +08:00
|
|
|
|
// information about the current execution state.
|
2015-10-28 02:33:17 +02:00
|
|
|
|
info := ResolveInfo{
|
2015-09-14 09:41:13 +08:00
|
|
|
|
FieldName: fieldName,
|
|
|
|
|
|
FieldASTs: fieldASTs,
|
|
|
|
|
|
ReturnType: returnType,
|
|
|
|
|
|
ParentType: parentType,
|
|
|
|
|
|
Schema: eCtx.Schema,
|
|
|
|
|
|
Fragments: eCtx.Fragments,
|
|
|
|
|
|
RootValue: eCtx.Root,
|
|
|
|
|
|
Operation: eCtx.Operation,
|
2015-09-12 11:09:33 +08:00
|
|
|
|
VariableValues: eCtx.VariableValues,
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2015-11-26 23:11:54 -05:00
|
|
|
|
var resolveFnError error
|
2015-11-26 02:17:00 -05:00
|
|
|
|
|
2015-11-26 23:11:54 -05:00
|
|
|
|
result, resolveFnError = resolveFn(ResolveParams{
|
2016-01-05 12:12:52 -05:00
|
|
|
|
Source: source,
|
|
|
|
|
|
Args: args,
|
|
|
|
|
|
Info: info,
|
|
|
|
|
|
Context: eCtx.Context,
|
2015-09-12 11:09:33 +08:00
|
|
|
|
})
|
2015-09-19 21:51:32 +08:00
|
|
|
|
|
2015-11-26 23:11:54 -05:00
|
|
|
|
if resolveFnError != nil {
|
|
|
|
|
|
panic(gqlerrors.FormatError(resolveFnError))
|
2015-11-26 02:17:00 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
2015-09-19 18:26:46 +08:00
|
|
|
|
completed := completeValueCatchingError(eCtx, returnType, fieldASTs, info, result)
|
2015-09-19 21:51:32 +08:00
|
|
|
|
return completed, resultState
|
2015-09-12 11:09:33 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2015-10-30 00:49:49 +02:00
|
|
|
|
func completeValueCatchingError(eCtx *ExecutionContext, returnType Type, fieldASTs []*ast.Field, info ResolveInfo, result interface{}) (completed interface{}) {
|
2015-09-14 22:48:41 +08:00
|
|
|
|
// catch panic
|
2015-09-19 18:26:46 +08:00
|
|
|
|
defer func() interface{} {
|
2015-09-14 22:48:41 +08:00
|
|
|
|
if r := recover(); r != nil {
|
2015-09-19 18:26:46 +08:00
|
|
|
|
//send panic upstream
|
2015-10-28 02:33:17 +02:00
|
|
|
|
if _, ok := returnType.(*NonNull); ok {
|
2015-09-19 18:26:46 +08:00
|
|
|
|
panic(r)
|
|
|
|
|
|
}
|
2015-10-30 00:49:49 +02:00
|
|
|
|
if err, ok := r.(gqlerrors.FormattedError); ok {
|
2015-09-19 18:26:46 +08:00
|
|
|
|
eCtx.Errors = append(eCtx.Errors, err)
|
|
|
|
|
|
}
|
|
|
|
|
|
return completed
|
2015-09-14 22:48:41 +08:00
|
|
|
|
}
|
2015-09-19 18:26:46 +08:00
|
|
|
|
return completed
|
2015-09-14 22:48:41 +08:00
|
|
|
|
}()
|
|
|
|
|
|
|
2015-10-28 02:33:17 +02:00
|
|
|
|
if returnType, ok := returnType.(*NonNull); ok {
|
2015-09-19 18:26:46 +08:00
|
|
|
|
completed := completeValue(eCtx, returnType, fieldASTs, info, result)
|
|
|
|
|
|
return completed
|
2015-09-14 22:48:41 +08:00
|
|
|
|
}
|
2015-09-19 18:26:46 +08:00
|
|
|
|
completed = completeValue(eCtx, returnType, fieldASTs, info, result)
|
2015-09-14 22:48:41 +08:00
|
|
|
|
resultVal := reflect.ValueOf(completed)
|
|
|
|
|
|
if resultVal.IsValid() && resultVal.Type().Kind() == reflect.Func {
|
|
|
|
|
|
if propertyFn, ok := completed.(func() interface{}); ok {
|
2015-09-19 18:26:46 +08:00
|
|
|
|
return propertyFn()
|
2015-09-14 22:48:41 +08:00
|
|
|
|
}
|
2015-10-30 00:49:49 +02:00
|
|
|
|
err := gqlerrors.NewFormattedError("Error resolving func. Expected `func() interface{}` signature")
|
|
|
|
|
|
panic(gqlerrors.FormatError(err))
|
2015-09-14 22:48:41 +08:00
|
|
|
|
}
|
2015-09-19 18:26:46 +08:00
|
|
|
|
return completed
|
2015-09-14 22:48:41 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2015-10-30 00:49:49 +02:00
|
|
|
|
func completeValue(eCtx *ExecutionContext, returnType Type, fieldASTs []*ast.Field, info ResolveInfo, result interface{}) interface{} {
|
2015-09-14 22:48:41 +08:00
|
|
|
|
|
|
|
|
|
|
resultVal := reflect.ValueOf(result)
|
|
|
|
|
|
if resultVal.IsValid() && resultVal.Type().Kind() == reflect.Func {
|
|
|
|
|
|
if propertyFn, ok := result.(func() interface{}); ok {
|
2015-09-19 18:26:46 +08:00
|
|
|
|
return propertyFn()
|
2015-09-14 22:48:41 +08:00
|
|
|
|
}
|
2015-10-30 00:49:49 +02:00
|
|
|
|
err := gqlerrors.NewFormattedError("Error resolving func. Expected `func() interface{}` signature")
|
|
|
|
|
|
panic(gqlerrors.FormatError(err))
|
2015-09-14 22:48:41 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2016-05-31 00:02:29 +08:00
|
|
|
|
// If field type is NonNull, complete for inner type, and throw field error
|
|
|
|
|
|
// if result is null.
|
2015-10-28 02:33:17 +02:00
|
|
|
|
if returnType, ok := returnType.(*NonNull); ok {
|
2015-09-19 18:26:46 +08:00
|
|
|
|
completed := completeValue(eCtx, returnType.OfType, fieldASTs, info, result)
|
2015-09-14 22:48:41 +08:00
|
|
|
|
if completed == nil {
|
2015-10-28 02:33:17 +02:00
|
|
|
|
err := NewLocatedError(
|
2015-09-17 18:32:16 +08:00
|
|
|
|
fmt.Sprintf("Cannot return null for non-nullable field %v.%v.", info.ParentType, info.FieldName),
|
2015-10-28 02:33:17 +02:00
|
|
|
|
FieldASTsToNodeASTs(fieldASTs),
|
2015-09-17 18:32:16 +08:00
|
|
|
|
)
|
2015-10-30 00:49:49 +02:00
|
|
|
|
panic(gqlerrors.FormatError(err))
|
2015-09-14 22:48:41 +08:00
|
|
|
|
}
|
2015-09-19 18:26:46 +08:00
|
|
|
|
return completed
|
2015-09-14 22:48:41 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2016-05-31 00:02:29 +08:00
|
|
|
|
// If result value is null-ish (null, undefined, or NaN) then return null.
|
2015-09-17 18:32:16 +08:00
|
|
|
|
if isNullish(result) {
|
2015-09-19 18:26:46 +08:00
|
|
|
|
return nil
|
2015-09-14 22:48:41 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// If field type is List, complete each item in the list with the inner type
|
2015-10-28 02:33:17 +02:00
|
|
|
|
if returnType, ok := returnType.(*List); ok {
|
2016-05-30 11:20:07 +08:00
|
|
|
|
return completeListValue(eCtx, returnType, fieldASTs, info, result)
|
2015-09-14 22:48:41 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2016-05-31 00:02:29 +08:00
|
|
|
|
// If field type is a leaf type, Scalar or Enum, serialize to a valid value,
|
|
|
|
|
|
// returning null if serialization is not possible.
|
2015-10-28 02:33:17 +02:00
|
|
|
|
if returnType, ok := returnType.(*Scalar); ok {
|
2016-05-31 09:41:14 +08:00
|
|
|
|
return completeLeafValue(returnType, result)
|
2015-09-14 22:48:41 +08:00
|
|
|
|
}
|
2015-10-28 02:33:17 +02:00
|
|
|
|
if returnType, ok := returnType.(*Enum); ok {
|
2016-05-31 00:04:05 +08:00
|
|
|
|
return completeLeafValue(returnType, result)
|
2015-09-14 22:48:41 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2016-05-31 00:02:29 +08:00
|
|
|
|
// If field type is an abstract type, Interface or Union, determine the
|
|
|
|
|
|
// runtime Object type and complete for that type.
|
2016-05-30 12:02:30 +08:00
|
|
|
|
if returnType, ok := returnType.(Abstract); ok {
|
|
|
|
|
|
return completeAbstractValue(eCtx, returnType, fieldASTs, info, result)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2016-05-31 00:02:29 +08:00
|
|
|
|
// If field type is Object, execute and complete all sub-selections.
|
|
|
|
|
|
if returnType, ok := returnType.(*Object); ok {
|
|
|
|
|
|
return completeObjectValue(eCtx, returnType, fieldASTs, info, result)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Not reachable. All possible output types have been considered.
|
2016-05-30 12:06:06 +08:00
|
|
|
|
err := invariant(false,
|
|
|
|
|
|
fmt.Sprintf(`Cannot complete value of unexpected type "%v."`, returnType),
|
|
|
|
|
|
)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
panic(gqlerrors.FormatError(err))
|
|
|
|
|
|
}
|
2016-05-30 12:02:30 +08:00
|
|
|
|
return nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2016-05-31 00:02:29 +08:00
|
|
|
|
// completeAbstractValue completes value of an Abstract type (Union / Interface) by determining the runtime type
|
2016-05-30 12:02:30 +08:00
|
|
|
|
// of that value, then completing based on that type.
|
|
|
|
|
|
func completeAbstractValue(eCtx *ExecutionContext, returnType Abstract, fieldASTs []*ast.Field, info ResolveInfo, result interface{}) interface{} {
|
|
|
|
|
|
|
2016-04-06 17:23:31 +08:00
|
|
|
|
var runtimeType *Object
|
2016-05-30 11:56:34 +08:00
|
|
|
|
|
2016-05-31 09:44:26 +08:00
|
|
|
|
if unionReturnType, ok := returnType.(*Union); ok && unionReturnType.ResolveType != nil {
|
|
|
|
|
|
runtimeType = unionReturnType.ResolveType(result, info)
|
|
|
|
|
|
} else if interfaceReturnType, ok := returnType.(*Interface); ok && interfaceReturnType.ResolveType != nil {
|
|
|
|
|
|
runtimeType = interfaceReturnType.ResolveType(result, info)
|
|
|
|
|
|
} else {
|
|
|
|
|
|
runtimeType = defaultResolveTypeFn(result, info, returnType)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2016-05-31 12:16:48 +08:00
|
|
|
|
if runtimeType == nil {
|
|
|
|
|
|
return nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2016-05-31 09:44:26 +08:00
|
|
|
|
if runtimeType != nil && !returnType.IsPossibleType(runtimeType) {
|
|
|
|
|
|
panic(gqlerrors.NewFormattedError(
|
|
|
|
|
|
fmt.Sprintf(`Runtime Object type "%v" is not a possible type `+
|
|
|
|
|
|
`for "%v".`, runtimeType, returnType),
|
|
|
|
|
|
))
|
2015-09-14 22:48:41 +08:00
|
|
|
|
}
|
2016-05-30 11:56:34 +08:00
|
|
|
|
|
|
|
|
|
|
return completeObjectValue(eCtx, runtimeType, fieldASTs, info, result)
|
|
|
|
|
|
}
|
2016-05-31 00:02:29 +08:00
|
|
|
|
|
|
|
|
|
|
// completeObjectValue complete an Object value by executing all sub-selections.
|
2016-05-30 11:56:34 +08:00
|
|
|
|
func completeObjectValue(eCtx *ExecutionContext, returnType *Object, fieldASTs []*ast.Field, info ResolveInfo, result interface{}) interface{} {
|
|
|
|
|
|
|
2015-09-14 22:48:41 +08:00
|
|
|
|
// If there is an isTypeOf predicate function, call it with the
|
|
|
|
|
|
// current result. If isTypeOf returns false, then raise an error rather
|
|
|
|
|
|
// than continuing execution.
|
2016-05-30 11:56:34 +08:00
|
|
|
|
if returnType.IsTypeOf != nil && !returnType.IsTypeOf(result, info) {
|
2015-10-30 00:49:49 +02:00
|
|
|
|
panic(gqlerrors.NewFormattedError(
|
2016-05-30 11:56:34 +08:00
|
|
|
|
fmt.Sprintf(`Expected value of type "%v" but got: %T.`, returnType, result),
|
2015-09-16 21:25:42 +08:00
|
|
|
|
))
|
2015-09-14 22:48:41 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Collect sub-fields to execute to complete this value.
|
2015-10-30 00:49:49 +02:00
|
|
|
|
subFieldASTs := map[string][]*ast.Field{}
|
2015-09-14 22:48:41 +08:00
|
|
|
|
visitedFragmentNames := map[string]bool{}
|
|
|
|
|
|
for _, fieldAST := range fieldASTs {
|
|
|
|
|
|
if fieldAST == nil {
|
|
|
|
|
|
continue
|
|
|
|
|
|
}
|
|
|
|
|
|
selectionSet := fieldAST.SelectionSet
|
|
|
|
|
|
if selectionSet != nil {
|
|
|
|
|
|
innerParams := CollectFieldsParams{
|
|
|
|
|
|
ExeContext: eCtx,
|
2016-05-30 11:56:34 +08:00
|
|
|
|
RuntimeType: returnType,
|
2015-09-14 22:48:41 +08:00
|
|
|
|
SelectionSet: selectionSet,
|
|
|
|
|
|
Fields: subFieldASTs,
|
|
|
|
|
|
VisitedFragmentNames: visitedFragmentNames,
|
|
|
|
|
|
}
|
|
|
|
|
|
subFieldASTs = collectFields(innerParams)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
executeFieldsParams := ExecuteFieldsParams{
|
|
|
|
|
|
ExecutionContext: eCtx,
|
2016-05-30 11:56:34 +08:00
|
|
|
|
ParentType: returnType,
|
2015-09-16 01:50:58 +08:00
|
|
|
|
Source: result,
|
2015-09-14 22:48:41 +08:00
|
|
|
|
Fields: subFieldASTs,
|
|
|
|
|
|
}
|
2015-09-19 18:26:46 +08:00
|
|
|
|
results := executeFields(executeFieldsParams)
|
2015-09-19 21:51:32 +08:00
|
|
|
|
|
2015-09-19 18:26:46 +08:00
|
|
|
|
return results.Data
|
2015-09-14 22:48:41 +08:00
|
|
|
|
|
|
|
|
|
|
}
|
2016-05-30 11:42:00 +08:00
|
|
|
|
|
|
|
|
|
|
// completeLeafValue complete a leaf value (Scalar / Enum) by serializing to a valid value, returning nil if serialization is not possible.
|
2016-05-31 00:04:05 +08:00
|
|
|
|
func completeLeafValue(returnType Leaf, result interface{}) interface{} {
|
2016-05-30 11:35:16 +08:00
|
|
|
|
serializedResult := returnType.Serialize(result)
|
|
|
|
|
|
if isNullish(serializedResult) {
|
|
|
|
|
|
return nil
|
|
|
|
|
|
}
|
|
|
|
|
|
return serializedResult
|
|
|
|
|
|
}
|
2015-09-16 11:42:48 +08:00
|
|
|
|
|
2016-05-30 11:42:00 +08:00
|
|
|
|
// completeListValue complete a list value by completing each item in the list with the inner type
|
2016-05-30 11:20:07 +08:00
|
|
|
|
func completeListValue(eCtx *ExecutionContext, returnType *List, fieldASTs []*ast.Field, info ResolveInfo, result interface{}) interface{} {
|
|
|
|
|
|
resultVal := reflect.ValueOf(result)
|
|
|
|
|
|
parentTypeName := ""
|
|
|
|
|
|
if info.ParentType != nil {
|
|
|
|
|
|
parentTypeName = info.ParentType.Name()
|
|
|
|
|
|
}
|
|
|
|
|
|
err := invariant(
|
|
|
|
|
|
resultVal.IsValid() && resultVal.Type().Kind() == reflect.Slice,
|
|
|
|
|
|
fmt.Sprintf("User Error: expected iterable, but did not find one "+
|
|
|
|
|
|
"for field %v.%v.", parentTypeName, info.FieldName),
|
|
|
|
|
|
)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
panic(gqlerrors.FormatError(err))
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
itemType := returnType.OfType
|
|
|
|
|
|
completedResults := []interface{}{}
|
|
|
|
|
|
for i := 0; i < resultVal.Len(); i++ {
|
|
|
|
|
|
val := resultVal.Index(i).Interface()
|
|
|
|
|
|
completedItem := completeValueCatchingError(eCtx, itemType, fieldASTs, info, val)
|
|
|
|
|
|
completedResults = append(completedResults, completedItem)
|
|
|
|
|
|
}
|
|
|
|
|
|
return completedResults
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2016-05-31 09:44:26 +08:00
|
|
|
|
// defaultResolveTypeFn If a resolveType function is not given, then a default resolve behavior is
|
|
|
|
|
|
// used which tests each possible type for the abstract type by calling
|
|
|
|
|
|
// isTypeOf for the object being coerced, returning the first type that matches.
|
|
|
|
|
|
func defaultResolveTypeFn(value interface{}, info ResolveInfo, abstractType Abstract) *Object {
|
|
|
|
|
|
possibleTypes := abstractType.PossibleTypes()
|
|
|
|
|
|
for _, possibleType := range possibleTypes {
|
|
|
|
|
|
if possibleType.IsTypeOf == nil {
|
|
|
|
|
|
continue
|
|
|
|
|
|
}
|
|
|
|
|
|
if res := possibleType.IsTypeOf(value, info); res {
|
|
|
|
|
|
return possibleType
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// defaultResolveFn If a resolve function is not given, then a default resolve behavior is used
|
|
|
|
|
|
// which takes the property of the source object of the same name as the field
|
|
|
|
|
|
// and returns it as the result, or if it's a function, returns the result
|
|
|
|
|
|
// of calling that function.
|
2015-11-25 18:23:57 -05:00
|
|
|
|
func defaultResolveFn(p ResolveParams) (interface{}, error) {
|
2015-09-19 21:51:32 +08:00
|
|
|
|
// try to resolve p.Source as a struct first
|
|
|
|
|
|
sourceVal := reflect.ValueOf(p.Source)
|
2015-09-30 17:03:31 +08:00
|
|
|
|
if sourceVal.IsValid() && sourceVal.Type().Kind() == reflect.Ptr {
|
2015-09-19 21:51:32 +08:00
|
|
|
|
sourceVal = sourceVal.Elem()
|
|
|
|
|
|
}
|
2015-10-01 00:21:14 +08:00
|
|
|
|
if !sourceVal.IsValid() {
|
2015-11-25 18:23:57 -05:00
|
|
|
|
return nil, nil
|
2015-10-01 00:21:14 +08:00
|
|
|
|
}
|
|
|
|
|
|
if sourceVal.Type().Kind() == reflect.Struct {
|
2015-09-19 21:51:32 +08:00
|
|
|
|
// find field based on struct's json tag
|
2015-10-27 15:03:56 +02:00
|
|
|
|
// we could potentially create a custom `graphql` tag, but its unnecessary at this point
|
2015-09-19 21:51:32 +08:00
|
|
|
|
// since graphql speaks to client in a json-like way anyway
|
|
|
|
|
|
// so json tags are a good way to start with
|
|
|
|
|
|
for i := 0; i < sourceVal.NumField(); i++ {
|
|
|
|
|
|
valueField := sourceVal.Field(i)
|
|
|
|
|
|
typeField := sourceVal.Type().Field(i)
|
2015-09-26 23:14:45 +08:00
|
|
|
|
// try matching the field name first
|
|
|
|
|
|
if typeField.Name == p.Info.FieldName {
|
2015-11-25 18:23:57 -05:00
|
|
|
|
return valueField.Interface(), nil
|
2015-09-26 23:14:45 +08:00
|
|
|
|
}
|
2015-09-19 21:51:32 +08:00
|
|
|
|
tag := typeField.Tag
|
|
|
|
|
|
jsonTag := tag.Get("json")
|
|
|
|
|
|
jsonOptions := strings.Split(jsonTag, ",")
|
|
|
|
|
|
if len(jsonOptions) == 0 {
|
|
|
|
|
|
continue
|
|
|
|
|
|
}
|
|
|
|
|
|
if jsonOptions[0] != p.Info.FieldName {
|
|
|
|
|
|
continue
|
|
|
|
|
|
}
|
2015-11-25 18:23:57 -05:00
|
|
|
|
return valueField.Interface(), nil
|
2015-09-19 21:51:32 +08:00
|
|
|
|
}
|
2015-11-25 18:23:57 -05:00
|
|
|
|
return nil, nil
|
2015-09-19 21:51:32 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// try p.Source as a map[string]interface
|
2015-09-16 11:42:48 +08:00
|
|
|
|
if sourceMap, ok := p.Source.(map[string]interface{}); ok {
|
|
|
|
|
|
property := sourceMap[p.Info.FieldName]
|
|
|
|
|
|
val := reflect.ValueOf(property)
|
|
|
|
|
|
if val.IsValid() && val.Type().Kind() == reflect.Func {
|
|
|
|
|
|
// 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 {
|
2015-11-25 18:23:57 -05:00
|
|
|
|
return propertyFn(), nil
|
2015-09-16 11:42:48 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2015-11-25 18:23:57 -05:00
|
|
|
|
return property, nil
|
2015-09-16 11:42:48 +08:00
|
|
|
|
}
|
2015-09-19 21:51:32 +08:00
|
|
|
|
|
2015-09-26 23:14:45 +08:00
|
|
|
|
// last resort, return nil
|
2015-11-25 18:23:57 -05:00
|
|
|
|
return nil, nil
|
2015-09-16 11:42:48 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2016-04-15 18:02:57 +08:00
|
|
|
|
// This method looks up the field on the given type defintion.
|
|
|
|
|
|
// It has special casing for the two introspection fields, __schema
|
|
|
|
|
|
// and __typename. __typename is special because it can always be
|
|
|
|
|
|
// queried as a field, even in situations where no other fields
|
|
|
|
|
|
// are allowed, like on a Union. __schema could get automatically
|
|
|
|
|
|
// added to the query type, but that would require mutating type
|
|
|
|
|
|
// definitions, which would cause issues.
|
2015-10-28 02:33:17 +02:00
|
|
|
|
func getFieldDef(schema Schema, parentType *Object, fieldName string) *FieldDefinition {
|
2015-09-16 11:42:48 +08:00
|
|
|
|
|
|
|
|
|
|
if parentType == nil {
|
|
|
|
|
|
return nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2015-10-28 02:33:17 +02:00
|
|
|
|
if fieldName == SchemaMetaFieldDef.Name &&
|
2015-11-07 16:01:14 -08:00
|
|
|
|
schema.QueryType() == parentType {
|
2015-10-28 02:33:17 +02:00
|
|
|
|
return SchemaMetaFieldDef
|
2015-09-16 11:42:48 +08:00
|
|
|
|
}
|
2015-10-28 02:33:17 +02:00
|
|
|
|
if fieldName == TypeMetaFieldDef.Name &&
|
2015-11-07 16:01:14 -08:00
|
|
|
|
schema.QueryType() == parentType {
|
2015-10-28 02:33:17 +02:00
|
|
|
|
return TypeMetaFieldDef
|
2015-09-16 11:42:48 +08:00
|
|
|
|
}
|
2015-10-28 02:33:17 +02:00
|
|
|
|
if fieldName == TypeNameMetaFieldDef.Name {
|
|
|
|
|
|
return TypeNameMetaFieldDef
|
2015-09-16 11:42:48 +08:00
|
|
|
|
}
|
2015-11-07 16:01:14 -08:00
|
|
|
|
return parentType.Fields()[fieldName]
|
2015-09-16 11:42:48 +08:00
|
|
|
|
}
|