Пример #1
0
        /// <summary>
        /// Submits the GraphQL query for processing.
        /// </summary>
        /// <param name="queryData">The query data.</param>
        /// <returns>Task&lt;IActionResult&gt;.</returns>
        public virtual async Task SubmitGraphQLQuery(GraphQueryData queryData)
        {
            // ensure data was received
            if (queryData == null || string.IsNullOrWhiteSpace(queryData.Query))
            {
                await this.WriteStatusCodeResponse(HttpStatusCode.BadRequest, ERROR_NO_QUERY_PROVIDED).ConfigureAwait(false);

                return;
            }

            using (var cancelSource = new CancellationTokenSource())
            {
                try
                {
                    // *******************************
                    // Setup
                    // *******************************
                    this.GraphQLRequest = this.CreateRequest(queryData);
                    if (this.GraphQLRequest == null)
                    {
                        await this.WriteStatusCodeResponse(HttpStatusCode.InternalServerError, ERROR_NO_REQUEST_CREATED).ConfigureAwait(false);

                        return;
                    }

                    // *******************************
                    // Primary query execution
                    // *******************************
                    var metricPackage = this.EnableMetrics ? _metricsFactory.CreateMetricsPackage() : null;
                    var context       = new GraphQueryExecutionContext(
                        this.GraphQLRequest,
                        this.HttpContext.RequestServices,
                        this.HttpContext.User,
                        metricPackage,
                        _logger);

                    await _queryPipeline.InvokeAsync(context, cancelSource.Token).ConfigureAwait(false);

                    // *******************************
                    // Response Generation
                    // *******************************
                    var queryResponse = context.Result;
                    if (queryResponse == null)
                    {
                        queryResponse = this.ErrorMessageAsGraphQLResponse(ERROR_NO_RESPONSE);
                    }

                    // if any metrics were populated in the execution, allow a child class to process them
                    if (context.Metrics != null)
                    {
                        this.HandleQueryMetrics(context.Metrics);
                    }

                    // all done, finalize and return
                    queryResponse = this.FinalizeResult(queryResponse);
                    await this.WriteResponse(queryResponse).ConfigureAwait(false);
                }
                catch (Exception ex)
                {
                    var exceptionResult = this.HandleQueryException(ex);
                    if (exceptionResult == null)
                    {
                        // no one was able to handle hte exception. Log it if able and just fail out to the caller
                        _logger?.UnhandledExceptionEvent(ex);
                        await this.WriteStatusCodeResponse(HttpStatusCode.InternalServerError, ERROR_INTERNAL_SERVER_ISSUE).ConfigureAwait(false);
                    }
                    else
                    {
                        await this.WriteResponse(exceptionResult).ConfigureAwait(false);
                    }
                }
            }
        }
 /// <inheritdoc />
 public IGraphQueryExecutionMetrics CreateMetricsPackage()
 {
     return(_metricsFactory?.CreateMetricsPackage());
 }