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);
        }
        public async Task InvokeAsync(IQueryContext context)
        {
            if (IsContextIncomplete(context))
            {
                context.Result = QueryResult.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 &&
                DoHashesMatch(context, _hashName))
            {
                // save the  query
                await _writeStoredQueries.WriteQueryAsync(
                    context.QueryKey,
                    context.Request.Query)
                .ConfigureAwait(false);

                // add persistence receipt to the result
                if (context.Result is QueryResult result &&
                    context.Request.Extensions.TryGetValue(
                        _persistedQuery, out var s) &&
                    s is IReadOnlyDictionary <string, object> settings &&
                    settings.TryGetValue(_hashName, out object h) &&
                    h is string hash)
                {
                    result.Extensions[_persistedQuery] =
                        new Dictionary <string, object>
                    {
                        { _hashName, hash },
                        { _persisted, true }
                    };
                }

                context.ContextData[ContextDataKeys.DocumentSaved] = true;
            }

            await _next(context).ConfigureAwait(false);
        }