/// <summary>
        /// Initializes a new instance of the <see cref="ApolloServerErrorMessage" /> class.
        /// </summary>
        /// <param name="message">The message to put into the generated error.</param>
        /// <param name="code">The custom code to apply to the error message.</param>
        /// <param name="severity">The severity of the error message being generated.</param>
        /// <param name="lastMessageId">The id of the  message received which caused this error. The id will be returned
        /// as a meta data item for reference to the client.</param>
        /// <param name="lastMessageType">The type of the  message received which caused this error. The type will be returned
        /// as a meta data item for reference to the client.</param>
        /// <param name="exception">An internal exception that was thrown that should be carried with this message.</param>
        /// <param name="clientProvidedId">The client provided identifier of the failed operation.</param>
        public ApolloServerErrorMessage(
            string message,
            string code = Constants.ErrorCodes.DEFAULT,
            GraphMessageSeverity severity     = GraphMessageSeverity.Critical,
            string lastMessageId              = null,
            ApolloMessageType?lastMessageType = null,
            Exception exception     = null,
            string clientProvidedId = null)
            : base(ApolloMessageType.ERROR)
        {
            this.Id      = clientProvidedId;
            this.Payload = new GraphExecutionMessage(severity, message, code, SourceOrigin.None, exception);

            if (!string.IsNullOrWhiteSpace(lastMessageId))
            {
                this.Payload.MetaData.Add(ApolloConstants.Messaging.LAST_RECEIVED_MESSAGE_ID, lastMessageId);
            }

            if (lastMessageType.HasValue)
            {
                this.Payload.MetaData.Add(
                    ApolloConstants.Messaging.LAST_RECEIVED_MESSAGE_TYPE,
                    ApolloMessageTypeExtensions.Serialize(lastMessageType.Value));
            }
        }
Example #2
0
        /// <summary>
        /// Handles the MessageRecieved event of the ApolloClient control. The client raises this event
        /// whenever a message is recieved and successfully parsed from the under lying websocket.
        /// </summary>
        /// <param name="message">The message.</param>
        /// <returns>TaskMethodBuilder.</returns>
        internal Task DispatchMessage(ApolloMessage message)
        {
            if (message == null)
            {
                return(Task.CompletedTask);
            }

            _logger?.MessageReceived(message);
            switch (message.Type)
            {
            case ApolloMessageType.CONNECTION_INIT:
                return(this.AcknowledgeNewConnection());

            case ApolloMessageType.START:
                return(this.ExecuteStartRequest(message as ApolloClientStartMessage));

            case ApolloMessageType.STOP:
                return(this.ExecuteStopRequest(message as ApolloClientStopMessage));

            case ApolloMessageType.CONNECTION_TERMINATE:
                return(this.CloseConnection(
                           ClientConnectionCloseStatus.NormalClosure,
                           $"Recieved closure request via message '{ApolloMessageTypeExtensions.Serialize(ApolloMessageType.CONNECTION_TERMINATE)}'."));

            default:
                return(this.UnknownMessageRecieved(message));
            }
        }
        /// <summary>
        /// Writes a specified value as JSON.
        /// </summary>
        /// <param name="writer">The writer to write to.</param>
        /// <param name="value">The value to convert to JSON.</param>
        /// <param name="options">An object that specifies serialization options to use.</param>
        public override void Write(Utf8JsonWriter writer, ApolloServerErrorMessage value, JsonSerializerOptions options)
        {
            writer.WriteStartObject();
            writer.WriteString(ApolloConstants.Messaging.MESSAGE_TYPE, ApolloMessageTypeExtensions.Serialize(value.Type));

            if (value.Id != null)
            {
                writer.WriteString(ApolloConstants.Messaging.MESSAGE_ID, value.Id);
            }

            writer.WritePropertyName(ApolloConstants.Messaging.MESSAGE_PAYLOAD);
            if (value.Payload == null)
            {
                writer.WriteNullValue();
            }
            else
            {
                var messageWriter = new SingleMessageResponseWriter(_schema);
                messageWriter.WriteSingleMessage(
                    writer,
                    value.Payload,
                    new GraphQLResponseOptions()
                {
                    ExposeExceptions = _schema.Configuration.ResponseOptions.ExposeExceptions,
                    ExposeMetrics    = _schema.Configuration.ResponseOptions.ExposeMetrics,
                });
            }

            writer.WriteEndObject();
        }
Example #4
0
        /// <summary>
        /// Writes a specified value as JSON.
        /// </summary>
        /// <param name="writer">The writer to write to.</param>
        /// <param name="value">The value to convert to JSON.</param>
        /// <param name="options">An object that specifies serialization options to use.</param>
        public override void Write(Utf8JsonWriter writer, ApolloServerCompleteMessage value, JsonSerializerOptions options)
        {
            writer.WriteStartObject();
            writer.WriteString(ApolloConstants.Messaging.MESSAGE_TYPE, ApolloMessageTypeExtensions.Serialize(value.Type));

            if (value.Id != null)
            {
                writer.WriteString(ApolloConstants.Messaging.MESSAGE_ID, value.Id);
            }

            writer.WriteEndObject();
        }
Example #5
0
        /// <summary>
        /// Writes a specified value as JSON.
        /// </summary>
        /// <param name="writer">The writer to write to.</param>
        /// <param name="value">The value to convert to JSON.</param>
        /// <param name="options">An object that specifies serialization options to use.</param>
        public override void Write(Utf8JsonWriter writer, ApolloMessage value, JsonSerializerOptions options)
        {
            writer.WriteStartObject();
            writer.WriteString(ApolloConstants.Messaging.MESSAGE_TYPE, ApolloMessageTypeExtensions.Serialize(value.Type));

            if (value.Id != null)
            {
                writer.WriteString(ApolloConstants.Messaging.MESSAGE_ID, value.Id);
            }

            if (value.PayloadObject != null)
            {
                writer.WritePropertyName(ApolloConstants.Messaging.MESSAGE_PAYLOAD);
                JsonSerializer.Serialize(writer, value.PayloadObject, value.PayloadObject.GetType());
            }

            writer.WriteEndObject();
        }