Skip to content

Commit 0f178b3

Browse files
committed
nullable reference types
1 parent f388408 commit 0f178b3

7 files changed

Lines changed: 124 additions & 1 deletion

File tree

Tocsoft.GraphQLCodeGen.Cli/Templates/cs/CSharp.template

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ namespace {{Namespace}}
3636
{{!# jsonConverters }}
3737
{{!# BeforeInterfaceOperation}}
3838
{{!# StringifyEnums}} true
39+
{{!# Nullable }} disabled
3940

4041
{{!# JsonPropertyAttributeName}}
4142
{{~ifTemplate 'JsonConverter' 'Newtonsoft.Json'~}}JsonProperty{{~/ifTemplate~}}
@@ -427,5 +428,5 @@ namespace {{Namespace}}
427428
{{~#if isCollection }}IEnumerable<{{/if~}}
428429
{{~#if prefix }}{{@root.ClassName}}.{{/if~}}
429430
{{#if fixCase}}{{pascalCase name}}{{/if}}{{#unless fixCase}}{{name}}{{/unless}}
430-
{{~#if isValueType}}{{#if nullable}}?{{/if}}{{/if~}}
431+
{{~ifTemplate 'Nullable' 'enabled'~}}{{#if nullable}}?{{/if}}{{else}}{{~#if isValueType}}{{#if nullable}}?{{/if}}{{/if~}}{{~/ifTemplate~}}
431432
{{~#if isCollection}}>{{/if}}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
query q($temp: String!){
2+
testNonNull(id: $temp)
3+
{
4+
nullable,
5+
nonnullable
6+
}
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
query q($temp: String){
2+
test(id: $temp)
3+
{
4+
nullable,
5+
nonnullable
6+
}
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"root": true,
3+
"schema": "schema.gql",
4+
"output": "test.cs",
5+
"class": "Sample.Client.Test",
6+
7+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
2+
schema {
3+
query: Query
4+
}
5+
6+
type Query {
7+
testNonNull(id: string!): Droid!
8+
test(id: string): Droid
9+
}
10+
11+
12+
type Droid {
13+
nullable: Episode
14+
nonnullable: Episode!
15+
}
16+
17+
enum Episode {
18+
NEWHOPE
19+
EMPIRE
20+
JEDI
21+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
using System;
2+
using System.Linq;
3+
using System.Threading.Tasks;
4+
using Xunit;
5+
6+
namespace Tocsoft.GraphQLCodeGen.Tests
7+
{
8+
public class NullableReferenceTypes
9+
{
10+
private readonly CodeGeneratorTester tester;
11+
12+
public NullableReferenceTypes()
13+
{
14+
tester = new CodeGeneratorTester();
15+
}
16+
17+
[Fact]
18+
public async Task NullableWithValue_Enabled()
19+
{
20+
tester.AddQuery("./Files/NullableReferenceTypes/NullableWithValue.gql");
21+
tester.Configure(s =>
22+
{
23+
s.TemplateSettings["Nullable"] = "enabled";
24+
});
25+
26+
var code = await tester.Generate();
27+
await tester.Verify();
28+
29+
Assert.Contains("public Test.DroidResult? Test { get; set; }", code);
30+
Assert.Contains("Task<Test.NullableWithValueQResult> NullableWithValueQAsync(string? temp);", code);
31+
}
32+
[Fact]
33+
public async Task NonNullableWithValue_Enabled()
34+
{
35+
tester.AddQuery("./Files/NullableReferenceTypes/NonNullableWithValue.gql");
36+
tester.Configure(s =>
37+
{
38+
s.TemplateSettings["Nullable"] = "enabled";
39+
});
40+
41+
var code = await tester.Generate();
42+
await tester.Verify();
43+
Assert.Contains("public Test.DroidResult TestNonNull { get; set; }", code);
44+
Assert.Contains("Task<Test.NonNullableWithValueQResult> NonNullableWithValueQAsync(string temp);", code);
45+
}
46+
47+
[Fact]
48+
public async Task NullableWithValue_Disabled()
49+
{
50+
tester.AddQuery("./Files/NullableReferenceTypes/NullableWithValue.gql");
51+
tester.Configure(s =>
52+
{
53+
s.TemplateSettings["Nullable"] = "disabled";
54+
});
55+
56+
var code = await tester.Generate();
57+
await tester.Verify();
58+
Assert.Contains("public Test.DroidResult TestNonNull { get; set; }", code);
59+
Assert.Contains("Task<Test.NullableWithValueQResult> NullableWithValueQAsync(string temp);", code);
60+
}
61+
62+
[Fact]
63+
public async Task NonNullableWithValue_Disabled()
64+
{
65+
tester.AddQuery("./Files/NullableReferenceTypes/NonNullableWithValue.gql");
66+
tester.Configure(s =>
67+
{
68+
s.TemplateSettings["Nullable"] = "disabled";
69+
});
70+
71+
var code = await tester.Generate();
72+
await tester.Verify();
73+
Assert.Contains("public Test.DroidResult TestNonNull { get; set; }", code);
74+
Assert.Contains("Task<Test.NonNullableWithValueQResult> NonNullableWithValueQAsync(string temp);", code);
75+
}
76+
}
77+
}

Tocsoft.GraphQLCodeGen.Tests/Tocsoft.GraphQLCodeGen.Tests.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@
4242
<None Update="Files\SchemaFiles\gqlsettings.json">
4343
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
4444
</None>
45+
<None Update="Files\NullableReferenceTypes\gqlsettings.json">
46+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
47+
</None>
4548
<None Update="Files\ToggleJsonConverter\gqlsettings.json">
4649
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
4750
</None>

0 commit comments

Comments
 (0)