Skip to content

Commit 5daacd2

Browse files
authored
Merge pull request #42 from Linq2GraphQL/input-type-required
Input type required
2 parents da7a78c + 72f9fc2 commit 5daacd2

26 files changed

+245
-271
lines changed

docs/Linq2GraphQL.Docs/Components/GenerateClient.razor.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ private async Task CopyIntrospection()
5757
await toastService.AddToastAsync(new ToastModel
5858
{
5959
Title = "Copy Complete",
60-
Message = "Introspection queryExecute has been copied to the clipboard"
60+
Message = "Introspection query has been copied to the clipboard"
6161
});
6262

6363
}
@@ -79,6 +79,12 @@ private async Task SaveEntriesAsync(List<FileEntry> entries)
7979
memoryStream.Seek(0, SeekOrigin.Begin);
8080
await tablerService.SaveAsBinary($"{options.ClientName}.zip", "application/zip", memoryStream.ToArray());
8181

82+
await toastService.AddToastAsync(new ToastModel
83+
{
84+
Title = "Generate Complete",
85+
Message = $"{options.ClientName}.zip has been been created! Please check you downloads."
86+
});
87+
8288
}
8389

8490
private async Task GenerateClientJson()
@@ -89,6 +95,7 @@ private async Task GenerateClientJson()
8995
var generator = new Generator.ClientGenerator(options.Namespace, options.ClientName, options.IncludeSubscriptions, EnumGeneratorStrategy.FailIfMissing, options.Nullable);
9096
var entries = generator.Generate(options.Schema);
9197
await SaveEntriesAsync(entries);
98+
9299
}
93100
catch (Exception ex)
94101
{

src/Linq2GraphQL.Generator/ClientGenerator.cs

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,19 @@ public ClientGenerator(string namespaceName, string clientName, bool includeSubs
3131

3232
private void AddFile(string directory, string fileName, string content)
3333
{
34+
var infoText = $@"
35+
//---------------------------------------------------------------------
36+
// This code was automatically generated by Linq2GraphQL
37+
// Please don't edit this file
38+
// Github:https://github.com/linq2graphql/linq2graphql.client
39+
// Url: https://linq2graphql.com
40+
// Generation Date: {DateTimeOffset.Now.ToString("F")}
41+
//---------------------------------------------------------------------
42+
43+
";
44+
45+
content = infoText + content;
46+
3447
entries.Add(new FileEntry { Content = content, DirectoryName = directory, FileName = fileName });
3548
}
3649

@@ -125,9 +138,18 @@ public List<FileEntry> Generate(string schemaJson)
125138
AddFile("Inputs", classType.FileName, classText);
126139
}
127140

128-
Console.WriteLine("Generate InputFactory...");
129-
var inputFactoryText = new InputFactoryClassTemplate(inputs, namespaceName).TransformText();
130-
AddFile("Inputs", "InputFactory.cs", inputFactoryText);
141+
if (GeneratorSettings.Current.Nullable)
142+
{
143+
Console.WriteLine("No InputFactory for a Nullable client ");
144+
}
145+
else
146+
{
147+
Console.WriteLine("Generate InputFactory...");
148+
var inputFactoryText = new InputFactoryClassTemplate(inputs, namespaceName).TransformText();
149+
AddFile("Inputs", "InputFactory.cs", inputFactoryText);
150+
}
151+
152+
131153

132154
Console.WriteLine("Generate Enums...");
133155
foreach (var enumType in schema.GetEnums())
@@ -155,7 +177,7 @@ public List<FileEntry> Generate(string schemaJson)
155177
}
156178

157179

158-
Console.WriteLine("Generate Client...");
180+
Console.WriteLine("Generate Client...");
159181
var templateText = new ClientTemplate(namespaceName, clientName, queryType, mutationType, subscriptionType)
160182
.TransformText();
161183
var fileName = clientName + ".cs";

src/Linq2GraphQL.Generator/Templates/Class/InputClassTemplate.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public virtual string TransformText()
6868
this.Write("\")]\r\n\tpublic ");
6969

7070
#line 19 "C:\Code\Github\Linq2GraphQL.Client\src\Linq2GraphQL.Generator\Templates\Class\InputClassTemplate.tt"
71-
this.Write(this.ToStringHelper.ToStringWithCulture(coreType.CSharpTypeDefinition));
71+
this.Write(this.ToStringHelper.ToStringWithCulture(GetPropertyDefinition(field)));
7272

7373
#line default
7474
#line hidden

src/Linq2GraphQL.Generator/Templates/Class/InputClassTemplate.tt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public partial class <#= classType.Name #> : GraphInputBase
1616
var coreType = field.CoreType;
1717
#>
1818
[JsonPropertyName("<#= field.Name #>")]
19-
public <#= coreType.CSharpTypeDefinition #> <#= field.CSharpName #>
19+
public <#= GetPropertyDefinition(field) #> <#= field.CSharpName #>
2020
{
2121
get => GetValue<<#= coreType.CSharpTypeDefinition #>>("<#= field.Name #>");
2222
set => SetValue("<#= field.Name #>", value);

src/Linq2GraphQL.Generator/Templates/Class/InputClassTemplate.tt.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,15 @@ public InputClassTemplate(GraphqlType classType, string namespaceName)
1212
}
1313

1414
public bool IsInput => classType.Kind == TypeKind.InputObject;
15+
16+
public string GetPropertyDefinition(Field field)
17+
{
18+
if (!GeneratorSettings.Current.Nullable || !field.CoreType.OuterNoneNull)
19+
{
20+
return field.CoreType.CSharpTypeDefinition;
21+
}
22+
23+
return "required " + field.CoreType.CSharpTypeDefinition;
24+
25+
}
1526
}

test/Linq2GraphQL.TestClientNullable/Generated/Client/MutationMethods.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
2+
//---------------------------------------------------------------------
3+
// This code was automatically generated by Linq2GraphQL
4+
// Please don't edit this file
5+
// Github:https://github.com/linq2graphql/linq2graphql.client
6+
// Url: https://linq2graphql.com
7+
// Generation Date: den 8 september 2024 17:02:30
8+
//---------------------------------------------------------------------
9+
110
using System.Collections.Generic;
211
using System;
312
using Linq2GraphQL.Client;

test/Linq2GraphQL.TestClientNullable/Generated/Client/QueryMethods.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
2+
//---------------------------------------------------------------------
3+
// This code was automatically generated by Linq2GraphQL
4+
// Please don't edit this file
5+
// Github:https://github.com/linq2graphql/linq2graphql.client
6+
// Url: https://linq2graphql.com
7+
// Generation Date: den 8 september 2024 17:02:30
8+
//---------------------------------------------------------------------
9+
110
using System.Collections.Generic;
211
using System;
312
using Linq2GraphQL.Client;

test/Linq2GraphQL.TestClientNullable/Generated/Client/SampleNullableClient.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
2+
//---------------------------------------------------------------------
3+
// This code was automatically generated by Linq2GraphQL
4+
// Please don't edit this file
5+
// Github:https://github.com/linq2graphql/linq2graphql.client
6+
// Url: https://linq2graphql.com
7+
// Generation Date: den 8 september 2024 17:02:30
8+
//---------------------------------------------------------------------
9+
110
using Linq2GraphQL.Client;
211
using Microsoft.Extensions.Caching.Memory;
312
using Microsoft.Extensions.DependencyInjection;

test/Linq2GraphQL.TestClientNullable/Generated/Client/SampleNullableClientExtensions.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
2+
//---------------------------------------------------------------------
3+
// This code was automatically generated by Linq2GraphQL
4+
// Please don't edit this file
5+
// Github:https://github.com/linq2graphql/linq2graphql.client
6+
// Url: https://linq2graphql.com
7+
// Generation Date: den 8 september 2024 17:02:30
8+
//---------------------------------------------------------------------
9+
110
using Linq2GraphQL.Client;
211
using Microsoft.Extensions.DependencyInjection;
312
using Microsoft.Extensions.Options;

test/Linq2GraphQL.TestClientNullable/Generated/Enums/AddressType.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
2+
//---------------------------------------------------------------------
3+
// This code was automatically generated by Linq2GraphQL
4+
// Please don't edit this file
5+
// Github:https://github.com/linq2graphql/linq2graphql.client
6+
// Url: https://linq2graphql.com
7+
// Generation Date: den 8 september 2024 17:02:30
8+
//---------------------------------------------------------------------
9+
110
using Linq2GraphQL.Client;
211
using System.Runtime.Serialization;
312
using System.Text.Json.Serialization;

test/Linq2GraphQL.TestClientNullable/Generated/Enums/CustomerStatus.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
2+
//---------------------------------------------------------------------
3+
// This code was automatically generated by Linq2GraphQL
4+
// Please don't edit this file
5+
// Github:https://github.com/linq2graphql/linq2graphql.client
6+
// Url: https://linq2graphql.com
7+
// Generation Date: den 8 september 2024 17:02:30
8+
//---------------------------------------------------------------------
9+
110
using Linq2GraphQL.Client;
211
using System.Runtime.Serialization;
312
using System.Text.Json.Serialization;

test/Linq2GraphQL.TestClientNullable/Generated/Inputs/AddressInput.cs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
2+
//---------------------------------------------------------------------
3+
// This code was automatically generated by Linq2GraphQL
4+
// Please don't edit this file
5+
// Github:https://github.com/linq2graphql/linq2graphql.client
6+
// Url: https://linq2graphql.com
7+
// Generation Date: den 8 september 2024 17:02:30
8+
//---------------------------------------------------------------------
9+
110
using System;
211
using System.Collections.Generic;
312
using System.Text.Json.Serialization;
@@ -9,21 +18,21 @@ namespace Linq2GraphQL.TestClientNullable;
918
public partial class AddressInput : GraphInputBase
1019
{
1120
[JsonPropertyName("name")]
12-
public string Name
21+
public required string Name
1322
{
1423
get => GetValue<string>("name");
1524
set => SetValue("name", value);
1625
}
1726

1827
[JsonPropertyName("street")]
19-
public string Street
28+
public required string Street
2029
{
2130
get => GetValue<string>("street");
2231
set => SetValue("street", value);
2332
}
2433

2534
[JsonPropertyName("postalCode")]
26-
public string PostalCode
35+
public required string PostalCode
2736
{
2837
get => GetValue<string>("postalCode");
2938
set => SetValue("postalCode", value);

test/Linq2GraphQL.TestClientNullable/Generated/Inputs/CustomerInput.cs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
2+
//---------------------------------------------------------------------
3+
// This code was automatically generated by Linq2GraphQL
4+
// Please don't edit this file
5+
// Github:https://github.com/linq2graphql/linq2graphql.client
6+
// Url: https://linq2graphql.com
7+
// Generation Date: den 8 september 2024 17:02:30
8+
//---------------------------------------------------------------------
9+
110
using System;
211
using System.Collections.Generic;
312
using System.Text.Json.Serialization;
@@ -9,28 +18,28 @@ namespace Linq2GraphQL.TestClientNullable;
918
public partial class CustomerInput : GraphInputBase
1019
{
1120
[JsonPropertyName("customerId")]
12-
public Guid CustomerId
21+
public required Guid CustomerId
1322
{
1423
get => GetValue<Guid>("customerId");
1524
set => SetValue("customerId", value);
1625
}
1726

1827
[JsonPropertyName("customerName")]
19-
public string CustomerName
28+
public required string CustomerName
2029
{
2130
get => GetValue<string>("customerName");
2231
set => SetValue("customerName", value);
2332
}
2433

2534
[JsonPropertyName("status")]
26-
public CustomerStatus Status
35+
public required CustomerStatus Status
2736
{
2837
get => GetValue<CustomerStatus>("status");
2938
set => SetValue("status", value);
3039
}
3140

3241
[JsonPropertyName("orders")]
33-
public List<OrderInput> Orders
42+
public required List<OrderInput> Orders
3443
{
3544
get => GetValue<List<OrderInput>>("orders");
3645
set => SetValue("orders", value);

0 commit comments

Comments
 (0)