Skip to content

Commit c3b4d15

Browse files
authored
Merge pull request #153 from graphql-dotnet/per-request-headers
Passing per-request headers
2 parents ad95dfc + 9e608af commit c3b4d15

File tree

4 files changed

+48
-5
lines changed

4 files changed

+48
-5
lines changed

src/GraphQL.Client.Http/GraphQLHttpClient.cs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ public IObservable<GraphQLResponse<TResponse>> CreateSubscriptionStream<TRespons
103103

104104
private async Task<GraphQLResponse<TResponse>> SendHttpPostRequestAsync<TResponse>(GraphQLRequest request, CancellationToken cancellationToken = default) {
105105
var preprocessedRequest = await Options.PreprocessRequest(request, this);
106-
using var httpRequestMessage = this.GenerateHttpRequestMessage(preprocessedRequest.SerializeToJson(Options));
106+
using var httpRequestMessage = this.GenerateHttpRequestMessage(preprocessedRequest);
107107
using var httpResponseMessage = await this.HttpClient.SendAsync(httpRequestMessage, cancellationToken);
108108
if (!httpResponseMessage.IsSuccessStatusCode) {
109109
throw new GraphQLHttpException(httpResponseMessage);
@@ -113,10 +113,15 @@ private async Task<GraphQLResponse<TResponse>> SendHttpPostRequestAsync<TRespons
113113
return await bodyStream.DeserializeFromJsonAsync<GraphQLHttpResponse<TResponse>>(Options, cancellationToken);
114114
}
115115

116-
private HttpRequestMessage GenerateHttpRequestMessage(string requestString) {
117-
return new HttpRequestMessage(HttpMethod.Post, this.Options.EndPoint) {
118-
Content = new StringContent(requestString, Encoding.UTF8, "application/json")
116+
private HttpRequestMessage GenerateHttpRequestMessage(GraphQLRequest request) {
117+
var message = new HttpRequestMessage(HttpMethod.Post, this.Options.EndPoint) {
118+
Content = new StringContent(request.SerializeToJson(Options), Encoding.UTF8, "application/json")
119119
};
120+
121+
if (request is GraphQLHttpRequest httpRequest)
122+
httpRequest.PreprocessHttpRequestMessage(message);
123+
124+
return message;
120125
}
121126

122127
private Uri GetWebSocketUri() {
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,22 @@
1+
using System;
2+
using System.Net.Http;
3+
using System.Text.Json.Serialization;
4+
15
namespace GraphQL.Client.Http {
26

37
public class GraphQLHttpRequest : GraphQLRequest {
8+
public GraphQLHttpRequest()
9+
{
10+
}
11+
12+
public GraphQLHttpRequest(string query, object? variables = null, string? operationName = null) : base(query, variables, operationName)
13+
{
14+
}
15+
16+
/// <summary>
17+
/// Allows to preprocess a <see cref="HttpRequestMessage"/> before it is sent, i.e. add custom headers
18+
/// </summary>
19+
[JsonIgnore]
20+
public Action<HttpRequestMessage> PreprocessHttpRequestMessage { get; set; } = message => { };
421
}
522
}

tests/GraphQL.Integration.Tests/Helpers/WebHostHelpers.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public static TestServerSetup SetupTest<TStartup>(bool requestsViaWebsocket = fa
5050
public class TestServerSetup : IDisposable
5151
{
5252
public IWebHost Server { get; set; }
53-
public IGraphQLClient Client { get; set; }
53+
public GraphQLHttpClient Client { get; set; }
5454
public void Dispose()
5555
{
5656
Server?.Dispose();

tests/GraphQL.Integration.Tests/QueryAndMutationTests.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
using System.Net.Http;
12
using GraphQL.Client;
3+
using GraphQL.Client.Http;
24
using GraphQL.Integration.Tests.Helpers;
35
using GraphQL.Integration.Tests.TestData;
46
using IntegrationTestServer;
@@ -111,5 +113,24 @@ query Human($id: String!){
111113
Assert.Equal("Han Solo", queryResponse.Data.Human.Name);
112114
}
113115
}
116+
117+
[Fact]
118+
public async void PreprocessHttpRequestMessageIsCalled() {
119+
var callbackTester = new CallbackTester<HttpRequestMessage>();
120+
var graphQLRequest = new GraphQLHttpRequest($"{{ human(id: \"1\") {{ name }} }}") {
121+
PreprocessHttpRequestMessage = callbackTester.Callback
122+
};
123+
124+
using (var setup = SetupTest()) {
125+
var defaultHeaders = setup.Client.HttpClient.DefaultRequestHeaders;
126+
var response = await setup.Client.SendQueryAsync(graphQLRequest, () => new { Human = new { Name = string.Empty } })
127+
.ConfigureAwait(false);
128+
callbackTester.CallbackShouldHaveBeenInvoked(message => {
129+
Assert.Equal(defaultHeaders, message.Headers);
130+
});
131+
Assert.Null(response.Errors);
132+
Assert.Equal("Luke", response.Data.Human.Name);
133+
}
134+
}
114135
}
115136
}

0 commit comments

Comments
 (0)