/// <summary>
 /// Initializes a new instance of the <see cref="SubcriptionExecutionContext" /> class.
 /// </summary>
 /// <param name="client">The client.</param>
 /// <param name="request">The request to be processed through the query pipeline.</param>
 /// <param name="subscriptionId">The unique id to assign to the created subscription, when one is made.</param>
 /// <param name="metrics">The metrics package to profile this request, if any.</param>
 /// <param name="logger">The logger instance to record events related to this context.</param>
 /// <param name="items">A key/value pair collection for random access data.</param>
 public SubcriptionExecutionContext(
     ISubscriptionClientProxy client,
     IGraphOperationRequest request,
     string subscriptionId,
     IGraphQueryExecutionMetrics metrics = null,
     IGraphEventLogger logger            = null,
     MetaDataCollection items            = null)
     : base(request, client?.ServiceProvider, client?.User, metrics, logger, items)
 {
     this.Client         = Validation.ThrowIfNullOrReturn(client, nameof(client));
     this.SubscriptionId = Validation.ThrowIfNullWhiteSpaceOrReturn(subscriptionId, nameof(subscriptionId));
 }
示例#2
0
 /// <summary>
 /// Initializes a new instance of the <see cref="GraphQueryExecutionContext" /> class.
 /// </summary>
 /// <param name="request">The request to be processed through the query pipeline.</param>
 /// <param name="serviceProvider">The service provider passed on the HttpContext.</param>
 /// <param name="user">The user authenticated by the Asp.net runtime.</param>
 /// <param name="metrics">The metrics package to profile this request, if any.</param>
 /// <param name="logger">The logger instance to record events related to this context.</param>
 /// <param name="items">A key/value pair collection for random access data.</param>
 public GraphQueryExecutionContext(
     IGraphOperationRequest request,
     IServiceProvider serviceProvider,
     ClaimsPrincipal user = null,
     IGraphQueryExecutionMetrics metrics = null,
     IGraphEventLogger logger            = null,
     MetaDataCollection items            = null)
     : base(serviceProvider, user, metrics, logger, items)
 {
     this.Request      = Validation.ThrowIfNullOrReturn(request, nameof(request));
     this.FieldResults = new List <GraphDataItem>();
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="BaseGraphMiddlewareContext" /> class.
 /// </summary>
 /// <param name="serviceProvider">The service provider passed on the HttpContext.</param>
 /// <param name="user">The user authenticated by the Asp.net runtime.</param>
 /// <param name="metrics">The metrics package to profile this request, if any.</param>
 /// <param name="logger">The logger instance to record events related to this context.</param>
 /// <param name="items">A key/value pair collection for random access data.</param>
 protected BaseGraphMiddlewareContext(
     IServiceProvider serviceProvider,
     ClaimsPrincipal user = null,
     IGraphQueryExecutionMetrics metrics = null,
     IGraphEventLogger logger            = null,
     MetaDataCollection items            = null)
 {
     this.ServiceProvider = Validation.ThrowIfNullOrReturn(serviceProvider, nameof(serviceProvider));
     this.User            = user;
     this.Metrics         = metrics;
     this.Items           = items ?? new MetaDataCollection();
     this.Messages        = new GraphMessageCollection();
     this.Logger          = logger;
 }
        /// <summary>
        /// Initializes a new instance of the <see cref="GraphOperationResult" /> class.
        /// </summary>
        /// <param name="originalRequest">The original request.</param>
        /// <param name="messages">The message collection containing any messages that may
        /// have been generated during execution.</param>
        /// <param name="dataItem">The top level field set resolved during the operation.</param>
        /// <param name="metrics">The metrics package that was filled during the operation execution.</param>
        public GraphOperationResult(
            IGraphOperationRequest originalRequest,
            IEnumerable <IGraphMessage> messages = null,
            IResponseFieldSet dataItem           = null,
            IGraphQueryExecutionMetrics metrics  = null)
        {
            this.Request  = originalRequest;
            this.Data     = dataItem;
            this.Messages = new GraphMessageCollection();
            if (messages != null)
            {
                this.Messages.AddRange(messages);
            }

            this.Metrics = metrics;
        }
        /// <inheritdoc />
        public Task <IGraphOperationResult> ExecuteRequest(
            IServiceProvider serviceProvider,
            ClaimsPrincipal user,
            IGraphOperationRequest request,
            IGraphQueryExecutionMetrics metricsPackage = null,
            CancellationToken cancelToken = default)
        {
            Validation.ThrowIfNull(serviceProvider, nameof(serviceProvider));
            Validation.ThrowIfNull(user, nameof(user));
            Validation.ThrowIfNull(request, nameof(request));

            var context = new GraphQueryExecutionContext(
                request,
                serviceProvider,
                user,
                metricsPackage,
                _logger);

            return(this.ExecuteRequest(context, cancelToken));
        }
 /// <summary>
 /// Adds the metrics package to the query context that is being generated by this builder.
 /// </summary>
 /// <param name="metricsPackage">The metrics package.</param>
 /// <returns>SubscriptionContextBuilder.</returns>
 public SubscriptionContextBuilder AddMetrics(IGraphQueryExecutionMetrics metricsPackage)
 {
     _metrics = metricsPackage;
     return(this);
 }
示例#7
0
 /// <summary>
 /// When overriden in a child class, this method provides access to the metrics package populated during a query run to facilicate custom processing.
 /// This method is only called if a metrics package was generated for the request and will be called regardless of whether metrics are
 /// exposed to the requestor in a response package.
 /// </summary>
 /// <param name="metrics">The metrics.</param>
 protected virtual void HandleQueryMetrics(IGraphQueryExecutionMetrics metrics)
 {
 }
示例#8
0
        /// <summary>
        /// Instructs the client to process the new event. If this is an event the client subscribes
        /// to it should process the data appropriately and send down any data to its underlying connection
        /// as necessary.
        /// </summary>
        /// <param name="field">The unique field corrisponding to the event that was raised
        /// by the publisher.</param>
        /// <param name="sourceData">The source data sent from the publisher when the event was raised.</param>
        /// <param name="cancelToken">A cancellation token.</param>
        /// <returns>Task.</returns>
        public async Task ReceiveEvent(GraphFieldPath field, object sourceData, CancellationToken cancelToken = default)
        {
            await Task.Yield();

            if (field == null)
            {
                return;
            }

            var subscriptions = _subscriptions.RetreiveByRoute(field);

            _logger?.SubscriptionEventReceived(field, subscriptions);
            if (subscriptions.Count == 0)
            {
                return;
            }

            var runtime = this.ServiceProvider.GetRequiredService <IGraphQLRuntime <TSchema> >();
            var schema  = this.ServiceProvider.GetRequiredService <TSchema>();

            var tasks = new List <Task>();

            foreach (var subscription in subscriptions)
            {
                IGraphQueryExecutionMetrics metricsPackage = null;
                IGraphEventLogger           logger         = this.ServiceProvider.GetService <IGraphEventLogger>();

                if (schema.Configuration.ExecutionOptions.EnableMetrics)
                {
                    var factory = this.ServiceProvider.GetRequiredService <IGraphQueryExecutionMetricsFactory <TSchema> >();
                    metricsPackage = factory.CreateMetricsPackage();
                }

                var context = new GraphQueryExecutionContext(
                    runtime.CreateRequest(subscription.QueryData),
                    this.ServiceProvider,
                    this.User,
                    metricsPackage,
                    logger);

                // register the event data as a source input for the target subscription field
                context.DefaultFieldSources.AddSource(subscription.Field, sourceData);
                context.QueryPlan      = subscription.QueryPlan;
                context.QueryOperation = subscription.QueryOperation;

                tasks.Add(runtime.ExecuteRequest(context, cancelToken)
                          .ContinueWith(
                              task =>
                {
                    if (task.IsFaulted)
                    {
                        return(task);
                    }

                    // send the message with the resultant data package
                    var message = new ApolloServerDataMessage(subscription.Id, task.Result);
                    return(this.SendMessage(message));
                },
                              cancelToken));
            }

            await Task.WhenAll(tasks).ConfigureAwait(false);
        }