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
13 changes: 9 additions & 4 deletions src/GraphQL.Client.Http/GraphQLHttpClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public IObservable<GraphQLResponse<TResponse>> CreateSubscriptionStream<TRespons

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

private HttpRequestMessage GenerateHttpRequestMessage(string requestString) {
return new HttpRequestMessage(HttpMethod.Post, this.Options.EndPoint) {
Content = new StringContent(requestString, Encoding.UTF8, "application/json")
private HttpRequestMessage GenerateHttpRequestMessage(GraphQLRequest request) {
var message = new HttpRequestMessage(HttpMethod.Post, this.Options.EndPoint) {
Content = new StringContent(request.SerializeToJson(Options), Encoding.UTF8, "application/json")
};

if (request is GraphQLHttpRequest httpRequest)
httpRequest.PreprocessHttpRequestMessage(message);

return message;
}

private Uri GetWebSocketUri() {
Expand Down
17 changes: 17 additions & 0 deletions src/GraphQL.Client.Http/GraphQLHttpRequest.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,22 @@
using System;
using System.Net.Http;
using System.Text.Json.Serialization;

namespace GraphQL.Client.Http {

public class GraphQLHttpRequest : GraphQLRequest {
public GraphQLHttpRequest()
{
}

public GraphQLHttpRequest(string query, object? variables = null, string? operationName = null) : base(query, variables, operationName)
{
}

/// <summary>
/// Allows to preprocess a <see cref="HttpRequestMessage"/> before it is sent, i.e. add custom headers
/// </summary>
[JsonIgnore]
public Action<HttpRequestMessage> PreprocessHttpRequestMessage { get; set; } = message => { };
}
}
2 changes: 1 addition & 1 deletion tests/GraphQL.Integration.Tests/Helpers/WebHostHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public static TestServerSetup SetupTest<TStartup>(bool requestsViaWebsocket = fa
public class TestServerSetup : IDisposable
{
public IWebHost Server { get; set; }
public IGraphQLClient Client { get; set; }
public GraphQLHttpClient Client { get; set; }
public void Dispose()
{
Server?.Dispose();
Expand Down
21 changes: 21 additions & 0 deletions tests/GraphQL.Integration.Tests/QueryAndMutationTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System.Net.Http;
using GraphQL.Client;
using GraphQL.Client.Http;
using GraphQL.Integration.Tests.Helpers;
using GraphQL.Integration.Tests.TestData;
using IntegrationTestServer;
Expand Down Expand Up @@ -111,5 +113,24 @@ query Human($id: String!){
Assert.Equal("Han Solo", queryResponse.Data.Human.Name);
}
}

[Fact]
public async void PreprocessHttpRequestMessageIsCalled() {
var callbackTester = new CallbackTester<HttpRequestMessage>();
var graphQLRequest = new GraphQLHttpRequest($"{{ human(id: \"1\") {{ name }} }}") {
PreprocessHttpRequestMessage = callbackTester.Callback
};

using (var setup = SetupTest()) {
var defaultHeaders = setup.Client.HttpClient.DefaultRequestHeaders;
var response = await setup.Client.SendQueryAsync(graphQLRequest, () => new { Human = new { Name = string.Empty } })
.ConfigureAwait(false);
callbackTester.CallbackShouldHaveBeenInvoked(message => {
Assert.Equal(defaultHeaders, message.Headers);
});
Assert.Null(response.Errors);
Assert.Equal("Luke", response.Data.Human.Name);
}
}
}
}