Skip to content

Commit 4b0a8cf

Browse files
committed
Added Required
1 parent 3b5e9c5 commit 4b0a8cf

16 files changed

Lines changed: 119 additions & 47 deletions

File tree

src/Linq2GraphQL.Generator/GraphQLSchema/RootSchema.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -170,8 +170,7 @@ private TypeInfo GetFieldTypeInfo()
170170

171171
var isList = allTypes.Any(e => e.Kind == TypeKind.List);
172172
var isNoneNull = allTypes.Any(e => e.Kind == TypeKind.NonNull);
173-
var csharpNullable = !isNoneNull && csharpType != null && csharpTypeName != "string";
174-
173+
175174
var graphTypeDefinition = isNoneNull ? baseFieldType.Name + "!" : baseFieldType.Name;
176175
if (isList)
177176
{
@@ -180,13 +179,14 @@ private TypeInfo GetFieldTypeInfo()
180179

181180
return new TypeInfo
182181
{
182+
183183
Kind = baseFieldType.Kind,
184184
IsList = isList,
185185
IsNoneNull = isNoneNull,
186186
CSharpType = csharpType,
187187
CSharpTypeName = csharpTypeName,
188188
GraphTypeDefinition = graphTypeDefinition,
189-
IsEnum = baseFieldType.Kind == TypeKind.Enum
189+
IsEnum = baseFieldType.Kind == TypeKind.Enum
190190
};
191191
}
192192

@@ -261,7 +261,7 @@ public class TypeInfo
261261
public bool IsNoneNull { get; set; }
262262
public bool IsList { get; set; }
263263

264-
264+
265265
private bool CSharpNullQuestion()
266266
{
267267
if (GeneratorSettings.Current.Nullable)

src/Linq2GraphQL.Generator/Properties/launchSettings.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"profiles": {
33
"Linq2GraphQL.Generator": {
44
"commandName": "Project",
5-
"commandLineArgs": "https://localhost:50741/graphql/ -c=\"SampleNullableClient\" -n=\"Linq2GraphQL.TestClientNullable\" -o=\"C:\\temp\\Linq2GraphQL\\Linq2GraphQL.TestClientNullable\\Generated\" -s=true -nu=true"
5+
"commandLineArgs": "https://localhost:50741/graphql/ -c=\"SampleNullableClient\" -n=\"Linq2GraphQL.TestClientNullable\" -o=\"C:\\Code\\Github\\Linq2GraphQL.Client\\test\\Linq2GraphQL.TestClientNullable\\Generated\" -s=true -nu=true"
66
}
77
}
88
}

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

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,33 @@ public ClassTemplate(GraphqlType classType, string namespaceName)
1414

1515
public bool IsInput => classType.Kind == TypeKind.InputObject;
1616

17-
17+
1818
public string GetFieldCSharpName(Field field)
1919
{
2020

2121
if (field.GraphqlType.IsPageInfo())
2222
{
2323
return "Linq2GraphQL.Client.Common.PageInfo";
2424
}
25-
return field.FieldInfo.CSharpTypeNameFull;
2625

26+
var result = "";
27+
if (GeneratorSettings.Current.Nullable && field.FieldInfo.IsNoneNull)
28+
{
29+
result += "required ";
30+
}
31+
32+
result += field.FieldInfo.CSharpTypeNameFull;
33+
34+
return result;
2735

36+
37+
}
38+
39+
40+
public bool NullableEnabled()
41+
{
42+
return GeneratorSettings.Current.Nullable;
2843
}
2944

30-
3145

3246
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using System.Collections.Generic;
2+
using System;
3+
using Linq2GraphQL.Client;
4+
5+
namespace Linq2GraphQL.TestClientNullable;
6+
7+
public class MutationMethods
8+
{
9+
private readonly GraphClient client;
10+
11+
public MutationMethods(GraphClient client)
12+
{
13+
this.client = client;
14+
}
15+
16+
public GraphQuery<Customer?> UpdateCustomer(Guid customerId, string name)
17+
{
18+
var arguments = new List<ArgumentValue>
19+
{
20+
new("customerId","UUID!", customerId),
21+
new("name","String!", name),
22+
};
23+
24+
return new GraphQuery<Customer?>(client, "updateCustomer", OperationType.Mutation, arguments);
25+
}
26+
27+
}

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,22 @@ public GraphQuery<List<Customer>> CustomerList()
3131
return new GraphQuery<Customer?>(client, "customerNullable", OperationType.Query, arguments);
3232
}
3333

34+
public GraphQuery<List<Customer?>> CustomerListAllNullable()
35+
{
36+
var arguments = new List<ArgumentValue>
37+
{
38+
};
39+
40+
return new GraphQuery<List<Customer?>>(client, "customerListAllNullable", OperationType.Query, arguments);
41+
}
42+
43+
public GraphQuery<List<Customer>> CustomerListNullable()
44+
{
45+
var arguments = new List<ArgumentValue>
46+
{
47+
};
48+
49+
return new GraphQuery<List<Customer>>(client, "customerListNullable", OperationType.Query, arguments);
50+
}
51+
3452
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@ public SampleNullableClient(HttpClient httpClient, IOptions<GraphClientOptions>
1111
{
1212
var client = new GraphClient(httpClient, options, provider);
1313
Query = new QueryMethods(client);
14+
Mutation = new MutationMethods(client);
1415
}
1516

1617
public QueryMethods Query { get; private set; }
18+
public MutationMethods Mutation { get; private set; }
1719

1820
}

test/Linq2GraphQL.TestClientNullable/Generated/Input/InputFactory.cs

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

test/Linq2GraphQL.TestClientNullable/Generated/Types/Address.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ namespace Linq2GraphQL.TestClientNullable;
99
public partial class Address : GraphQLTypeBase
1010
{
1111
[JsonPropertyName("name")]
12-
public string Name { get; set; }
12+
public required string Name { get; set; }
1313

1414
[JsonPropertyName("street")]
15-
public string Street { get; set; }
15+
public required string Street { get; set; }
1616

1717
[JsonPropertyName("postalCode")]
18-
public string PostalCode { get; set; }
18+
public required string PostalCode { get; set; }
1919

2020
}

test/Linq2GraphQL.TestClientNullable/Generated/Types/Customer.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,16 @@ namespace Linq2GraphQL.TestClientNullable;
99
public partial class Customer : GraphQLTypeBase
1010
{
1111
[JsonPropertyName("customerId")]
12-
public Guid CustomerId { get; set; }
12+
public required Guid CustomerId { get; set; }
1313

1414
[JsonPropertyName("customerName")]
15-
public string CustomerName { get; set; }
15+
public required string CustomerName { get; set; }
1616

1717
[JsonPropertyName("status")]
18-
public CustomerStatus Status { get; set; }
18+
public required CustomerStatus Status { get; set; }
1919

2020
[JsonPropertyName("orders")]
21-
public List<Order> Orders { get; set; }
21+
public required List<Order> Orders { get; set; }
2222

2323
[JsonPropertyName("address")]
2424
public Address? Address { get; set; }

test/Linq2GraphQL.TestClientNullable/Generated/Types/Item.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ namespace Linq2GraphQL.TestClientNullable;
99
public partial class Item : GraphQLTypeBase
1010
{
1111
[JsonPropertyName("itemId")]
12-
public string ItemId { get; set; }
12+
public required string ItemId { get; set; }
1313

1414
[JsonPropertyName("itemName")]
15-
public string ItemName { get; set; }
15+
public required string ItemName { get; set; }
1616

1717
}

0 commit comments

Comments
 (0)