Skip to content

Commit 6e38798

Browse files
authored
Merge pull request #198 from graphql-dotnet/remove-default-serializer
Remove default serializer
2 parents 843aec9 + 19382ad commit 6e38798

File tree

13 files changed

+25
-69
lines changed

13 files changed

+25
-69
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ Be careful when using `byte[]` in your variables object, as most JSON serializer
5151
### Execute Query/Mutation:
5252

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

5656
public class PersonAndFilmsResponse {
5757
public PersonContent Person { get; set; }

examples/GraphQL.Client.Example/GraphQL.Client.Example.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
</PropertyGroup>
88

99
<ItemGroup>
10+
<ProjectReference Include="..\..\src\GraphQL.Client.Serializer.Newtonsoft\GraphQL.Client.Serializer.Newtonsoft.csproj" />
1011
<ProjectReference Include="..\..\src\GraphQL.Client\GraphQL.Client.csproj" />
1112
</ItemGroup>
1213

examples/GraphQL.Client.Example/Program.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Text.Json;
44
using System.Threading.Tasks;
55
using GraphQL.Client.Http;
6+
using GraphQL.Client.Serializer.Newtonsoft;
67

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

1819
var personAndFilmsRequest = new GraphQLRequest
1920
{

src/GraphQL.Client.Serializer.Newtonsoft/MapConverter.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,6 @@ private IEnumerable<object> ReadArray(JToken element)
6161
}
6262
}
6363

64-
private object ReadNumber(JToken token) => ((JValue) token).Value;
65-
6664
private bool IsUnsupportedJTokenType(JTokenType type) => type == JTokenType.Constructor || type == JTokenType.Property || type == JTokenType.Comment;
6765
}
6866
}

src/GraphQL.Client/GraphQL.Client.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
<ItemGroup>
3737
<ProjectReference Include="..\GraphQL.Client.Abstractions.Websocket\GraphQL.Client.Abstractions.Websocket.csproj" />
3838
<ProjectReference Include="..\GraphQL.Client.Abstractions\GraphQL.Client.Abstractions.csproj" />
39-
<ProjectReference Include="..\GraphQL.Client.Serializer.Newtonsoft\GraphQL.Client.Serializer.Newtonsoft.csproj" />
4039
</ItemGroup>
4140

4241

src/GraphQL.Client/GraphQLHttpClient.cs

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
using GraphQL.Client.Abstractions;
99
using GraphQL.Client.Abstractions.Websocket;
1010
using GraphQL.Client.Http.Websocket;
11-
using GraphQL.Client.Serializer.Newtonsoft;
1211

1312
namespace GraphQL.Client.Http
1413
{
@@ -19,7 +18,11 @@ public class GraphQLHttpClient : IGraphQLClient
1918
private readonly GraphQLHttpWebSocket _graphQlHttpWebSocket;
2019
private readonly CancellationTokenSource _cancellationTokenSource = new CancellationTokenSource();
2120
private readonly ConcurrentDictionary<Tuple<GraphQLRequest, Type>, object> _subscriptionStreams = new ConcurrentDictionary<Tuple<GraphQLRequest, Type>, object>();
22-
private IGraphQLWebsocketJsonSerializer JsonSerializer => Options.JsonSerializer;
21+
22+
/// <summary>
23+
/// the json serializer
24+
/// </summary>
25+
public IGraphQLWebsocketJsonSerializer JsonSerializer { get; }
2326

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

4548
#region Constructors
4649

47-
public GraphQLHttpClient(string endPoint) : this(new Uri(endPoint)) { }
48-
49-
public GraphQLHttpClient(Uri endPoint) : this(o => o.EndPoint = endPoint) { }
50-
51-
public GraphQLHttpClient(Action<GraphQLHttpClientOptions> configure) : this(configure.New()) { }
50+
public GraphQLHttpClient(string endPoint, IGraphQLWebsocketJsonSerializer serializer) : this(new Uri(endPoint), serializer) { }
5251

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

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

57-
public GraphQLHttpClient(GraphQLHttpClientOptions options, HttpClient httpClient, IGraphQLWebsocketJsonSerializer serializer)
56+
public GraphQLHttpClient(GraphQLHttpClientOptions options, IGraphQLWebsocketJsonSerializer serializer) : this(options, serializer, new HttpClient(options.HttpMessageHandler)) { }
57+
58+
public GraphQLHttpClient(GraphQLHttpClientOptions options, IGraphQLWebsocketJsonSerializer serializer, HttpClient httpClient)
5859
{
5960
Options = options ?? throw new ArgumentNullException(nameof(options));
60-
Options.JsonSerializer = serializer ?? throw new ArgumentNullException(nameof(serializer));
61+
JsonSerializer = serializer ?? throw new ArgumentNullException(nameof(serializer));
6162
HttpClient = httpClient ?? throw new ArgumentNullException(nameof(httpClient));
6263
_graphQlHttpWebSocket = new GraphQLHttpWebSocket(GetWebSocketUri(), this);
6364
}

src/GraphQL.Client/GraphQLHttpClientOptions.cs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
using System.Net.Http;
33
using System.Net.Http.Headers;
44
using System.Threading.Tasks;
5-
using GraphQL.Client.Abstractions.Websocket;
65

76
namespace GraphQL.Client.Http
87
{
@@ -18,11 +17,6 @@ public class GraphQLHttpClientOptions
1817
/// </summary>
1918
public Uri EndPoint { get; set; }
2019

21-
/// <summary>
22-
/// the json serializer
23-
/// </summary>
24-
public IGraphQLWebsocketJsonSerializer JsonSerializer { get; set; }
25-
2620
/// <summary>
2721
/// The <see cref="System.Net.Http.HttpMessageHandler"/> that is going to be used
2822
/// </summary>

src/GraphQL.Client/HttpClientExtensions.cs

Lines changed: 0 additions & 20 deletions
This file was deleted.

src/GraphQL.Client/Websocket/GraphQLHttpWebSocket.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ public IObservable<GraphQLResponse<TResponse>> CreateSubscriptionStream<TRespons
139139
// post the GraphQLResponse to the stream (even if a GraphQL error occurred)
140140
Debug.WriteLine($"received payload on subscription {startRequest.Id} (thread {Thread.CurrentThread.ManagedThreadId})");
141141
var typedResponse =
142-
_client.Options.JsonSerializer.DeserializeToWebsocketResponse<TResponse>(
142+
_client.JsonSerializer.DeserializeToWebsocketResponse<TResponse>(
143143
response.MessageBytes);
144144
o.OnNext(typedResponse.Payload);
145145

@@ -296,7 +296,7 @@ public Task<GraphQLResponse<TResponse>> SendRequest<TResponse>(GraphQLRequest re
296296
{
297297
Debug.WriteLine($"received response for request {websocketRequest.Id}");
298298
var typedResponse =
299-
_client.Options.JsonSerializer.DeserializeToWebsocketResponse<TResponse>(
299+
_client.JsonSerializer.DeserializeToWebsocketResponse<TResponse>(
300300
response.MessageBytes);
301301
return typedResponse.Payload;
302302
});
@@ -353,7 +353,7 @@ private async Task SendWebSocketRequestAsync(GraphQLWebSocketRequest request)
353353
}
354354

355355
await InitializeWebSocket();
356-
var requestBytes = Options.JsonSerializer.SerializeToBytes(request);
356+
var requestBytes = _client.JsonSerializer.SerializeToBytes(request);
357357
await _clientWebSocket.SendAsync(
358358
new ArraySegment<byte>(requestBytes),
359359
WebSocketMessageType.Text,
@@ -554,7 +554,7 @@ private async Task<WebsocketMessageWrapper> ReceiveWebsocketMessagesAsync()
554554

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

tests/GraphQL.Integration.Tests/Extensions/WebApplicationFactoryExtensions.cs

Lines changed: 0 additions & 19 deletions
This file was deleted.

0 commit comments

Comments
 (0)