Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/GraphQL.Client/Http/GraphQLHttpClient.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading;
using System.Threading.Tasks;
Expand Down Expand Up @@ -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<GraphQLResponse> SendQueryAsync(string query, CancellationToken cancellationToken = default) =>
await this.SendQueryAsync(new GraphQLRequest { Query = query }, cancellationToken).ConfigureAwait(false);

Expand Down
21 changes: 21 additions & 0 deletions src/GraphQL.Client/Http/HttpClientExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System.Net.Http;

namespace GraphQL.Client.Http {

/// <summary>
/// Extensions for <see cref="HttpClient"/>
/// </summary>
public static class HttpClientExtensions {

/// <summary>
/// Creates a <see cref="GraphQLHttpClient"/> from a <see cref="HttpClient"/>
/// </summary>
/// <param name="httpClient"></param>
/// <param name="graphQLHttpClientOptions"></param>
/// <returns></returns>
public static GraphQLHttpClient AsGraphQLClient(this HttpClient httpClient, GraphQLHttpClientOptions graphQLHttpClientOptions) =>
new GraphQLHttpClient(graphQLHttpClientOptions, httpClient);

}

}
10 changes: 10 additions & 0 deletions src/GraphQL.Client/Http/Internal/GraphQLHttpHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/// <summary>
/// Send a <see cref="GraphQLRequest"/> via GET
/// </summary>
Expand Down
100 changes: 100 additions & 0 deletions tests/GraphQL.Client.Tests/Http/HttpClientExtensionsTest.cs
Original file line number Diff line number Diff line change
@@ -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>("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>("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>("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>("person").Name);
}

}

}