diff --git a/src/GraphQL.Client/Http/GraphQLHttpClient.cs b/src/GraphQL.Client/Http/GraphQLHttpClient.cs index 383489fc..252e3016 100644 --- a/src/GraphQL.Client/Http/GraphQLHttpClient.cs +++ b/src/GraphQL.Client/Http/GraphQLHttpClient.cs @@ -1,4 +1,5 @@ using System; +using System.Net.Http; using System.Net.Http.Headers; using System.Threading; using System.Threading.Tasks; @@ -89,6 +90,16 @@ public GraphQLHttpClient(GraphQLHttpClientOptions options) { this.graphQLHttpHandler = new GraphQLHttpHandler(options); } + internal GraphQLHttpClient(GraphQLHttpClientOptions options,HttpClient httpClient) { + if (options == null) { throw new ArgumentNullException(nameof(options)); } + if (options.EndPoint == null) { throw new ArgumentNullException(nameof(options.EndPoint)); } + if (options.JsonSerializerSettings == null) { throw new ArgumentNullException(nameof(options.JsonSerializerSettings)); } + if (options.HttpMessageHandler == null) { throw new ArgumentNullException(nameof(options.HttpMessageHandler)); } + if (options.MediaType == null) { throw new ArgumentNullException(nameof(options.MediaType)); } + + this.graphQLHttpHandler = new GraphQLHttpHandler(options,httpClient); + } + public async Task SendQueryAsync(string query, CancellationToken cancellationToken = default) => await this.SendQueryAsync(new GraphQLRequest { Query = query }, cancellationToken).ConfigureAwait(false); diff --git a/src/GraphQL.Client/Http/HttpClientExtensions.cs b/src/GraphQL.Client/Http/HttpClientExtensions.cs new file mode 100644 index 00000000..c0fb4b49 --- /dev/null +++ b/src/GraphQL.Client/Http/HttpClientExtensions.cs @@ -0,0 +1,21 @@ +using System.Net.Http; + +namespace GraphQL.Client.Http { + + /// + /// Extensions for + /// + public static class HttpClientExtensions { + + /// + /// Creates a from a + /// + /// + /// + /// + public static GraphQLHttpClient AsGraphQLClient(this HttpClient httpClient, GraphQLHttpClientOptions graphQLHttpClientOptions) => + new GraphQLHttpClient(graphQLHttpClientOptions, httpClient); + + } + +} diff --git a/src/GraphQL.Client/Http/Internal/GraphQLHttpHandler.cs b/src/GraphQL.Client/Http/Internal/GraphQLHttpHandler.cs index 4ed77cf2..be9cc8bf 100644 --- a/src/GraphQL.Client/Http/Internal/GraphQLHttpHandler.cs +++ b/src/GraphQL.Client/Http/Internal/GraphQLHttpHandler.cs @@ -26,6 +26,16 @@ public GraphQLHttpHandler(GraphQLHttpClientOptions options) { this.HttpClient = new HttpClient(this.Options.HttpMessageHandler); } + public GraphQLHttpHandler(GraphQLHttpClientOptions options, HttpClient httpClient) { + this.Options = options ?? throw new ArgumentNullException(nameof(options)); + if (options.EndPoint == null) { throw new ArgumentNullException(nameof(options.EndPoint)); } + if (options.JsonSerializerSettings == null) { throw new ArgumentNullException(nameof(options.JsonSerializerSettings)); } + if (options.HttpMessageHandler == null) { throw new ArgumentNullException(nameof(options.HttpMessageHandler)); } + if (options.MediaType == null) { throw new ArgumentNullException(nameof(options.MediaType)); } + + this.HttpClient = httpClient; + } + /// /// Send a via GET /// diff --git a/tests/GraphQL.Client.Tests/Http/HttpClientExtensionsTest.cs b/tests/GraphQL.Client.Tests/Http/HttpClientExtensionsTest.cs new file mode 100644 index 00000000..1738f0a2 --- /dev/null +++ b/tests/GraphQL.Client.Tests/Http/HttpClientExtensionsTest.cs @@ -0,0 +1,100 @@ +using System; +using System.Net.Http; +using GraphQL.Client.Http; +using GraphQL.Common.Request; +using GraphQL.Common.Tests.Model; +using Xunit; + +namespace GraphQL.Client.Tests.Http { + + public class HttpClientExtensionsTest : BaseGraphQLClientTest { + + public GraphQLHttpClient GraphQLHttpClient => new HttpClient().AsGraphQLClient(new GraphQLHttpClientOptions {EndPoint=new Uri( "https://swapi.apis.guru/") }); + + [Fact] + public async void QueryGetAsyncFact() { + var graphQLRequest = new GraphQLRequest { + Query = @" + { + person(personID: ""1"") { + name + } + }" + }; + var response = await this.GraphQLHttpClient.SendQueryAsync(graphQLRequest).ConfigureAwait(false); + + Assert.Equal("Luke Skywalker", response.Data.person.name.Value); + Assert.Equal("Luke Skywalker", response.GetDataFieldAs("person").Name); + } + + [Fact] + public async void OperationNameGetAsyncFact() { + var graphQLRequest = new GraphQLRequest { + Query = @" + query Person{ + person(personID: ""1"") { + name + } + } + + query Planet { + planet(planetID: ""1"") { + name + } + }", + OperationName = "Person" + }; + var response = await this.GraphQLHttpClient.SendQueryAsync(graphQLRequest).ConfigureAwait(false); + + Assert.Equal("Luke Skywalker", response.Data.person.name.Value); + Assert.Equal("Luke Skywalker", response.GetDataFieldAs("person").Name); + } + + [Fact] + public async void VariablesGetAsyncFact() { + var graphQLRequest = new GraphQLRequest { + Query = @" + query Person($personId: ID!){ + person(personID: $personId) { + name + } + }", + Variables = new { + personId = "1" + } + }; + var response = await this.GraphQLHttpClient.SendQueryAsync(graphQLRequest).ConfigureAwait(false); + + Assert.Equal("Luke Skywalker", response.Data.person.name.Value); + Assert.Equal("Luke Skywalker", response.GetDataFieldAs("person").Name); + } + + [Fact] + public async void OperationNameVariableGetAsyncFact() { + var graphQLRequest = new GraphQLRequest { + Query = @" + query Person($personId: ID!){ + person(personID: $personId) { + name + } + } + + query Planet { + planet(planetID: ""1"") { + name + } + }", + OperationName = "Person", + Variables = new { + personId = "1" + } + }; + var response = await this.GraphQLHttpClient.SendQueryAsync(graphQLRequest).ConfigureAwait(false); + + Assert.Equal("Luke Skywalker", response.Data.person.name.Value); + Assert.Equal("Luke Skywalker", response.GetDataFieldAs("person").Name); + } + + } + +}