Ejemplo n.º 1
0
        protected override async Task HandleAsync(
            ISocketConnection connection,
            DataStartMessage message,
            CancellationToken cancellationToken)
        {
            IQueryRequestBuilder requestBuilder =
                QueryRequestBuilder.From(message.Payload)
                .SetServices(connection.RequestServices);

            await _socketSessionInterceptor.OnRequestAsync(
                connection, requestBuilder, cancellationToken);

            IExecutionResult result = await _requestExecutor.ExecuteAsync(
                requestBuilder.Create(), cancellationToken);

            switch (result)
            {
            case IResponseStream responseStream:
                var subscription = new Subscription(connection, responseStream, message.Id);
                connection.Subscriptions.Register(subscription);
                break;

            case IQueryResult queryResult:
                using (queryResult)
                    await HandleQueryResultAsync(
                        connection, message, queryResult, cancellationToken);
                break;

            default:
                throw DataStartMessageHandler_RequestTypeNotSupported();
            }
        }
        ExecutedMutationWithRenamedInputField(
            IQueryRequestBuilder requestBuilder)
        {
            IHttpClientFactory clientFactory = CreateRemoteSchemas();

            var serviceCollection = new ServiceCollection();

            serviceCollection.AddSingleton(clientFactory);
            serviceCollection.AddStitchedSchema(builder =>
                                                builder.AddSchemaFromHttp("contract")
                                                .AddSchemaFromHttp("customer")
                                                .RenameField(
                                                    new FieldReference("CreateCustomerInput", "name"),
                                                    "foo")
                                                .AddExtensionsFromString(
                                                    FileResource.Open("StitchingExtensions.graphql"))
                                                .AddSchemaConfiguration(c =>
                                                                        c.RegisterType <PaginationAmountType>()));

            IServiceProvider services =
                serviceCollection.BuildServiceProvider();

            IQueryExecutor executor = services
                                      .GetRequiredService <IQueryExecutor>();

            using (IServiceScope scope = services.CreateScope())
            {
                requestBuilder.SetServices(scope.ServiceProvider);
                return(await executor.ExecuteAsync(requestBuilder.Create()));
            }
        }
Ejemplo n.º 3
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.TrySetServices(services);
            builder.TryAddProperty(nameof(HttpContext), context);
            builder.TryAddProperty(nameof(ClaimsPrincipal), context.GetUser());

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

            return(builder.Create());
        }
Ejemplo 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(new HttpContextWrapper(context), builder, requestAborted)
                    .ConfigureAwait(false);
            }

            return builder.Create();
        }
        public static IReadOnlyQueryRequest SetRequestQuery(this HttpContext context, IQueryRequestBuilder queryRequestBuilder)
        {
            IReadOnlyQueryRequest request = queryRequestBuilder.Create();

            context.Items.Add("graph", request);

            return(request);
        }
        protected override async Task HandleAsync(
            ISocketConnection connection,
            DataStartMessage message,
            CancellationToken cancellationToken)
        {
            IQueryRequestBuilder requestBuilder =
                QueryRequestBuilder.New()
                .SetQuery(message.Payload.Query)
                .SetQueryName(message.Payload.QueryName)
                .SetOperation(message.Payload.OperationName)
                .SetVariableValues(message.Payload.Variables)
                .SetProperties(message.Payload.Extensions)
                .SetServices(connection.RequestServices);

            if (_requestInterceptors != null)
            {
                for (var i = 0; i < _requestInterceptors.Length; i++)
                {
                    await _requestInterceptors[i].OnCreateAsync(
                        connection,
                        requestBuilder,
                        cancellationToken)
                    .ConfigureAwait(false);
                }
            }

            IExecutionResult result =
                await _queryExecutor.ExecuteAsync(
                    requestBuilder.Create(),
                    cancellationToken)
                .ConfigureAwait(false);

            switch (result)
            {
            case IResponseStream responseStream:
                connection.Subscriptions.Register(
                    new Subscription(
                        connection,
                        responseStream,
                        message.Id));
                break;

            case IReadOnlyQueryResult queryResult:
                using (queryResult)
                {
                    await HandleQueryResultAsync(
                        connection,
                        message,
                        queryResult,
                        cancellationToken)
                    .ConfigureAwait(false);
                }
                break;

            default:
                throw new NotSupportedException();
            }
        }
Ejemplo n.º 7
0
        public async ValueTask <IQueryResult> InvokeAsync(IResolverContext context)
        {
            if (context is null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            IQueryRequestBuilder builder = New().SetQuery(_query);

            _setVariables(context, builder);

            return(await _dataLoader
                   .LoadAsync(builder.Create(), context.RequestAborted)
                   .ConfigureAwait(false));
        }
Ejemplo n.º 8
0
        public static async Task <IExecutionResult> ExecuteQuery(
            IServiceProvider services, string token = null)
        {
            IQueryRequestBuilder requestBuilder =
                QueryRequestBuilder.New()
                .SetQuery("{ foo }");

            if (token != null)
            {
                HttpContext httpContext = services.GetRequiredService <HttpContext>();
                httpContext.Request.Headers.Add(BewitTokenHeader.Value, token);
            }

            return(await services.ExecuteRequestAsync(requestBuilder.Create()));
        }
Ejemplo n.º 9
0
        public async Task HandleAsync(
            IWebSocketContext context,
            GenericOperationMessage message,
            CancellationToken cancellationToken)
        {
            QueryRequestDto payload = message.Payload.ToObject <QueryRequestDto>();

            IQueryRequestBuilder requestBuilder =
                QueryRequestBuilder.New()
                .SetQuery(payload.Query)
                .SetOperation(payload.OperationName)
                .SetVariableValues(QueryMiddlewareUtilities
                                   .ToDictionary(payload.Variables))
                .SetServices(context.HttpContext.CreateRequestServices());

            await context.PrepareRequestAsync(requestBuilder)
            .ConfigureAwait(false);

            IExecutionResult result =
                await context.QueryExecutor.ExecuteAsync(
                    requestBuilder.Create(), cancellationToken)
                .ConfigureAwait(false);

            if (result is IResponseStream responseStream)
            {
                context.RegisterSubscription(
                    new Subscription(context, responseStream, message.Id));
            }
            else if (result is IReadOnlyQueryResult queryResult)
            {
                await context.SendSubscriptionDataMessageAsync(
                    message.Id, queryResult, cancellationToken)
                .ConfigureAwait(false);

                await context.SendSubscriptionCompleteMessageAsync(
                    message.Id, cancellationToken)
                .ConfigureAwait(false);
            }
        }
        protected override async Task HandleAsync(
            ISocketConnection connection,
            DataStartMessage message,
            CancellationToken cancellationToken)
        {
            IQueryRequestBuilder requestBuilder =
                QueryRequestBuilder.New()
                .SetQuery(message.Payload.Query)
                .SetQueryId(message.Payload.QueryId)
                .SetOperation(message.Payload.OperationName)
                .SetVariableValues(message.Payload.Variables)
                .SetProperties(message.Payload.Extensions)
                .SetServices(connection.RequestServices);

            await _socketSessionInterceptor.OnRequestAsync(
                connection, requestBuilder, cancellationToken);

            IExecutionResult result = await _requestExecutor.ExecuteAsync(
                requestBuilder.Create(), cancellationToken);

            switch (result)
            {
            case IResponseStream responseStream:
                var subscription = new Subscription(connection, responseStream, message.Id);
                connection.Subscriptions.Register(subscription);
                break;

            case IQueryResult queryResult:
                using (queryResult)
                    await HandleQueryResultAsync(
                        connection, message, queryResult, cancellationToken);
                break;

            default:
                throw new NotSupportedException("The response type is not supported.");
            }
        }
Ejemplo n.º 11
0
    protected override async Task HandleAsync(
        ISocketConnection connection,
        DataStartMessage message,
        CancellationToken cancellationToken)
    {
        var session  = new CancellationTokenSource();
        var combined = CancellationTokenSource.CreateLinkedTokenSource(
            session.Token, cancellationToken);
        var sessionIsHandled = false;

        IExecutionResult result = await ExecuteAsync(combined.Token);

        try
        {
            switch (result)
            {
            case SubscriptionResult subscriptionResult:
                // first we add the cts to the result so that they are disposed when the
                // subscription is disposed.
                subscriptionResult.RegisterDisposable(combined);

                // while a subscription result must be disposed we are not handling it here
                // and leave this responsibility to the subscription session.
                ISubscription subscription = GetSubscription(result);

                var subscriptionSession = new SubscriptionSession(
                    session,
                    _socketSessionInterceptor,
                    connection,
                    subscriptionResult,
                    subscription,
                    _diagnosticEvents,
                    message.Id);

                connection.Subscriptions.Register(subscriptionSession);
                sessionIsHandled = true;
                break;

            case IResponseStream streamResult:
                // stream results represent deferred execution streams that use execution
                // resources. We need to ensure that these are disposed when we are
                // finished.
                await using (streamResult)
                {
                    await HandleStreamResultAsync(
                        connection,
                        message,
                        streamResult,
                        cancellationToken);
                }

                break;

            case IQueryResult queryResult:
                // query results use pooled memory an need to be disposed after we have
                // used them.
                using (queryResult)
                {
                    await HandleQueryResultAsync(
                        connection,
                        message,
                        queryResult,
                        cancellationToken);
                }

                break;

            default:
                throw DataStartMessageHandler_RequestTypeNotSupported();
            }
        }
        finally
        {
            if (!sessionIsHandled)
            {
                session.Dispose();
                combined.Dispose();
            }
        }

        async ValueTask <IExecutionResult> ExecuteAsync(CancellationToken cancellationToken)
        {
            try
            {
                IQueryRequestBuilder requestBuilder =
                    QueryRequestBuilder.From(message.Payload)
                    .SetServices(connection.RequestServices);

                await _socketSessionInterceptor.OnRequestAsync(
                    connection, requestBuilder, cancellationToken);

                return(await _requestExecutor.ExecuteAsync(
                           requestBuilder.Create(), cancellationToken));
            }
            catch (Exception ex)
            {
                IErrorBuilder error = _errorHandler.CreateUnexpectedError(ex);
                return(QueryResultBuilder.CreateError(error.Build()));
            }
        }
    }
Ejemplo n.º 12
0
        protected override async Task HandleAsync(
            ISocketConnection connection,
            DataStartMessage message,
            CancellationToken cancellationToken)
        {
            IQueryRequestBuilder requestBuilder =
                QueryRequestBuilder.From(message.Payload)
                .SetServices(connection.RequestServices);

            await _socketSessionInterceptor.OnRequestAsync(
                connection, requestBuilder, cancellationToken);

            IExecutionResult result = await _requestExecutor.ExecuteAsync(
                requestBuilder.Create(), cancellationToken);

            switch (result)
            {
            case ISubscriptionResult subscriptionResult:
                // while a subscription result must be disposed we are not handling it here
                // and leave this responsibility to the subscription session.
                ISubscription subscription = GetSubscription(result);

                var subscriptionSession = new SubscriptionSession(
                    connection,
                    subscriptionResult,
                    subscription,
                    _diagnosticEvents,
                    message.Id);

                connection.Subscriptions.Register(subscriptionSession);
                break;

            case IResponseStream streamResult:
                // stream results represent deferred execution streams that use execution
                // resources. We need to ensure that these are disposed when we are
                // finished.
                await using (streamResult)
                {
                    await HandleStreamResultAsync(
                        connection,
                        message,
                        streamResult,
                        cancellationToken);
                }
                break;

            case IQueryResult queryResult:
                // query results use pooled memory an need to be disposed after we have
                // used them.
                using (queryResult)
                {
                    await HandleQueryResultAsync(
                        connection,
                        message,
                        queryResult,
                        cancellationToken);
                }
                break;

            default:
                throw DataStartMessageHandler_RequestTypeNotSupported();
            }
        }