Ejemplo n.º 1
0
        public ExecutionContext(
            ISchema schema,
            IOperation operation,
            IRequestContext requestContext,
            CancellationToken requestAborted)
        {
            Schema = schema ??
                     throw new ArgumentNullException(nameof(schema));
            Operation = operation ??
                        throw new ArgumentNullException(nameof(operation));
            _requestContext = requestContext
                              ?? throw new ArgumentNullException(nameof(requestContext));

            RequestAborted = requestAborted;

            _cachedQuery = _requestContext.CachedQuery;

            ErrorHandler = requestContext.ServiceScope.ServiceProvider
                           .GetRequiredService <IErrorHandler>();

            Result = new QueryResult();

            var fragments = new FragmentCollection(
                schema, operation.Document);

            Converter = _requestContext.ServiceScope
                        .ServiceProvider.GetTypeConversion();

            _fieldCollector = new FieldCollector(
                fragments, requestContext.ResolveMiddleware, Converter);

            Activator = new Activator(
                requestContext.ServiceScope.ServiceProvider);
        }
        public async Task InvokeAsync(IQueryContext context)
        {
            if (IsContextIncomplete(context))
            {
                context.Result = QueryResultBuilder.CreateError(
                    ErrorBuilder.New()
                    .SetMessage(CoreResources.ParseQueryMiddleware_InComplete)
                    .SetCode(ErrorCodes.Execution.Incomplete)
                    .Build());
            }
            else
            {
                Activity activity = _diagnosticEvents.BeginParsing(context);

                try
                {
                    bool         documentRetrievedFromCache = true;
                    ICachedQuery cachedQuery = null;
                    string       queryKey    = ResolveQueryKey(context.Request);

                    if (context.Request.Query is null)
                    {
                        _queryCache.TryGet(queryKey, out cachedQuery);
                    }
                    else
                    {
                        cachedQuery = _queryCache.GetOrCreate(
                            queryKey,
                            () =>
                        {
                            documentRetrievedFromCache = false;
                            DocumentNode document      =
                                ParseDocument(context.Request.Query);
                            return(new CachedQuery(queryKey, document));
                        });
                    }

                    // update context
                    context.QueryKey = queryKey;

                    if (cachedQuery != null)
                    {
                        context.CachedQuery = cachedQuery;
                        context.Document    = cachedQuery.Document;
                        context.ContextData[ContextDataKeys.DocumentCached] =
                            documentRetrievedFromCache;
                    }
                }
                finally
                {
                    _diagnosticEvents.EndParsing(activity, context);
                }

                await _next(context).ConfigureAwait(false);
            }
        }
Ejemplo n.º 3
0
        public async Task InvokeAsync(IQueryContext context)
        {
            if (IsContextIncomplete(context))
            {
                context.Result = QueryResult.CreateError(
                    ErrorBuilder.New()
                    .SetMessage(CoreResources.Read_PQ_Middleware_Incomplete)
                    .SetCode(ErrorCodes.Execution.Incomplete)
                    .Build());
                return;
            }

            if (context.Document is null)
            {
                ICachedQuery cachedQuery = null;
                IQuery       query       =
                    await _readStoredQueries.TryReadQueryAsync(context.QueryKey)
                    .ConfigureAwait(false);

                if (query == null)
                {
                    context.Result = QueryResult.CreateError(
                        _errorHandler.Handle(ErrorBuilder.New()
                                             .SetMessage(CoreResources.Read_PQ_Middleware_QueryNotFound)
                                             .SetCode(ErrorCodes.Execution.QueryNotFound)
                                             .Build()));
                    return;
                }

                cachedQuery = _queryCache.GetOrCreate(
                    context.QueryKey,
                    () =>
                {
                    DocumentNode document =
                        ParseDocument(query);
                    return(new CachedQuery(context.QueryKey, document));
                });

                // update context
                context.CachedQuery = cachedQuery;
                context.Document    = context.CachedQuery.Document;
                context.ContextData[ContextDataKeys.DocumentCached] = true;
            }

            await _next(context).ConfigureAwait(false);
        }
Ejemplo n.º 4
0
 public RequestContext(
     IRequestServiceScope serviceScope,
     Func <ObjectField, FieldNode, FieldDelegate> middlewareFactory,
     ICachedQuery cachedQuery,
     IDictionary <string, object> contextData,
     QueryExecutionDiagnostics diagnostics)
 {
     ServiceScope = serviceScope
                    ?? throw new ArgumentNullException(nameof(serviceScope));
     _factory = middlewareFactory
                ?? throw new ArgumentNullException(nameof(middlewareFactory));
     CachedQuery = cachedQuery
                   ?? throw new ArgumentNullException(nameof(cachedQuery));
     ContextData = contextData
                   ?? throw new ArgumentNullException(nameof(contextData));
     Diagnostics = diagnostics
                   ?? throw new ArgumentNullException(nameof(diagnostics));
 }
        public async Task InvokeAsync(IQueryContext context)
        {
            if (IsContextIncomplete(context))
            {
                context.Result = QueryResult.CreateError(
                    ErrorBuilder.New()
                    .SetMessage(CoreResources
                                .ParseQueryMiddleware_InComplete)
                    .Build());
            }
            else
            {
                Activity activity = _diagnosticEvents.BeginParsing(context);

                try
                {
                    bool         documentRetrievedFromCache = true;
                    string       queryKey    = context.Request.QueryName;
                    ICachedQuery cachedQuery = null;

                    if (queryKey is null || context.Request.Query != null)
                    {
                        queryKey = context.Request.QueryHash is null
                            ? _documentHashProvider.ComputeHash(
                            context.Request.Query.ToSource())
                            : context.Request.QueryHash;
                    }

                    if (context.Request.Query is null &&
                        !_queryCache.TryGet(queryKey, out cachedQuery))
                    {
                        // TODO : check for query storage here?
                        // TODO : RESOURCES
                        context.Result = QueryResult.CreateError(
                            ErrorBuilder.New()
                            .SetMessage("persistedQueryNotFound")
                            .SetCode("CACHED_QUERY_NOT_FOUND")
                            .Build());
                        return;
                    }

                    if (cachedQuery is null)
                    {
                        cachedQuery = _queryCache.GetOrCreate(
                            queryKey,
                            () =>
                        {
                            documentRetrievedFromCache = false;
                            DocumentNode document      =
                                ParseDocument(context.Request.Query);
                            return(new CachedQuery(queryKey, document));
                        });
                    }

                    // update context
                    context.QueryKey    = queryKey;
                    context.CachedQuery = cachedQuery;
                    context.Document    = context.CachedQuery.Document;
                    context.ContextData[ContextDataKeys.DocumentCached] =
                        documentRetrievedFromCache;
                }
                finally
                {
                    _diagnosticEvents.EndParsing(activity, context);
                }

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