From dff498ebb45f11c8bf8320f728f54330093107a9 Mon Sep 17 00:00:00 2001 From: Raul Hidalgo Caballero Date: Sat, 27 Jan 2018 14:02:52 +0100 Subject: [PATCH 1/7] deleted using --- src/GraphQL.Client/GraphQLClientExtensions.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/GraphQL.Client/GraphQLClientExtensions.cs b/src/GraphQL.Client/GraphQLClientExtensions.cs index 74741a29..ba6aeb1e 100644 --- a/src/GraphQL.Client/GraphQLClientExtensions.cs +++ b/src/GraphQL.Client/GraphQLClientExtensions.cs @@ -1,4 +1,3 @@ -using System.IO; using System.Threading.Tasks; using GraphQL.Common.Request; using GraphQL.Common.Response; From 2bae82b5d5e2342dbba849f3088170931e367fbd Mon Sep 17 00:00:00 2001 From: Raul Hidalgo Caballero Date: Sat, 27 Jan 2018 14:16:56 +0100 Subject: [PATCH 2/7] parsing JSON as stream --- src/GraphQL.Client/GraphQLClient.cs | 11 +++++++++-- src/GraphQL.Common/Exceptions/GraphQLException.cs | 2 +- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/src/GraphQL.Client/GraphQLClient.cs b/src/GraphQL.Client/GraphQLClient.cs index 008626c7..758ac618 100644 --- a/src/GraphQL.Client/GraphQLClient.cs +++ b/src/GraphQL.Client/GraphQLClient.cs @@ -1,4 +1,5 @@ using System; +using System.IO; using System.Net.Http; using System.Net.Http.Headers; using System.Text; @@ -158,8 +159,14 @@ public void Dispose() => /// The Response /// The GrahQLResponse private async Task ReadHttpResponseMessageAsync(HttpResponseMessage httpResponseMessage) { - var resultString = await httpResponseMessage.Content.ReadAsStringAsync().ConfigureAwait(false); - return JsonConvert.DeserializeObject(resultString, this.Options.JsonSerializerSettings); + using (var stream = await httpResponseMessage.Content.ReadAsStreamAsync().ConfigureAwait(false)) + using (var streamReader = new StreamReader(stream)) + using (var jsonTextReader = new JsonTextReader(streamReader)) { + var jsonSerializer = new JsonSerializer { + ContractResolver = this.Options.JsonSerializerSettings.ContractResolver + }; + return jsonSerializer.Deserialize(jsonTextReader); + } } } diff --git a/src/GraphQL.Common/Exceptions/GraphQLException.cs b/src/GraphQL.Common/Exceptions/GraphQLException.cs index db043494..5e03a77a 100644 --- a/src/GraphQL.Common/Exceptions/GraphQLException.cs +++ b/src/GraphQL.Common/Exceptions/GraphQLException.cs @@ -17,7 +17,7 @@ public class GraphQLException : Exception { /// Constructor for a GraphQLException /// /// The GraphQL Error - public GraphQLException(GraphQLError graphQLError):base(graphQLError.Message) { + public GraphQLException(GraphQLError graphQLError) : base(graphQLError.Message) { this.GraphQLError = graphQLError; } From ba4ef8e04bd0d6eceb695552e52398942a83de8d Mon Sep 17 00:00:00 2001 From: Raul Hidalgo Caballero Date: Sat, 27 Jan 2018 14:26:27 +0100 Subject: [PATCH 3/7] protected --- tests/GraphQL.Client.Tests/BaseGraphQLClientTest.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/GraphQL.Client.Tests/BaseGraphQLClientTest.cs b/tests/GraphQL.Client.Tests/BaseGraphQLClientTest.cs index 5fbbd9b0..d7f952cb 100644 --- a/tests/GraphQL.Client.Tests/BaseGraphQLClientTest.cs +++ b/tests/GraphQL.Client.Tests/BaseGraphQLClientTest.cs @@ -2,9 +2,9 @@ namespace GraphQL.Client.Tests { - public abstract class BaseGraphQLClientTest : IDisposable{ + public abstract class BaseGraphQLClientTest : IDisposable { - public GraphQLClient GraphQLClient { get; set; } = new GraphQLClient("https://swapi.apis.guru/"); + protected GraphQLClient GraphQLClient { get; } = new GraphQLClient("https://swapi.apis.guru/"); public void Dispose() => this.GraphQLClient.Dispose(); From 0af610ec94a82d834851969c89715265f5121e4d Mon Sep 17 00:00:00 2001 From: Raul Hidalgo Caballero Date: Sat, 27 Jan 2018 14:46:48 +0100 Subject: [PATCH 4/7] GraphQLHttpException --- .../Exceptions/GraphQLHttpException.cs | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 src/GraphQL.Client/Exceptions/GraphQLHttpException.cs diff --git a/src/GraphQL.Client/Exceptions/GraphQLHttpException.cs b/src/GraphQL.Client/Exceptions/GraphQLHttpException.cs new file mode 100644 index 00000000..ed648e75 --- /dev/null +++ b/src/GraphQL.Client/Exceptions/GraphQLHttpException.cs @@ -0,0 +1,26 @@ +using System; +using System.Net.Http; + +namespace GraphQL.Client.Exceptions { + + /// + /// An exception thrown on unexpected + /// + public class GraphQLHttpException : Exception { + + /// + /// The + /// + public HttpResponseMessage HttpResponseMessage { get; } + + /// + /// Creates a new instance of + /// + /// The unexpected + public GraphQLHttpException(HttpResponseMessage httpResponseMessage):base($"Unexpected {nameof(System.Net.Http.HttpResponseMessage)} with code: {httpResponseMessage.StatusCode}") { + this.HttpResponseMessage = httpResponseMessage ?? throw new ArgumentNullException(nameof(httpResponseMessage)); + } + + } + +} From e4a157b57ff343466bd3ff2741e4594dd9cc2300 Mon Sep 17 00:00:00 2001 From: Raul Hidalgo Caballero Date: Sat, 27 Jan 2018 14:47:03 +0100 Subject: [PATCH 5/7] catching errors --- src/GraphQL.Client/GraphQLClient.cs | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/GraphQL.Client/GraphQLClient.cs b/src/GraphQL.Client/GraphQLClient.cs index 758ac618..3dca16ba 100644 --- a/src/GraphQL.Client/GraphQLClient.cs +++ b/src/GraphQL.Client/GraphQLClient.cs @@ -4,6 +4,7 @@ using System.Net.Http.Headers; using System.Text; using System.Threading.Tasks; +using GraphQL.Client.Exceptions; using GraphQL.Common.Request; using GraphQL.Common.Response; using Newtonsoft.Json; @@ -165,7 +166,15 @@ private async Task ReadHttpResponseMessageAsync(HttpResponseMes var jsonSerializer = new JsonSerializer { ContractResolver = this.Options.JsonSerializerSettings.ContractResolver }; - return jsonSerializer.Deserialize(jsonTextReader); + try { + return jsonSerializer.Deserialize(jsonTextReader); + } + catch (JsonReaderException exception) { + if (httpResponseMessage.IsSuccessStatusCode) { + throw exception; + } + throw new GraphQLHttpException(httpResponseMessage); + } } } From d913ba88db1e75d13048bbac652735743be1ea40 Mon Sep 17 00:00:00 2001 From: Raul Hidalgo Caballero Date: Sat, 27 Jan 2018 14:50:49 +0100 Subject: [PATCH 6/7] fix style --- src/GraphQL.Client/Exceptions/GraphQLHttpException.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/GraphQL.Client/Exceptions/GraphQLHttpException.cs b/src/GraphQL.Client/Exceptions/GraphQLHttpException.cs index ed648e75..e29d0677 100644 --- a/src/GraphQL.Client/Exceptions/GraphQLHttpException.cs +++ b/src/GraphQL.Client/Exceptions/GraphQLHttpException.cs @@ -17,7 +17,7 @@ public class GraphQLHttpException : Exception { /// Creates a new instance of /// /// The unexpected - public GraphQLHttpException(HttpResponseMessage httpResponseMessage):base($"Unexpected {nameof(System.Net.Http.HttpResponseMessage)} with code: {httpResponseMessage.StatusCode}") { + public GraphQLHttpException(HttpResponseMessage httpResponseMessage) : base($"Unexpected {nameof(System.Net.Http.HttpResponseMessage)} with code: {httpResponseMessage.StatusCode}") { this.HttpResponseMessage = httpResponseMessage ?? throw new ArgumentNullException(nameof(httpResponseMessage)); } From b7c5e4131012627025290e4c997ade0514322d25 Mon Sep 17 00:00:00 2001 From: Raul Hidalgo Caballero Date: Sat, 27 Jan 2018 15:01:33 +0100 Subject: [PATCH 7/7] disposing requests --- src/GraphQL.Client/GraphQLClient.cs | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/GraphQL.Client/GraphQLClient.cs b/src/GraphQL.Client/GraphQLClient.cs index 3dca16ba..2f4177a4 100644 --- a/src/GraphQL.Client/GraphQLClient.cs +++ b/src/GraphQL.Client/GraphQLClient.cs @@ -118,8 +118,9 @@ public async Task GetAsync(GraphQLRequest request) { var queryParamsBuilder = new StringBuilder($"query={request.Query}", 3); if (request.OperationName != null) { queryParamsBuilder.Append($"&operationName={request.OperationName}"); } if (request.Variables != null) { queryParamsBuilder.Append($"&variables={JsonConvert.SerializeObject(request.Variables)}"); } - var httpResponseMessage = await this.httpClient.GetAsync($"{this.Options.EndPoint}?{queryParamsBuilder.ToString()}").ConfigureAwait(false); - return await this.ReadHttpResponseMessageAsync(httpResponseMessage).ConfigureAwait(false); + using (var httpResponseMessage = await this.httpClient.GetAsync($"{this.Options.EndPoint}?{queryParamsBuilder.ToString()}").ConfigureAwait(false)) { + return await this.ReadHttpResponseMessageAsync(httpResponseMessage).ConfigureAwait(false); + } } /// @@ -143,9 +144,10 @@ public async Task PostAsync(GraphQLRequest request) { if (request.Query == null) { throw new ArgumentNullException(nameof(request.Query)); } var graphQLString = JsonConvert.SerializeObject(request, this.Options.JsonSerializerSettings); - var httpContent = new StringContent(graphQLString, Encoding.UTF8, this.Options.MediaType.MediaType); - var httpResponseMessage = await this.httpClient.PostAsync(this.EndPoint, httpContent).ConfigureAwait(false); - return await this.ReadHttpResponseMessageAsync(httpResponseMessage).ConfigureAwait(false); + using (var httpContent = new StringContent(graphQLString, Encoding.UTF8, this.Options.MediaType.MediaType)) + using (var httpResponseMessage = await this.httpClient.PostAsync(this.EndPoint, httpContent).ConfigureAwait(false)) { + return await this.ReadHttpResponseMessageAsync(httpResponseMessage).ConfigureAwait(false); + } } ///