public async Task InvokeAsync(IQueryContext context)
        {
            if (IsContextIncomplete(context))
            {
                context.Result = QueryResultBuilder.CreateError(
                    ErrorBuilder.New()
                    .SetMessage(CoreResources.Write_PQ_Middleware_Incomplete)
                    .SetCode(ErrorCodes.Execution.Incomplete).Build());
                return;
            }

            if (_writeStoredQueries != null &&
                context.Request.Query != null &&
                context.QueryKey != null &&
                context.Result is IReadOnlyQueryResult result &&
                context.Request.Extensions != null &&
                context.Request.Extensions.TryGetValue(_persistedQuery, out var s) &&
                s is IReadOnlyDictionary <string, object> settings)
            {
                IQueryResultBuilder builder = QueryResultBuilder.FromResult(result);

                // hash is found and matches the query key -> store the query
                if (DoHashesMatch(settings, context.QueryKey, _hashName, out string userHash))
                {
                    // save the  query
                    await _writeStoredQueries.WriteQueryAsync(
                        context.QueryKey,
                        context.Request.Query)
                    .ConfigureAwait(false);

                    // add persistence receipt to the result
                    builder.SetExtension(
                        _persistedQuery,
                        new Dictionary <string, object>
                    {
                        { _hashName, userHash },
                        { _persisted, true }
                    });

                    context.ContextData[ContextDataKeys.DocumentSaved] = true;
                }
                else
                {
                    builder.SetExtension(
                        _persistedQuery,
                        new Dictionary <string, object>
                    {
                        { _hashName, userHash },
                        { _expectedValue, context.QueryKey },
                        { _expectedType, _hashName },
                        { _expectedFormat, _hashFormat.ToString() },
                        { _persisted, false }
                    });
                }

                context.Result = builder.Create();
            }

            await _next(context).ConfigureAwait(false);
        }
Пример #2
0
 /// <inheritdoc />
 public IConversion SetHashFormat(HashFormat hashFormat)
 {
     _hashFormat = $"-hash {hashFormat.ToString()} ";
     return(this);
 }