Skip to content

Commit e64175d

Browse files
committed
List in List complete
1 parent 57a1603 commit e64175d

File tree

14 files changed

+206
-200
lines changed

14 files changed

+206
-200
lines changed

src/Linq2GraphQL.Generator/GraphQLSchema/RootSchema.cs

Lines changed: 133 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -135,15 +135,15 @@ public class BaseField
135135
public BaseType Type { get; set; }
136136

137137

138-
private TypeInfo fieldInfo;
139-
public TypeInfo FieldInfo
140-
{
141-
get
142-
{
143-
fieldInfo ??= GetFieldTypeInfo();
144-
return fieldInfo;
145-
}
146-
}
138+
//private TypeInfo fieldInfo;
139+
//public TypeInfo FieldInfo
140+
//{
141+
// get
142+
// {
143+
// fieldInfo ??= GetFieldTypeInfo();
144+
// return fieldInfo;
145+
// }
146+
//}
147147

148148
private CoreType coreType;
149149
public CoreType CoreType
@@ -157,54 +157,54 @@ public CoreType CoreType
157157

158158

159159

160-
private TypeInfo GetFieldTypeInfo()
161-
{
160+
//private TypeInfo GetFieldTypeInfo()
161+
//{
162162

163-
if (Type == null) return null;
163+
// if (Type == null) return null;
164164

165-
var allTypes = Type.GetAllTypes();
165+
// var allTypes = Type.GetAllTypes();
166166

167-
var baseFieldType = Type.GetBaseBaseType();
167+
// var baseFieldType = Type.GetBaseBaseType();
168168

169-
Type csharpType = null;
169+
// Type csharpType = null;
170170

171-
string csharpTypeName;
172-
if (Helpers.TypeMapping.TryGetValue(baseFieldType.Name, out var typeMapping))
173-
{
174-
csharpTypeName = typeMapping.Name;
175-
csharpType = typeMapping.type;
176-
}
177-
else
178-
{
179-
csharpTypeName = baseFieldType.Name.ToPascalCase();
180-
}
171+
// string csharpTypeName;
172+
// if (Helpers.TypeMapping.TryGetValue(baseFieldType.Name, out var typeMapping))
173+
// {
174+
// csharpTypeName = typeMapping.Name;
175+
// csharpType = typeMapping.type;
176+
// }
177+
// else
178+
// {
179+
// csharpTypeName = baseFieldType.Name.ToPascalCase();
180+
// }
181181

182182

183-
var coreType = Type.GetCoreType();
183+
// var coreType = Type.GetCoreType();
184184

185-
var isList = allTypes.Any(e => e.Kind == TypeKind.List);
186-
var isNoneNull = allTypes.Any(e => e.Kind == TypeKind.NonNull);
185+
// var isList = allTypes.Any(e => e.Kind == TypeKind.List);
186+
// var isNoneNull = allTypes.Any(e => e.Kind == TypeKind.NonNull);
187187

188-
var graphTypeDefinition = isNoneNull ? baseFieldType.Name + "!" : baseFieldType.Name;
189-
if (isList)
190-
{
191-
graphTypeDefinition = $"[{graphTypeDefinition}]";
192-
var gr = coreType.GetGraphQLTypeDefinition();
193-
var cs = coreType.GetCSharpTypeDefinition();
194-
}
188+
// var graphTypeDefinition = isNoneNull ? baseFieldType.Name + "!" : baseFieldType.Name;
189+
// if (isList)
190+
// {
191+
// graphTypeDefinition = $"[{graphTypeDefinition}]";
192+
// var gr = coreType.GetGraphQLTypeDefinition();
193+
// var cs = coreType.GetCSharpTypeDefinition();
194+
// }
195195

196-
return new TypeInfo
197-
{
196+
// return new TypeInfo
197+
// {
198198

199-
Kind = baseFieldType.Kind,
200-
IsList = isList,
201-
IsNoneNull = isNoneNull,
202-
CSharpType = csharpType,
203-
CSharpTypeName = csharpTypeName,
204-
GraphTypeDefinition = graphTypeDefinition,
205-
IsEnum = baseFieldType.Kind == TypeKind.Enum
206-
};
207-
}
199+
// Kind = baseFieldType.Kind,
200+
// IsList = isList,
201+
// IsNoneNull = isNoneNull,
202+
// CSharpType = csharpType,
203+
// CSharpTypeName = csharpTypeName,
204+
// GraphTypeDefinition = graphTypeDefinition,
205+
// IsEnum = baseFieldType.Kind == TypeKind.Enum
206+
// };
207+
//}
208208

209209
}
210210

@@ -218,8 +218,8 @@ public class Field : BaseField
218218
public bool SupportCursorPaging()
219219
{
220220
if (!GraphqlType.ContainPageInfo()) { return false; }
221-
if (Args?.FirstOrDefault(e => e.Name == "after" && e.FieldInfo.CSharpTypeName == "string") == null) { return false; }
222-
if (Args?.FirstOrDefault(e => e.Name == "before" && e.FieldInfo.CSharpTypeName == "string") == null) { return false; }
221+
if (Args?.FirstOrDefault(e => e.Name == "after" && e.CoreType.CSharpTypeName == "string") == null) { return false; }
222+
if (Args?.FirstOrDefault(e => e.Name == "before" && e.CoreType.CSharpTypeName == "string") == null) { return false; }
223223
return true;
224224
}
225225

@@ -238,9 +238,9 @@ public string GetArgString(bool addTypeAttribute)
238238
return result;
239239
}
240240

241-
foreach (var arg in Args.OrderByDescending(x => x.FieldInfo.IsNoneNull))
241+
foreach (var arg in Args.OrderBy(x => x.CoreType.OuterNoneNull))
242242
{
243-
var baseType = arg.FieldInfo;
243+
var coreType = arg.CoreType;
244244
if (result != "")
245245
{
246246
result += ", ";
@@ -249,14 +249,14 @@ public string GetArgString(bool addTypeAttribute)
249249
if (addTypeAttribute)
250250
{
251251
result +=
252-
$"[GraphArgument(\"{baseType.GraphTypeDefinition}\")] {baseType.CSharpTypeNameFull} {arg.Name.ToCamelCase()}";
252+
$"[GraphArgument(\"{arg.CoreType.GraphQLTypeDefinition}\")] {arg.CoreType.CSharpTypeDefinition} {arg.Name.ToCamelCase()}";
253253
}
254254
else
255255
{
256-
result += $"{baseType.CSharpTypeNameFull} {arg.Name.ToCamelCase()}";
256+
result += $"{arg.CoreType.CSharpTypeDefinition} {arg.Name.ToCamelCase()}";
257257
}
258258

259-
if (!baseType.IsNoneNull || baseType.IsList)
259+
if (!arg.CoreType.OuterNoneNull)
260260
{
261261
result += " = null";
262262
}
@@ -266,63 +266,63 @@ public string GetArgString(bool addTypeAttribute)
266266
}
267267
}
268268

269-
public class TypeInfo
270-
{
271-
public TypeKind Kind { get; set; }
269+
//public class TypeInfo
270+
//{
271+
// public TypeKind Kind { get; set; }
272272

273-
public string GraphTypeDefinition { get; set; }
273+
// public string GraphTypeDefinition { get; set; }
274274

275-
public string CSharpTypeName { get; set; }
276-
public Type CSharpType { get; set; }
277-
public bool IsNoneNull { get; set; }
278-
public bool IsList { get; set; }
275+
// public string CSharpTypeName { get; set; }
276+
// public Type CSharpType { get; set; }
277+
// public bool IsNoneNull { get; set; }
278+
// public bool IsList { get; set; }
279279

280280

281-
private bool CSharpNullQuestion()
282-
{
283-
if (GeneratorSettings.Current.Nullable)
284-
{
285-
return !IsNoneNull;
286-
}
287-
else
288-
{
289-
return !IsNoneNull && (Kind == TypeKind.Enum || (CSharpType != null && CSharpTypeName != "string"));
290-
}
281+
// private bool CSharpNullQuestion()
282+
// {
283+
// if (GeneratorSettings.Current.Nullable)
284+
// {
285+
// return !IsNoneNull;
286+
// }
287+
// else
288+
// {
289+
// return !IsNoneNull && (Kind == TypeKind.Enum || (CSharpType != null && CSharpTypeName != "string"));
290+
// }
291291

292-
}
292+
// }
293293

294-
public string CSharpTypeNameFullNeverNull
295-
{
296-
get
297-
{
298-
var result = CSharpTypeName;
294+
// public string CSharpTypeNameFullNeverNull
295+
// {
296+
// get
297+
// {
298+
// var result = CSharpTypeName;
299299

300-
if (IsList)
301-
{
302-
return $"List<{result}>";
303-
}
300+
// if (IsList)
301+
// {
302+
// return $"List<{result}>";
303+
// }
304304

305-
return result;
306-
}
307-
}
305+
// return result;
306+
// }
307+
// }
308308

309-
public string CSharpTypeNameFull
310-
{
311-
get
312-
{
313-
var result = CSharpTypeName + (CSharpNullQuestion() ? "?" : "");
309+
// public string CSharpTypeNameFull
310+
// {
311+
// get
312+
// {
313+
// var result = CSharpTypeName + (CSharpNullQuestion() ? "?" : "");
314314

315-
if (IsList)
316-
{
317-
return $"List<{result}>";
318-
}
315+
// if (IsList)
316+
// {
317+
// return $"List<{result}>";
318+
// }
319319

320-
return result;
321-
}
322-
}
320+
// return result;
321+
// }
322+
// }
323323

324-
public bool IsEnum { get; set; }
325-
}
324+
// public bool IsEnum { get; set; }
325+
//}
326326

327327

328328
public class Arg : BaseField
@@ -365,14 +365,13 @@ public BaseType GetBaseBaseType()
365365

366366

367367
public CoreType GetCoreType()
368-
{
368+
{
369369
var result = new CoreType();
370-
370+
371371
bool currentNoneNull = false;
372372

373373
foreach (var type in GetAllTypes())
374374
{
375-
376375
switch (type.Kind)
377376
{
378377
case TypeKind.NonNull:
@@ -394,18 +393,21 @@ public CoreType GetCoreType()
394393

395394
result.Lists.Reverse();
396395

397-
if (Helpers.TypeMapping.TryGetValue(result.BaseType.Name, out var typeMapping))
396+
if (Helpers.TypeMapping.TryGetValue(result.BaseType.Name, out var typeMapping))
398397
{
399398
result.CSharpTypeName = typeMapping.Name;
400399
result.CSharpType = typeMapping.type;
401400
}
402401
else
403402
{
404-
result.CSharpTypeName = result.BaseType.Name.ToPascalCase();
403+
result.CSharpTypeName = result.BaseType.Name.ToPascalCase();
405404
}
406405

407406
result.OuterNoneNull = result.Lists.FirstOrDefault()?.NoneNull ?? result.NoneNull;
408407

408+
result.SetCSharpTypeDefinition();
409+
result.SetGraphQLTypeDefinition();
410+
409411
return result;
410412

411413
}
@@ -429,9 +431,23 @@ public class CoreType
429431
public List<CoreTypeList> Lists { get; set; } = [];
430432
public bool IsInList => Lists.Count != 0;
431433

434+
public bool IsEnum => BaseType.Kind == TypeKind.Enum;
432435

436+
public string CSharpTypeDefinition { get; set; }
437+
public string CSharpTypeDefinitionNeverNull
438+
{
439+
get
440+
{
441+
if (!string.IsNullOrWhiteSpace(CSharpTypeDefinition) && CSharpTypeDefinition.EndsWith('?'))
442+
{
443+
return CSharpTypeDefinition.RemoveFromEnd("?");
444+
}
445+
return CSharpTypeDefinition;
446+
}
447+
}
448+
public string GraphQLTypeDefinition { get; set; }
433449

434-
public string GetGraphQLTypeDefinition()
450+
public void SetGraphQLTypeDefinition()
435451
{
436452
var result = BaseType.Name;
437453

@@ -443,37 +459,43 @@ public string GetGraphQLTypeDefinition()
443459
if (list.NoneNull) { result += "!"; }
444460
}
445461

446-
return result;
462+
GraphQLTypeDefinition = result;
447463

448464
}
449465

450466
private bool UseSharpNoneNull()
451467
{
452-
if (GeneratorSettings.Current.Nullable)
468+
469+
if (GeneratorSettings.Current.Nullable || NoneNull)
453470
{
454471
return NoneNull;
455472
}
456-
else
473+
474+
//If Nullable
475+
if (BaseType.Kind == TypeKind.Enum || (CSharpType != null && CSharpTypeName != "string"))
457476
{
458-
return NoneNull && !(BaseType.Kind == TypeKind.Enum || (CSharpType != null && CSharpTypeName != "string"));
477+
return NoneNull;
478+
479+
// return NoneNull && !(BaseType.Kind == TypeKind.Enum || (CSharpType != null && CSharpTypeName != "string"));
459480
}
460481

482+
return true;
461483
}
462484

463485

464-
public string GetCSharpTypeDefinition()
486+
public void SetCSharpTypeDefinition()
465487
{
466488
var result = CSharpTypeName;
467489

468-
if (UseSharpNoneNull()) { result += "?"; }
490+
if (!UseSharpNoneNull()) { result += "?"; }
469491

470492
foreach (var list in Lists)
471493
{
472494
result = $"List<{result}>";
473495
if (!list.NoneNull && GeneratorSettings.Current.Nullable) { result += "?"; }
474496
}
475497

476-
return result;
498+
CSharpTypeDefinition = result;
477499

478500
}
479501

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public string GetFieldCSharpName(Field field)
2222
{
2323
return "Linq2GraphQL.Client.Common.PageInfo";
2424
}
25-
return field.FieldInfo.CSharpTypeNameFull;
25+
return field.CoreType.CSharpTypeDefinition;
2626
}
2727

2828

0 commit comments

Comments
 (0)