Skip to content

Commit cadd54f

Browse files
author
Raul Hidalgo Caballero
authored
Nullable check (#86)
* nonnullable * fix some things * other fix * netstandard2.0
1 parent b6e84bf commit cadd54f

15 files changed

+187
-247
lines changed

root.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<Description>A GraphQL Client for .NET Standard</Description>
77
<GenerateAssemblyInfo>True</GenerateAssemblyInfo>
88
<GenerateDocumentationFile>True</GenerateDocumentationFile>
9-
<LangVersion>latest</LangVersion>
9+
<LangVersion>8.0</LangVersion>
1010
<NeutralLanguage>en-US</NeutralLanguage>
1111
<NoWarn>CS0618;CS1591;CS1701</NoWarn>
1212
<PackageIconUrl>https://raw.githubusercontent.com/graphql-dotnet/graphql-client/master/assets/logo.64x64.png</PackageIconUrl>

src/GraphQL.Client/GraphQL.Client.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
<?xml version="1.0" encoding="utf-8"?>
1+
<?xml version="1.0" encoding="utf-8"?>
22
<Project Sdk="Microsoft.NET.Sdk">
33

44
<Import Project="../src.props" />
55

66
<PropertyGroup>
77
<Description>A GraphQL Client</Description>
8-
<TargetFrameworks>netstandard1.3;netstandard2.0</TargetFrameworks>
8+
<TargetFramework>netstandard2.0</TargetFramework>
99
</PropertyGroup>
1010

1111
<ItemGroup>

src/GraphQL.Client/Http/GraphQLHttpClient.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,20 +101,20 @@ internal GraphQLHttpClient(GraphQLHttpClientOptions options, HttpClient httpClie
101101
}
102102

103103
public Task<GraphQLResponse> SendQueryAsync(string query, CancellationToken cancellationToken = default) =>
104-
this.SendQueryAsync(new GraphQLRequest { Query = query }, cancellationToken);
104+
this.SendQueryAsync(new GraphQLRequest(query), cancellationToken);
105105

106106
public Task<GraphQLResponse> SendQueryAsync(GraphQLRequest request, CancellationToken cancellationToken = default) =>
107107
this.graphQLHttpHandler.PostAsync(request, cancellationToken);
108108

109109
public Task<GraphQLResponse> SendMutationAsync(string query, CancellationToken cancellationToken = default) =>
110-
this.SendMutationAsync(new GraphQLRequest { Query = query }, cancellationToken);
110+
this.SendMutationAsync(new GraphQLRequest(query), cancellationToken);
111111

112112
public Task<GraphQLResponse> SendMutationAsync(GraphQLRequest request, CancellationToken cancellationToken = default) =>
113113
this.graphQLHttpHandler.PostAsync(request, cancellationToken);
114114

115115
[Obsolete("EXPERIMENTAL API")]
116116
public Task<IGraphQLSubscriptionResult> SendSubscribeAsync(string query, CancellationToken cancellationToken = default) =>
117-
this.SendSubscribeAsync(new GraphQLRequest { Query = query }, cancellationToken);
117+
this.SendSubscribeAsync(new GraphQLRequest(query), cancellationToken);
118118

119119
[Obsolete("EXPERIMENTAL API")]
120120
public Task<IGraphQLSubscriptionResult> SendSubscribeAsync(GraphQLRequest request, CancellationToken cancellationToken = default) {

src/GraphQL.Client/Obsolete.GraphQLClient.Extensions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,8 @@ fragment TypeRef on __Type {
9999
}
100100
}";
101101

102-
private static readonly GraphQLRequest IntrospectionGraphQLRequest = new GraphQLRequest {
103-
Query = IntrospectionQuery.Replace("\t", "").Replace("\n", "").Replace("\r", ""),
102+
private static readonly GraphQLRequest IntrospectionGraphQLRequest = new GraphQLRequest(
103+
IntrospectionQuery.Replace("\t", "").Replace("\n", "").Replace("\r", "")){
104104
Variables = null
105105
};
106106

src/GraphQL.Client/Obsolete.GraphQLClient.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public GraphQLClient(GraphQLClientOptions options) : base(options) { }
5353
/// <param name="cancellationToken">A cancellation token that can be used by other objects or threads to receive notice of cancellation.</param>
5454
/// <returns>The Response</returns>
5555
public Task<GraphQLResponse> GetQueryAsync(string query, CancellationToken cancellationToken = default) =>
56-
this.GetAsync(new GraphQLRequest { Query = query }, cancellationToken);
56+
this.GetAsync(new GraphQLRequest(query), cancellationToken);
5757

5858
/// <summary>
5959
/// Send a <see cref="GraphQLRequest"/> via GET
@@ -71,7 +71,7 @@ public Task<GraphQLResponse> GetAsync(GraphQLRequest request, CancellationToken
7171
/// <param name="cancellationToken">A cancellation token that can be used by other objects or threads to receive notice of cancellation.</param>
7272
/// <returns>The Response</returns>
7373
public Task<GraphQLResponse> PostQueryAsync(string query, CancellationToken cancellationToken = default) =>
74-
this.PostAsync(new GraphQLRequest { Query = query }, cancellationToken);
74+
this.PostAsync(new GraphQLRequest(query), cancellationToken);
7575

7676
/// <summary>
7777
/// Send a <see cref="GraphQLRequest"/> via POST

src/GraphQL.Common/GraphQL.Common.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?xml version="1.0" encoding="utf-8"?>
1+
<?xml version="1.0" encoding="utf-8"?>
22
<Project Sdk="Microsoft.NET.Sdk">
33

44
<Import Project="../src.props" />
@@ -8,7 +8,7 @@
88
</PropertyGroup>
99

1010
<PropertyGroup>
11-
<TargetFrameworks>netstandard1.0;netstandard2.0</TargetFrameworks>
11+
<TargetFramework>netstandard2.0</TargetFramework>
1212
</PropertyGroup>
1313

1414
<ItemGroup>

src/GraphQL.Common/Request/GraphQLRequest.cs

Lines changed: 18 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#nullable enable
12
using System;
23
using System.Collections.Generic;
34

@@ -7,7 +8,7 @@ namespace GraphQL.Common.Request {
78
/// Represents a Query that can be fetched to a GraphQL Server.
89
/// For more information <see href="http://graphql.org/learn/serving-over-http/#post-request"/>
910
/// </summary>
10-
public class GraphQLRequest : IEquatable<GraphQLRequest> {
11+
public class GraphQLRequest : IEquatable<GraphQLRequest?> {
1112

1213
/// <summary>
1314
/// The Query
@@ -17,50 +18,40 @@ public class GraphQLRequest : IEquatable<GraphQLRequest> {
1718
/// <summary>
1819
/// If the provided <see cref="Query"/> contains multiple named operations, this specifies which operation should be executed.
1920
/// </summary>
20-
public string OperationName { get; set; }
21+
public string? OperationName { get; set; }
2122

2223
/// <summary>
2324
/// The Variables
2425
/// </summary>
25-
public dynamic Variables { get; set; }
26+
public dynamic? Variables { get; set; }
27+
28+
/// <summary>
29+
/// Initialize a new GraphQLRequest
30+
/// </summary>
31+
/// <param name="query">The Query</param>
32+
public GraphQLRequest(string query){
33+
this.Query = query;
34+
}
2635

2736
/// <inheritdoc />
28-
public override bool Equals(object obj) => this.Equals(obj as GraphQLRequest);
37+
public override bool Equals(object? obj) => this.Equals(obj as GraphQLRequest);
2938

3039
/// <inheritdoc />
31-
public bool Equals(GraphQLRequest other) {
40+
public bool Equals(GraphQLRequest? other) {
3241
if (other == null) {
3342
return false;
3443
}
35-
if (ReferenceEquals(this, other)) {
36-
return true;
37-
}
38-
if (!Equals(this.Query, other.Query)) {
39-
return false;
40-
}
41-
if (!Equals(this.OperationName, other.OperationName)) {
42-
return false;
43-
}
44-
if (!Equals(this.Variables, other.Variables)) {
45-
return false;
46-
}
47-
return true;
44+
return EqualityComparer<GraphQLRequest>.Default.Equals(this, other);
4845
}
4946

5047
/// <inheritdoc />
51-
public override int GetHashCode() {
52-
var hashCode = -689803966;
53-
hashCode = hashCode * -1521134295 + EqualityComparer<string>.Default.GetHashCode(this.Query);
54-
hashCode = hashCode * -1521134295 + EqualityComparer<string>.Default.GetHashCode(this.OperationName);
55-
hashCode = hashCode * -1521134295 + EqualityComparer<dynamic>.Default.GetHashCode(this.Variables);
56-
return hashCode;
57-
}
48+
public override int GetHashCode() => EqualityComparer<GraphQLRequest>.Default.GetHashCode(this);
5849

5950
/// <inheritdoc />
60-
public static bool operator ==(GraphQLRequest request1, GraphQLRequest request2) => EqualityComparer<GraphQLRequest>.Default.Equals(request1, request2);
51+
public static bool operator ==(GraphQLRequest? request1, GraphQLRequest? request2) => EqualityComparer<GraphQLRequest?>.Default.Equals(request1, request2);
6152

6253
/// <inheritdoc />
63-
public static bool operator !=(GraphQLRequest request1, GraphQLRequest request2) => !(request1 == request2);
54+
public static bool operator !=(GraphQLRequest? request1, GraphQLRequest? request2) => !(request1 == request2);
6455

6556
}
6657

tests/GraphQL.Client.Tests/GraphQLClientGetTests.cs

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,12 @@ public class GraphQLClientGetTests : BaseGraphQLClientTest {
88

99
[Fact]
1010
public async void QueryGetAsyncFact() {
11-
var graphQLRequest = new GraphQLRequest {
12-
Query = @"
11+
var graphQLRequest = new GraphQLRequest(@"
1312
{
1413
person(personID: ""1"") {
1514
name
1615
}
17-
}"
18-
};
16+
}");
1917
var response = await this.GraphQLClient.GetAsync(graphQLRequest).ConfigureAwait(false);
2018

2119
Assert.Equal("Luke Skywalker", response.Data.person.name.Value);
@@ -24,8 +22,7 @@ public async void QueryGetAsyncFact() {
2422

2523
[Fact]
2624
public async void OperationNameGetAsyncFact() {
27-
var graphQLRequest = new GraphQLRequest {
28-
Query = @"
25+
var graphQLRequest = new GraphQLRequest(@"
2926
query Person{
3027
person(personID: ""1"") {
3128
name
@@ -36,7 +33,7 @@ query Planet {
3633
planet(planetID: ""1"") {
3734
name
3835
}
39-
}",
36+
}") {
4037
OperationName = "Person"
4138
};
4239
var response = await this.GraphQLClient.GetAsync(graphQLRequest).ConfigureAwait(false);
@@ -47,13 +44,12 @@ query Planet {
4744

4845
[Fact]
4946
public async void VariablesGetAsyncFact() {
50-
var graphQLRequest = new GraphQLRequest {
51-
Query = @"
47+
var graphQLRequest = new GraphQLRequest (@"
5248
query Person($personId: ID!){
5349
person(personID: $personId) {
5450
name
5551
}
56-
}",
52+
}") {
5753
Variables = new {
5854
personId = "1"
5955
}
@@ -66,8 +62,7 @@ query Person($personId: ID!){
6662

6763
[Fact]
6864
public async void OperationNameVariableGetAsyncFact() {
69-
var graphQLRequest = new GraphQLRequest {
70-
Query = @"
65+
var graphQLRequest = new GraphQLRequest(@"
7166
query Person($personId: ID!){
7267
person(personID: $personId) {
7368
name
@@ -78,7 +73,7 @@ query Planet {
7873
planet(planetID: ""1"") {
7974
name
8075
}
81-
}",
76+
}"){
8277
OperationName = "Person",
8378
Variables = new {
8479
personId = "1"

tests/GraphQL.Client.Tests/GraphQLClientPostTests.cs

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,32 +9,28 @@ public class GraphQLClientPostTests : BaseGraphQLClientTest {
99

1010
[Fact]
1111
public async void QueryPostAsyncFact() {
12-
var graphQLRequest = new GraphQLRequest {
13-
Query = @"
12+
var graphQLRequest = new GraphQLRequest(@"
1413
{
1514
person(personID: ""1"") {
1615
name
1716
}
1817
}"
19-
};
18+
);
2019
var response = await this.GraphQLClient.PostAsync(graphQLRequest).ConfigureAwait(false);
2120

2221
Assert.Equal("Luke Skywalker", response.Data.person.name.Value);
2322
Assert.Equal("Luke Skywalker", response.GetDataFieldAs<Person>("person").Name);
2423
}
2524

2625
[Fact]
27-
public async void QueryPostAsyncWithoutUtf8EncodingFact()
28-
{
29-
var graphQLRequest = new GraphQLRequest
30-
{
31-
Query = @"
26+
public async void QueryPostAsyncWithoutUtf8EncodingFact(){
27+
var graphQLRequest = new GraphQLRequest(@"
3228
{
3329
person(personID: ""1"") {
3430
name
3531
}
3632
}"
37-
};
33+
);
3834
this.GraphQLClient.Options.MediaType = MediaTypeHeaderValue.Parse("application/json");
3935
var response = await this.GraphQLClient.PostAsync(graphQLRequest).ConfigureAwait(false);
4036

@@ -44,8 +40,7 @@ public async void QueryPostAsyncWithoutUtf8EncodingFact()
4440

4541
[Fact]
4642
public async void OperationNamePostAsyncFact() {
47-
var graphQLRequest = new GraphQLRequest {
48-
Query = @"
43+
var graphQLRequest = new GraphQLRequest(@"
4944
query Person{
5045
person(personID: ""1"") {
5146
name
@@ -56,7 +51,7 @@ query Planet {
5651
planet(planetID: ""1"") {
5752
name
5853
}
59-
}",
54+
}") {
6055
OperationName = "Person"
6156
};
6257
var response = await this.GraphQLClient.PostAsync(graphQLRequest).ConfigureAwait(false);
@@ -67,13 +62,12 @@ query Planet {
6762

6863
[Fact]
6964
public async void VariablesPostAsyncFact() {
70-
var graphQLRequest = new GraphQLRequest {
71-
Query = @"
65+
var graphQLRequest = new GraphQLRequest(@"
7266
query Person($personId: ID!){
7367
person(personID: $personId) {
7468
name
7569
}
76-
}",
70+
}") {
7771
Variables = new {
7872
personId = "1"
7973
}
@@ -86,8 +80,7 @@ query Person($personId: ID!){
8680

8781
[Fact]
8882
public async void OperationNameVariablePostAsyncFact() {
89-
var graphQLRequest = new GraphQLRequest {
90-
Query = @"
83+
var graphQLRequest = new GraphQLRequest(@"
9184
query Person($personId: ID!){
9285
person(personID: $personId) {
9386
name
@@ -98,7 +91,7 @@ query Planet {
9891
planet(planetID: ""1"") {
9992
name
10093
}
101-
}",
94+
}") {
10295
OperationName = "Person",
10396
Variables = new {
10497
personId = "1"

0 commit comments

Comments
 (0)