Exemplo n.º 1
0
        protected override async Task ExecuteRequestAsync(
            HttpContext context,
            IServiceProvider services)
        {
            var builder = QueryRequestBuilder.New();

#if ASPNETCLASSIC
            IReadableStringCollection requestQuery = context.Request.Query;
#else
            IQueryCollection requestQuery = context.Request.Query;
#endif

            builder
            .SetQuery(requestQuery[_queryIdentifier])
            .SetQueryName(requestQuery[_namedQueryIdentifier])
            .SetOperation(requestQuery[_operationNameIdentifier]);

            string variables = requestQuery[_variablesIdentifier];
            if (variables != null &&
                variables.Length > 0 &&
                Utf8GraphQLRequestParser.ParseJson(variables)
                is IReadOnlyDictionary <string, object> v)
            {
                builder.SetVariableValues(v);
            }

            IReadOnlyQueryRequest request =
                await BuildRequestAsync(
                    context,
                    services,
                    builder)
                .ConfigureAwait(false);

            var schemaName = await _schemaNameProvider(context).ConfigureAwait(false);

            var _queryExecutor = _queryExecutorProvider.GetQueryExecutor(schemaName);

            IExecutionResult result = await _queryExecutor
                                      .ExecuteAsync(request, context.GetCancellationToken())
                                      .ConfigureAwait(false);

            SetResponseHeaders(
                context.Response,
                _resultSerializer.ContentType);

            await _resultSerializer.SerializeAsync(
                result,
                context.Response.Body,
                context.GetCancellationToken())
            .ConfigureAwait(false);
        }
Exemplo n.º 2
0
        protected async Task <IReadOnlyQueryRequest> BuildRequestAsync(
            HttpContext context,
            IServiceProvider services,
            IQueryRequestBuilder builder)
        {
            if (!_interceptorInitialized)
            {
                _interceptor = services
                               .GetService <IQueryRequestInterceptor <HttpContext> >();
                _interceptorInitialized = true;
            }

            if (_interceptor != null)
            {
                await _interceptor.OnCreateAsync(
                    context,
                    builder,
                    context.GetCancellationToken())
                .ConfigureAwait(false);
            }

            builder.SetServices(services);
            builder.TryAddProperty(nameof(HttpContext), context);
            builder.TryAddProperty(nameof(ClaimsPrincipal), context.GetUser());

            if (context.IsTracingEnabled())
            {
                builder.TryAddProperty(ContextDataKeys.EnableTracing, true);
            }

            return(builder.Create());
        }
Exemplo n.º 3
0
        private async Task HandleRequestAsync(
            HttpContext context,
            IQueryExecutor queryExecutor)
        {
#if ASPNETCLASSIC
            if (_accessor != null)
            {
                _accessor.OwinContext = context;
            }

            using (IServiceScope serviceScope =
                Executor.Schema.Services.CreateScope())
            {
                IServiceProvider serviceProvider =
                    context.CreateRequestServices(
                        serviceScope.ServiceProvider);
#else
            IServiceProvider serviceProvider = context.RequestServices;
#endif

            IReadOnlyQueryRequest request =
                await CreateQueryRequestInternalAsync(context, serviceProvider)
                    .ConfigureAwait(false);

                IExecutionResult result = await queryExecutor
                    .ExecuteAsync(request, context.GetCancellationToken())
                    .ConfigureAwait(false);

                await WriteResponseAsync(context.Response, result)
                    .ConfigureAwait(false);
#if ASPNETCLASSIC
            }
#endif
        }
Exemplo n.º 4
0
            CreateQueryRequestInternalAsync(
                HttpContext context,
                IServiceProvider services)
        {
            IQueryRequestBuilder builder =
                await CreateQueryRequestAsync(context)
                    .ConfigureAwait(false);

            OnCreateRequestAsync onCreateRequest = Options.OnCreateRequest
                ?? GetService<OnCreateRequestAsync>(context);

            builder.AddProperty(nameof(HttpContext), context);
            builder.AddProperty(nameof(ClaimsPrincipal), context.GetUser());
            builder.SetServices(services);

            if (context.IsTracingEnabled())
            {
                builder.AddProperty(ContextDataKeys.EnableTracing, true);
            }

            if (onCreateRequest != null)
            {
                CancellationToken requestAborted =
                    context.GetCancellationToken();

                await onCreateRequest(context, builder, requestAborted)
                    .ConfigureAwait(false);
            }

            return builder.Create();
        }
Exemplo n.º 5
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public async Task InvokeAsync(HttpContext context)
#endif
        {
            string queryUrl = BuildUrl(context.Request, _queryPath);

            context.Response.ContentType = "application/javascript";

            await context.Response.WriteAsync($@"
                window.Settings = {{
                    url: ""{queryUrl}"",
                }}
            ",
                                              context.GetCancellationToken())
            .ConfigureAwait(false);
        }
Exemplo n.º 6
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public async Task InvokeAsync(HttpContext context)
#endif
        {
            string queryUrl        = BuildUrl(context.Request, false, _queryPath);
            string subscriptionUrl = _options.EnableSubscription
                ? $"\"{BuildUrl(context.Request, true, _subscriptionPath)}\""
                : "null";

            context.Response.ContentType = "application/javascript";

            await context.Response.WriteAsync($@"
                window.Settings = {{
                    url: ""{queryUrl}"",
                    subscriptionUrl: {subscriptionUrl},
                }}
            ",
                                              context.GetCancellationToken())
            .ConfigureAwait(false);
        }