public async Task <object> ResolveField(ExecutionContext context, ObjectGraphType parentType, object source, Fields fields)
        {
            context.CancellationToken.ThrowIfCancellationRequested();

            var field = fields.First();

            var fieldDefinition = GetFieldDefinition(context.Schema, parentType, field);

            if (fieldDefinition == null)
            {
                return(null);
            }

            var arguments = GetArgumentValues(context.Schema, fieldDefinition.Arguments, field.Arguments, context.Variables);

            Func <ResolveFieldContext, object> defaultResolve = (ctx) =>
            {
                return(ctx.Source != null?GetProperyValue(ctx.Source, ctx.FieldAst.Name) : null);
            };

            try
            {
                var resolveContext = new ResolveFieldContext();
                resolveContext.FieldName         = field.Name;
                resolveContext.FieldAst          = field;
                resolveContext.FieldDefinition   = fieldDefinition;
                resolveContext.ReturnType        = context.Schema.FindType(fieldDefinition.Type);
                resolveContext.ParentType        = parentType;
                resolveContext.Arguments         = arguments;
                resolveContext.Source            = source;
                resolveContext.Schema            = context.Schema;
                resolveContext.Fragments         = context.Fragments;
                resolveContext.RootValue         = context.RootValue;
                resolveContext.Operation         = context.Operation;
                resolveContext.Variables         = context.Variables;
                resolveContext.CancellationToken = context.CancellationToken;
                var resolve = fieldDefinition.Resolve ?? defaultResolve;
                var result  = resolve(resolveContext);

                if (result is Task)
                {
                    var   task = result as Task;
                    await task;

                    result = GetProperyValue(task, "Result");
                }

                if (parentType is __Field && result is Type)
                {
                    result = context.Schema.FindType(result as Type);
                }

                return(await CompleteValue(context, context.Schema.FindType(fieldDefinition.Type), fields, result));
            }
            catch (Exception exc)
            {
                context.Errors.Add(new ExecutionError("Error trying to resolve {0}.".ToFormat(field.Name), exc));
                return(null);
            }
        }
        public object ResolveField(ExecutionContext context, ObjectGraphType parentType, object source, Fields fields)
        {
            var field = fields.First();

            var fieldDefinition = GetFieldDefinition(context.Schema, parentType, field);

            if (fieldDefinition == null)
            {
                return(null);
            }

            var arguments = GetArgumentValues(fieldDefinition.Arguments, field.Arguments, context.Variables);

            Func <ResolveFieldContext, object> defaultResolve = (ctx) =>
            {
                return(ctx.Source != null?GetProperyValue(ctx.Source, ctx.FieldAst.Name) : null);
            };

            try
            {
                var resolveContext = new ResolveFieldContext();
                resolveContext.FieldAst        = field;
                resolveContext.FieldDefinition = fieldDefinition;
                resolveContext.Schema          = context.Schema;
                resolveContext.ParentType      = parentType;
                resolveContext.Arguments       = arguments;
                resolveContext.Source          = source;
                var resolve = fieldDefinition.Resolve ?? defaultResolve;
                var result  = resolve(resolveContext);
                return(CompleteValue(context, fieldDefinition.Type, fields, result));
            }
            catch (Exception exc)
            {
                context.Errors.Add(new ExecutionError(string.Format("Error trying to resolve {0}.", field.Name), exc));
                return(null);
            }
        }
        public async Task <ResolveFieldResult <object> > ResolveFieldAsync(ExecutionContext context, IObjectGraphType parentType, object source, Fields fields)
        {
            context.CancellationToken.ThrowIfCancellationRequested();

            var resolveResult = new ResolveFieldResult <object>
            {
                Skip = false
            };

            var field = fields.First();

            var fieldDefinition = GetFieldDefinition(context.Schema, parentType, field);

            if (fieldDefinition == null)
            {
                resolveResult.Skip = true;
                return(resolveResult);
            }

            var arguments = GetArgumentValues(context.Schema, fieldDefinition.Arguments, field.Arguments, context.Variables);

            try
            {
                var resolveContext = new ResolveFieldContext();
                resolveContext.FieldName         = field.Name;
                resolveContext.FieldAst          = field;
                resolveContext.FieldDefinition   = fieldDefinition;
                resolveContext.ReturnType        = fieldDefinition.ResolvedType;
                resolveContext.ParentType        = parentType;
                resolveContext.Arguments         = arguments;
                resolveContext.Source            = source;
                resolveContext.Schema            = context.Schema;
                resolveContext.Document          = context.Document;
                resolveContext.Fragments         = context.Fragments;
                resolveContext.RootValue         = context.RootValue;
                resolveContext.UserContext       = context.UserContext;
                resolveContext.Operation         = context.Operation;
                resolveContext.Variables         = context.Variables;
                resolveContext.CancellationToken = context.CancellationToken;
                resolveContext.Metrics           = context.Metrics;

                var resolver = fieldDefinition.Resolver ?? new NameFieldResolver();
                var result   = resolver.Resolve(resolveContext);

                if (result is Task)
                {
                    var task = result as Task;
                    if (task.IsFaulted)
                    {
                        var aggregateException = task.Exception;
                        var exception          = aggregateException.InnerExceptions.Count == 1
                            ? aggregateException.InnerException
                            : aggregateException;
                        return(GenerateError(resolveResult, field, context, exception));
                    }
                    await task.ConfigureAwait(false);

                    result = task.GetProperyValue("Result");
                }

                resolveResult.Value =
                    await CompleteValueAsync(context, fieldDefinition.ResolvedType, fields, result).ConfigureAwait(false);

                return(resolveResult);
            }
            catch (Exception exc)
            {
                return(GenerateError(resolveResult, field, context, exc));
            }
        }
Beispiel #4
0
        public async Task <ResolveFieldResult <object> > ResolveFieldAsync(ExecutionContext context, IObjectGraphType parentType, object source, Fields fields)
        {
            context.CancellationToken.ThrowIfCancellationRequested();

            var resolveResult = new ResolveFieldResult <object>
            {
                Skip = false
            };

            var field = fields.First();

            var fieldDefinition = GetFieldDefinition(context.Schema, parentType, field);

            if (fieldDefinition == null)
            {
                resolveResult.Skip = true;
                return(resolveResult);
            }

            var arguments = GetArgumentValues(context.Schema, fieldDefinition.Arguments, field.Arguments, context.Variables);

            try
            {
                var resolveContext = new ResolveFieldContext();
                resolveContext.FieldName         = field.Name;
                resolveContext.FieldAst          = field;
                resolveContext.FieldDefinition   = fieldDefinition;
                resolveContext.ReturnType        = context.Schema.FindType(fieldDefinition.Type);
                resolveContext.ParentType        = parentType;
                resolveContext.Arguments         = arguments;
                resolveContext.Source            = source;
                resolveContext.Schema            = context.Schema;
                resolveContext.Fragments         = context.Fragments;
                resolveContext.RootValue         = context.RootValue;
                resolveContext.Operation         = context.Operation;
                resolveContext.Variables         = context.Variables;
                resolveContext.CancellationToken = context.CancellationToken;
                var resolver = fieldDefinition.Resolver ?? new NameFieldResolver();
                var result   = resolver.Resolve(resolveContext);

                if (result is Task)
                {
                    var task = result as Task;
                    await task.ConfigureAwait(false);

                    result = GetProperyValue(task, "Result");
                }

                if (parentType is __Field && result is Type)
                {
                    result = context.Schema.FindType(result as Type);
                }

                resolveResult.Value = await CompleteValueAsync(context, context.Schema.FindType(fieldDefinition.Type), fields, result).ConfigureAwait(false);

                return(resolveResult);
            }
            catch (Exception exc)
            {
                var error = new ExecutionError("Error trying to resolve {0}.".ToFormat(field.Name), exc);
                error.AddLocation(field.SourceLocation.Line, field.SourceLocation.Column);
                context.Errors.Add(error);
                resolveResult.Skip = false;
                return(resolveResult);
            }
        }