Пример #1
0
        public async Task InvokeAsync(IMiddlewareContext context)
        {
            DelegateDirective delegateDirective = context.Field
                                                  .Directives[DirectiveNames.Delegate]
                                                  .FirstOrDefault()?.ToObject <DelegateDirective>();

            if (delegateDirective != null)
            {
                IImmutableStack <SelectionPathComponent> path =
                    delegateDirective.Path is null
                    ? ImmutableStack <SelectionPathComponent> .Empty
                    : SelectionPathParser.Parse(delegateDirective.Path);

                IReadOnlyQueryRequest request =
                    CreateQuery(context, delegateDirective.Schema, path);

                IReadOnlyQueryResult result = await ExecuteQueryAsync(
                    context, request, delegateDirective.Schema)
                                              .ConfigureAwait(false);

                UpdateContextData(context, result, delegateDirective);

                context.Result = new SerializedData(
                    ExtractData(result.Data, path.Count()));
                ReportErrors(context, result.Errors);
            }

            await _next.Invoke(context).ConfigureAwait(false);
        }
 private void UpdateContextData(
     IResolverContext context,
     IReadOnlyQueryResult result,
     DelegateDirective delegateDirective)
 {
     if (result.ContextData.Count > 0)
     {
         ImmutableDictionary <string, object> .Builder builder =
             ImmutableDictionary.CreateBuilder <string, object>();
         builder.AddRange(context.ScopedContextData);
         builder[WellKnownProperties.SchemaName] = delegateDirective.Schema;
         builder.AddRange(result.ContextData);
         context.ScopedContextData = builder.ToImmutableDictionary();
     }
     else
     {
         context.ModifyScopedContext(c => c.SetItem(
                                         WellKnownProperties.SchemaName,
                                         delegateDirective.Schema));
     }
 }