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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ Be careful when using `byte[]` in your variables object, as most JSON serializer
### Execute Query/Mutation:

```csharp
var graphQLClient = new GraphQLHttpClient("https://swapi.apis.guru/");
var graphQLClient = new GraphQLHttpClient("https://swapi.apis.guru/", new NewtonsoftJsonSerializer());

public class PersonAndFilmsResponse {
public PersonContent Person { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\GraphQL.Client.Serializer.Newtonsoft\GraphQL.Client.Serializer.Newtonsoft.csproj" />
<ProjectReference Include="..\..\src\GraphQL.Client\GraphQL.Client.csproj" />
</ItemGroup>

Expand Down
3 changes: 2 additions & 1 deletion examples/GraphQL.Client.Example/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Text.Json;
using System.Threading.Tasks;
using GraphQL.Client.Http;
using GraphQL.Client.Serializer.Newtonsoft;

namespace GraphQL.Client.Example
{
Expand All @@ -13,7 +14,7 @@ public class Program
public static async Task Main(string[] args)
{
_ = args;
using var graphQLClient = new GraphQLHttpClient("https://swapi.apis.guru/");
using var graphQLClient = new GraphQLHttpClient("https://swapi.apis.guru/", new NewtonsoftJsonSerializer());

var personAndFilmsRequest = new GraphQLRequest
{
Expand Down
2 changes: 0 additions & 2 deletions src/GraphQL.Client.Serializer.Newtonsoft/MapConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,6 @@ private IEnumerable<object> ReadArray(JToken element)
}
}

private object ReadNumber(JToken token) => ((JValue) token).Value;

private bool IsUnsupportedJTokenType(JTokenType type) => type == JTokenType.Constructor || type == JTokenType.Property || type == JTokenType.Comment;
}
}
1 change: 0 additions & 1 deletion src/GraphQL.Client/GraphQL.Client.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
<ItemGroup>
<ProjectReference Include="..\GraphQL.Client.Abstractions.Websocket\GraphQL.Client.Abstractions.Websocket.csproj" />
<ProjectReference Include="..\GraphQL.Client.Abstractions\GraphQL.Client.Abstractions.csproj" />
<ProjectReference Include="..\GraphQL.Client.Serializer.Newtonsoft\GraphQL.Client.Serializer.Newtonsoft.csproj" />
</ItemGroup>


Expand Down
23 changes: 12 additions & 11 deletions src/GraphQL.Client/GraphQLHttpClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
using GraphQL.Client.Abstractions;
using GraphQL.Client.Abstractions.Websocket;
using GraphQL.Client.Http.Websocket;
using GraphQL.Client.Serializer.Newtonsoft;

namespace GraphQL.Client.Http
{
Expand All @@ -19,7 +18,11 @@ public class GraphQLHttpClient : IGraphQLClient
private readonly GraphQLHttpWebSocket _graphQlHttpWebSocket;
private readonly CancellationTokenSource _cancellationTokenSource = new CancellationTokenSource();
private readonly ConcurrentDictionary<Tuple<GraphQLRequest, Type>, object> _subscriptionStreams = new ConcurrentDictionary<Tuple<GraphQLRequest, Type>, object>();
private IGraphQLWebsocketJsonSerializer JsonSerializer => Options.JsonSerializer;

/// <summary>
/// the json serializer
/// </summary>
public IGraphQLWebsocketJsonSerializer JsonSerializer { get; }

/// <summary>
/// the instance of <see cref="HttpClient"/> which is used internally
Expand All @@ -44,20 +47,18 @@ public class GraphQLHttpClient : IGraphQLClient

#region Constructors

public GraphQLHttpClient(string endPoint) : this(new Uri(endPoint)) { }

public GraphQLHttpClient(Uri endPoint) : this(o => o.EndPoint = endPoint) { }

public GraphQLHttpClient(Action<GraphQLHttpClientOptions> configure) : this(configure.New()) { }
public GraphQLHttpClient(string endPoint, IGraphQLWebsocketJsonSerializer serializer) : this(new Uri(endPoint), serializer) { }

public GraphQLHttpClient(GraphQLHttpClientOptions options) : this(options, new HttpClient(options.HttpMessageHandler)) { }
public GraphQLHttpClient(Uri endPoint, IGraphQLWebsocketJsonSerializer serializer) : this(o => o.EndPoint = endPoint, serializer) { }

public GraphQLHttpClient(GraphQLHttpClientOptions options, HttpClient httpClient) : this(options, httpClient, new NewtonsoftJsonSerializer()) { }
public GraphQLHttpClient(Action<GraphQLHttpClientOptions> configure, IGraphQLWebsocketJsonSerializer serializer) : this(configure.New(), serializer) { }

public GraphQLHttpClient(GraphQLHttpClientOptions options, HttpClient httpClient, IGraphQLWebsocketJsonSerializer serializer)
public GraphQLHttpClient(GraphQLHttpClientOptions options, IGraphQLWebsocketJsonSerializer serializer) : this(options, serializer, new HttpClient(options.HttpMessageHandler)) { }

public GraphQLHttpClient(GraphQLHttpClientOptions options, IGraphQLWebsocketJsonSerializer serializer, HttpClient httpClient)
{
Options = options ?? throw new ArgumentNullException(nameof(options));
Options.JsonSerializer = serializer ?? throw new ArgumentNullException(nameof(serializer));
JsonSerializer = serializer ?? throw new ArgumentNullException(nameof(serializer));
HttpClient = httpClient ?? throw new ArgumentNullException(nameof(httpClient));
_graphQlHttpWebSocket = new GraphQLHttpWebSocket(GetWebSocketUri(), this);
}
Expand Down
6 changes: 0 additions & 6 deletions src/GraphQL.Client/GraphQLHttpClientOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
using GraphQL.Client.Abstractions.Websocket;

namespace GraphQL.Client.Http
{
Expand All @@ -18,11 +17,6 @@ public class GraphQLHttpClientOptions
/// </summary>
public Uri EndPoint { get; set; }

/// <summary>
/// the json serializer
/// </summary>
public IGraphQLWebsocketJsonSerializer JsonSerializer { get; set; }

/// <summary>
/// The <see cref="System.Net.Http.HttpMessageHandler"/> that is going to be used
/// </summary>
Expand Down
20 changes: 0 additions & 20 deletions src/GraphQL.Client/HttpClientExtensions.cs

This file was deleted.

8 changes: 4 additions & 4 deletions src/GraphQL.Client/Websocket/GraphQLHttpWebSocket.cs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ public IObservable<GraphQLResponse<TResponse>> CreateSubscriptionStream<TRespons
// post the GraphQLResponse to the stream (even if a GraphQL error occurred)
Debug.WriteLine($"received payload on subscription {startRequest.Id} (thread {Thread.CurrentThread.ManagedThreadId})");
var typedResponse =
_client.Options.JsonSerializer.DeserializeToWebsocketResponse<TResponse>(
_client.JsonSerializer.DeserializeToWebsocketResponse<TResponse>(
response.MessageBytes);
o.OnNext(typedResponse.Payload);

Expand Down Expand Up @@ -296,7 +296,7 @@ public Task<GraphQLResponse<TResponse>> SendRequest<TResponse>(GraphQLRequest re
{
Debug.WriteLine($"received response for request {websocketRequest.Id}");
var typedResponse =
_client.Options.JsonSerializer.DeserializeToWebsocketResponse<TResponse>(
_client.JsonSerializer.DeserializeToWebsocketResponse<TResponse>(
response.MessageBytes);
return typedResponse.Payload;
});
Expand Down Expand Up @@ -353,7 +353,7 @@ private async Task SendWebSocketRequestAsync(GraphQLWebSocketRequest request)
}

await InitializeWebSocket();
var requestBytes = Options.JsonSerializer.SerializeToBytes(request);
var requestBytes = _client.JsonSerializer.SerializeToBytes(request);
await _clientWebSocket.SendAsync(
new ArraySegment<byte>(requestBytes),
WebSocketMessageType.Text,
Expand Down Expand Up @@ -554,7 +554,7 @@ private async Task<WebsocketMessageWrapper> ReceiveWebsocketMessagesAsync()

if (webSocketReceiveResult.MessageType == WebSocketMessageType.Text)
{
var response = await Options.JsonSerializer.DeserializeToWebsocketResponseWrapperAsync(ms);
var response = await _client.JsonSerializer.DeserializeToWebsocketResponseWrapperAsync(ms);
response.MessageBytes = ms.ToArray();
Debug.WriteLine($"{response.MessageBytes.Length} bytes received for id {response.Id} on websocket {_clientWebSocket.GetHashCode()} (thread {Thread.CurrentThread.ManagedThreadId})...");
return response;
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,5 @@
<ProjectReference Include="..\IntegrationTestServer\IntegrationTestServer.csproj" />
</ItemGroup>


</Project>
6 changes: 3 additions & 3 deletions tests/GraphQL.Integration.Tests/Helpers/WebHostHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ public static GraphQLHttpClient GetGraphQLClient(int port, string endpoint, bool
=> new GraphQLHttpClient(new GraphQLHttpClientOptions
{
EndPoint = new Uri($"http://localhost:{port}{endpoint}"),
UseWebSocketForQueriesAndMutations = requestsViaWebsocket,
JsonSerializer = serializer ?? new NewtonsoftJsonSerializer()
});
UseWebSocketForQueriesAndMutations = requestsViaWebsocket
},
serializer ?? new NewtonsoftJsonSerializer());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you have tests for STJ serializer?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, there is always an abstract base class containing the tests and a specific testclass for each serializer (currently Newtonsoft and System.Text.Json)

}

public class TestServerSetup : IDisposable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public async void QueryAsHttpResponseTheory(int id, string name)
httpResponse.ResponseHeaders.Date.Should().BeCloseTo(DateTimeOffset.Now, TimeSpan.FromMinutes(1));
}

[Theory]
[Theory(Skip = "System.Json.Net deserializes 'dynamic' as JsonElement.")]
[ClassData(typeof(StarWarsHumans))]
public async void QueryWithDynamicReturnTypeTheory(int id, string name)
{
Expand Down