Exemplo n.º 1
0
 public ResolveEventStreamContext(ResolveEventStreamContext context)
 {
     Source            = (T)context.Source;
     FieldName         = context.FieldName;
     FieldAst          = context.FieldAst;
     FieldDefinition   = context.FieldDefinition;
     ReturnType        = context.ReturnType;
     ParentType        = context.ParentType;
     Arguments         = context.Arguments;
     Schema            = context.Schema;
     Document          = context.Document;
     Fragments         = context.Fragments;
     RootValue         = context.RootValue;
     UserContext       = context.UserContext;
     Operation         = context.Operation;
     Variables         = context.Variables;
     CancellationToken = context.CancellationToken;
     Metrics           = context.Metrics;
     Errors            = context.Errors;
 }
Exemplo n.º 2
0
        private ResolveEventStreamResult ResolveEventStream(
            ExecutionContext context,
            IObjectGraphType parentType,
            object source,
            Field field,
            IEnumerable <string> path)
        {
            context.CancellationToken.ThrowIfCancellationRequested();

            var fieldPath = path?.ToList() ?? new List <string>();

            var resolveResult = new ResolveEventStreamResult
            {
                Skip = false
            };

            if (!(GetFieldDefinition(context.Document, context.Schema, parentType, field) is EventStreamFieldType fieldDefinition))
            {
                resolveResult.Skip = true;
                return(resolveResult);
            }

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

            try
            {
                var resolveContext = new ResolveEventStreamContext();
                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;
                resolveContext.Errors            = context.Errors;
                resolveContext.Path = fieldPath;

                if (fieldDefinition.Subscriber == null)
                {
                    return(GenerateError(resolveResult, field, context,
                                         new InvalidOperationException($"Subscriber not set for field {field.Name}"),
                                         fieldPath));
                }

                var result = fieldDefinition.Subscriber.Subscribe(resolveContext);

                var valueTransformer = result
                                       .SelectMany(async value =>
                {
                    var fieldResolveResult = await ResolveFieldAsync(context, parentType, value, field, fieldPath);
                    return(new ExecutionResult
                    {
                        Data = fieldResolveResult.Value
                    });
                })
                                       .Catch <ExecutionResult, Exception>(exception =>
                                                                           Observable.Return(
                                                                               new ExecutionResult
                {
                    Errors = new ExecutionErrors
                    {
                        new ExecutionError(
                            $"Could not subscribe to field '{field.Name}' in query '{context.Document.OriginalQuery}'",
                            exception)
                        {
                            Path = path
                        }
                    }
                }));

                resolveResult.Value = valueTransformer;
                return(resolveResult);
            }
            catch (Exception exc)
            {
                return(GenerateError(resolveResult, field, context, exc, path));
            }
        }