Skip to content

Commit 33316c5

Browse files
author
Raul Hidalgo Caballero
authored
Issue #44 (#54)
* Internal Use As GraphQL * Added Tests
1 parent 53489f6 commit 33316c5

File tree

4 files changed

+142
-0
lines changed

4 files changed

+142
-0
lines changed

src/GraphQL.Client/Http/GraphQLHttpClient.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Net.Http;
23
using System.Net.Http.Headers;
34
using System.Threading;
45
using System.Threading.Tasks;
@@ -89,6 +90,16 @@ public GraphQLHttpClient(GraphQLHttpClientOptions options) {
8990
this.graphQLHttpHandler = new GraphQLHttpHandler(options);
9091
}
9192

93+
internal GraphQLHttpClient(GraphQLHttpClientOptions options,HttpClient httpClient) {
94+
if (options == null) { throw new ArgumentNullException(nameof(options)); }
95+
if (options.EndPoint == null) { throw new ArgumentNullException(nameof(options.EndPoint)); }
96+
if (options.JsonSerializerSettings == null) { throw new ArgumentNullException(nameof(options.JsonSerializerSettings)); }
97+
if (options.HttpMessageHandler == null) { throw new ArgumentNullException(nameof(options.HttpMessageHandler)); }
98+
if (options.MediaType == null) { throw new ArgumentNullException(nameof(options.MediaType)); }
99+
100+
this.graphQLHttpHandler = new GraphQLHttpHandler(options,httpClient);
101+
}
102+
92103
public async Task<GraphQLResponse> SendQueryAsync(string query, CancellationToken cancellationToken = default) =>
93104
await this.SendQueryAsync(new GraphQLRequest { Query = query }, cancellationToken).ConfigureAwait(false);
94105

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using System.Net.Http;
2+
3+
namespace GraphQL.Client.Http {
4+
5+
/// <summary>
6+
/// Extensions for <see cref="HttpClient"/>
7+
/// </summary>
8+
public static class HttpClientExtensions {
9+
10+
/// <summary>
11+
/// Creates a <see cref="GraphQLHttpClient"/> from a <see cref="HttpClient"/>
12+
/// </summary>
13+
/// <param name="httpClient"></param>
14+
/// <param name="graphQLHttpClientOptions"></param>
15+
/// <returns></returns>
16+
public static GraphQLHttpClient AsGraphQLClient(this HttpClient httpClient, GraphQLHttpClientOptions graphQLHttpClientOptions) =>
17+
new GraphQLHttpClient(graphQLHttpClientOptions, httpClient);
18+
19+
}
20+
21+
}

src/GraphQL.Client/Http/Internal/GraphQLHttpHandler.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,16 @@ public GraphQLHttpHandler(GraphQLHttpClientOptions options) {
2626
this.HttpClient = new HttpClient(this.Options.HttpMessageHandler);
2727
}
2828

29+
public GraphQLHttpHandler(GraphQLHttpClientOptions options, HttpClient httpClient) {
30+
this.Options = options ?? throw new ArgumentNullException(nameof(options));
31+
if (options.EndPoint == null) { throw new ArgumentNullException(nameof(options.EndPoint)); }
32+
if (options.JsonSerializerSettings == null) { throw new ArgumentNullException(nameof(options.JsonSerializerSettings)); }
33+
if (options.HttpMessageHandler == null) { throw new ArgumentNullException(nameof(options.HttpMessageHandler)); }
34+
if (options.MediaType == null) { throw new ArgumentNullException(nameof(options.MediaType)); }
35+
36+
this.HttpClient = httpClient;
37+
}
38+
2939
/// <summary>
3040
/// Send a <see cref="GraphQLRequest"/> via GET
3141
/// </summary>
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
using System;
2+
using System.Net.Http;
3+
using GraphQL.Client.Http;
4+
using GraphQL.Common.Request;
5+
using GraphQL.Common.Tests.Model;
6+
using Xunit;
7+
8+
namespace GraphQL.Client.Tests.Http {
9+
10+
public class HttpClientExtensionsTest : BaseGraphQLClientTest {
11+
12+
public GraphQLHttpClient GraphQLHttpClient => new HttpClient().AsGraphQLClient(new GraphQLHttpClientOptions {EndPoint=new Uri( "https://swapi.apis.guru/") });
13+
14+
[Fact]
15+
public async void QueryGetAsyncFact() {
16+
var graphQLRequest = new GraphQLRequest {
17+
Query = @"
18+
{
19+
person(personID: ""1"") {
20+
name
21+
}
22+
}"
23+
};
24+
var response = await this.GraphQLHttpClient.SendQueryAsync(graphQLRequest).ConfigureAwait(false);
25+
26+
Assert.Equal("Luke Skywalker", response.Data.person.name.Value);
27+
Assert.Equal("Luke Skywalker", response.GetDataFieldAs<Person>("person").Name);
28+
}
29+
30+
[Fact]
31+
public async void OperationNameGetAsyncFact() {
32+
var graphQLRequest = new GraphQLRequest {
33+
Query = @"
34+
query Person{
35+
person(personID: ""1"") {
36+
name
37+
}
38+
}
39+
40+
query Planet {
41+
planet(planetID: ""1"") {
42+
name
43+
}
44+
}",
45+
OperationName = "Person"
46+
};
47+
var response = await this.GraphQLHttpClient.SendQueryAsync(graphQLRequest).ConfigureAwait(false);
48+
49+
Assert.Equal("Luke Skywalker", response.Data.person.name.Value);
50+
Assert.Equal("Luke Skywalker", response.GetDataFieldAs<Person>("person").Name);
51+
}
52+
53+
[Fact]
54+
public async void VariablesGetAsyncFact() {
55+
var graphQLRequest = new GraphQLRequest {
56+
Query = @"
57+
query Person($personId: ID!){
58+
person(personID: $personId) {
59+
name
60+
}
61+
}",
62+
Variables = new {
63+
personId = "1"
64+
}
65+
};
66+
var response = await this.GraphQLHttpClient.SendQueryAsync(graphQLRequest).ConfigureAwait(false);
67+
68+
Assert.Equal("Luke Skywalker", response.Data.person.name.Value);
69+
Assert.Equal("Luke Skywalker", response.GetDataFieldAs<Person>("person").Name);
70+
}
71+
72+
[Fact]
73+
public async void OperationNameVariableGetAsyncFact() {
74+
var graphQLRequest = new GraphQLRequest {
75+
Query = @"
76+
query Person($personId: ID!){
77+
person(personID: $personId) {
78+
name
79+
}
80+
}
81+
82+
query Planet {
83+
planet(planetID: ""1"") {
84+
name
85+
}
86+
}",
87+
OperationName = "Person",
88+
Variables = new {
89+
personId = "1"
90+
}
91+
};
92+
var response = await this.GraphQLHttpClient.SendQueryAsync(graphQLRequest).ConfigureAwait(false);
93+
94+
Assert.Equal("Luke Skywalker", response.Data.person.name.Value);
95+
Assert.Equal("Luke Skywalker", response.GetDataFieldAs<Person>("person").Name);
96+
}
97+
98+
}
99+
100+
}

0 commit comments

Comments
 (0)