Ejemplo n.º 1
0
        public void DuplicateIdIsRejected()
        {
            var client = new Mock <ISubscriptionClientProxy>().Object;
            var idSet  = new ClientTrackedMessageIdSet();

            var result = idSet.ReserveMessageId("abc123");

            Assert.IsTrue(result);

            result = idSet.ReserveMessageId("abc123");
            Assert.IsFalse(result);
        }
Ejemplo n.º 2
0
        public void ReleasedIdIsAllowedToBeAdded()
        {
            var client = new Mock <ISubscriptionClientProxy>().Object;
            var idSet  = new ClientTrackedMessageIdSet();

            var result = idSet.ReserveMessageId("abc123");

            Assert.IsTrue(result);

            idSet.ReleaseMessageId("abc123");
            result = idSet.ReserveMessageId("abc123");
            Assert.IsTrue(result);
        }
Ejemplo n.º 3
0
        public void ClearDropsAllIds()
        {
            var client = new Mock <ISubscriptionClientProxy>().Object;
            var idSet  = new ClientTrackedMessageIdSet();

            var result = idSet.ReserveMessageId("abc123");

            Assert.IsTrue(result);
            result = idSet.ReserveMessageId("abc1234");
            Assert.IsTrue(result);

            idSet.Clear();
            Assert.IsFalse(idSet.Contains("abc123"));
            Assert.IsFalse(idSet.Contains("abc1234"));
        }
Ejemplo n.º 4
0
        public void ContainsReturnsTrue_WhenIdIsPresent()
        {
            var client = new Mock <ISubscriptionClientProxy>().Object;
            var idSet  = new ClientTrackedMessageIdSet();

            var result = idSet.ReserveMessageId("abc123");

            Assert.IsTrue(result);
            Assert.IsTrue(idSet.Contains("abc123"));
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Parses the message contents to generate a valid client subscription and adds it to the watched
        /// set for this instance.
        /// </summary>
        /// <param name="message">The message with the subscription details.</param>
        private async Task ExecuteStartRequest(ApolloClientStartMessage message)
        {
            // ensure the id isnt already in use
            if (!_reservedMessageIds.ReserveMessageId(message.Id))
            {
                await this
                .SendMessage(new ApolloServerErrorMessage(
                                 $"The message id {message.Id} is already reserved for an outstanding request and cannot " +
                                 "be processed against. Allow the in-progress request to complete or stop the associated subscription.",
                                 SubscriptionConstants.ErrorCodes.DUPLICATE_MESSAGE_ID,
                                 lastMessage : message,
                                 clientProvidedId : message.Id))
                .ConfigureAwait(false);

                return;
            }

            var retainMessageId = false;
            var runtime         = this.ServiceProvider.GetRequiredService(typeof(IGraphQLRuntime <TSchema>)) as IGraphQLRuntime <TSchema>;
            var request         = runtime.CreateRequest(message.Payload);
            var metricsPackage  = _enableMetrics ? runtime.CreateMetricsPackage() : null;
            var context         = new SubcriptionExecutionContext(
                this,
                request,
                message.Id,
                metricsPackage);

            var result = await runtime
                         .ExecuteRequest(context)
                         .ConfigureAwait(false);

            if (context.IsSubscriptionOperation)
            {
                retainMessageId = await this
                                  .RegisterSubscriptionOrRespond(context.Subscription as ISubscription <TSchema>)
                                  .ConfigureAwait(false);
            }
            else
            {
                // not a subscription, just send back the generated response and close out the id
                ApolloMessage responseMessage;

                // report syntax errors as error messages
                // allow others to bubble into a fully reslt (per apollo spec)
                if (result.Messages.Count == 1 &&
                    result.Messages[0].Code == Constants.ErrorCodes.SYNTAX_ERROR)
                {
                    responseMessage = new ApolloServerErrorMessage(
                        result.Messages[0],
                        message,
                        message.Id);
                }
                else
                {
                    responseMessage = new ApolloServerDataMessage(message.Id, result);
                }

                await this
                .SendMessage(responseMessage)
                .ConfigureAwait(false);

                await this
                .SendMessage(new ApolloServerCompleteMessage(message.Id))
                .ConfigureAwait(false);
            }

            if (!retainMessageId)
            {
                _reservedMessageIds.ReleaseMessageId(message.Id);
            }
        }