Skip to content

Commit 5643494

Browse files
authored
Merge pull request #36 from Linq2GraphQL/support-list-in-list
Support list in list
2 parents 6d637f2 + 16bfa62 commit 5643494

File tree

16 files changed

+280
-187
lines changed

16 files changed

+280
-187
lines changed

src/Linq2GraphQL.Generator/GraphQLSchema/RootSchema.cs

Lines changed: 193 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public string GetInterfacesString(string baseClass = null)
6262
interfaces += "Linq2GraphQL.Client.Common.ICursorPaging";
6363
}
6464

65-
65+
6666

6767
if (!string.IsNullOrWhiteSpace(interfaces))
6868
{
@@ -135,60 +135,76 @@ public class BaseField
135135
public BaseType Type { get; set; }
136136

137137

138-
private TypeInfo fieldInfo;
139-
public TypeInfo FieldInfo
138+
//private TypeInfo fieldInfo;
139+
//public TypeInfo FieldInfo
140+
//{
141+
// get
142+
// {
143+
// fieldInfo ??= GetFieldTypeInfo();
144+
// return fieldInfo;
145+
// }
146+
//}
147+
148+
private CoreType coreType;
149+
public CoreType CoreType
140150
{
141151
get
142152
{
143-
fieldInfo ??= GetFieldTypeInfo();
144-
return fieldInfo;
153+
coreType ??= Type.GetCoreType();
154+
return coreType;
145155
}
146156
}
147157

148-
private TypeInfo GetFieldTypeInfo()
149-
{
150158

151-
if (Type == null) return null;
152159

153-
var allTypes = Type.GetAllTypes();
160+
//private TypeInfo GetFieldTypeInfo()
161+
//{
154162

155-
var baseFieldType = Type.GetBaseBaseType();
163+
// if (Type == null) return null;
156164

157-
Type csharpType = null;
165+
// var allTypes = Type.GetAllTypes();
158166

159-
string csharpTypeName;
160-
if (Helpers.TypeMapping.TryGetValue(baseFieldType.Name, out var typeMapping))
161-
{
162-
csharpTypeName = typeMapping.Name;
163-
csharpType = typeMapping.type;
164-
}
165-
else
166-
{
167-
csharpTypeName = baseFieldType.Name.ToPascalCase();
168-
}
167+
// var baseFieldType = Type.GetBaseBaseType();
169168

169+
// Type csharpType = null;
170170

171-
var isList = allTypes.Any(e => e.Kind == TypeKind.List);
172-
var isNoneNull = allTypes.Any(e => e.Kind == TypeKind.NonNull);
173-
174-
var graphTypeDefinition = isNoneNull ? baseFieldType.Name + "!" : baseFieldType.Name;
175-
if (isList)
176-
{
177-
graphTypeDefinition = $"[{graphTypeDefinition}]";
178-
}
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+
// }
179181

180-
return new TypeInfo
181-
{
182-
183-
Kind = baseFieldType.Kind,
184-
IsList = isList,
185-
IsNoneNull = isNoneNull,
186-
CSharpType = csharpType,
187-
CSharpTypeName = csharpTypeName,
188-
GraphTypeDefinition = graphTypeDefinition,
189-
IsEnum = baseFieldType.Kind == TypeKind.Enum
190-
};
191-
}
182+
183+
// var coreType = Type.GetCoreType();
184+
185+
// var isList = allTypes.Any(e => e.Kind == TypeKind.List);
186+
// var isNoneNull = allTypes.Any(e => e.Kind == TypeKind.NonNull);
187+
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+
// }
195+
196+
// return new TypeInfo
197+
// {
198+
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+
//}
192208

193209
}
194210

@@ -202,8 +218,8 @@ public class Field : BaseField
202218
public bool SupportCursorPaging()
203219
{
204220
if (!GraphqlType.ContainPageInfo()) { return false; }
205-
if (Args?.FirstOrDefault(e => e.Name == "after" && e.FieldInfo.CSharpTypeName == "string") == null) { return false; }
206-
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; }
207223
return true;
208224
}
209225

@@ -222,9 +238,9 @@ public string GetArgString(bool addTypeAttribute)
222238
return result;
223239
}
224240

225-
foreach (var arg in Args.OrderByDescending(x => x.FieldInfo.IsNoneNull))
241+
foreach (var arg in Args.OrderByDescending(x => x.CoreType.OuterNoneNull))
226242
{
227-
var baseType = arg.FieldInfo;
243+
var coreType = arg.CoreType;
228244
if (result != "")
229245
{
230246
result += ", ";
@@ -233,14 +249,14 @@ public string GetArgString(bool addTypeAttribute)
233249
if (addTypeAttribute)
234250
{
235251
result +=
236-
$"[GraphArgument(\"{baseType.GraphTypeDefinition}\")] {baseType.CSharpTypeNameFull} {arg.Name.ToCamelCase()}";
252+
$"[GraphArgument(\"{arg.CoreType.GraphQLTypeDefinition}\")] {arg.CoreType.CSharpTypeDefinition} {arg.Name.ToCamelCase()}";
237253
}
238254
else
239255
{
240-
result += $"{baseType.CSharpTypeNameFull} {arg.Name.ToCamelCase()}";
256+
result += $"{arg.CoreType.CSharpTypeDefinition} {arg.Name.ToCamelCase()}";
241257
}
242258

243-
if (!baseType.IsNoneNull || baseType.IsList)
259+
if (!arg.CoreType.OuterNoneNull)
244260
{
245261
result += " = null";
246262
}
@@ -250,101 +266,179 @@ public string GetArgString(bool addTypeAttribute)
250266
}
251267
}
252268

253-
public class TypeInfo
269+
270+
public class Arg : BaseField
254271
{
272+
public string DefaultValue { get; set; }
273+
}
274+
275+
276+
277+
public class BaseType
278+
{
279+
public string Name { get; set; }
255280
public TypeKind Kind { get; set; }
281+
public string Description { get; set; }
256282

257-
public string GraphTypeDefinition { get; set; }
283+
public string CSharpName => Name?.ToPascalCase();
284+
public string FileName => CSharpName + ".cs";
285+
286+
public BaseType OfType { get; set; }
258287

259-
public string CSharpTypeName { get; set; }
260-
public Type CSharpType { get; set; }
261-
public bool IsNoneNull { get; set; }
262-
public bool IsList { get; set; }
263288

264289

265-
private bool CSharpNullQuestion()
290+
public List<BaseType> GetAllTypes()
266291
{
267-
if (GeneratorSettings.Current.Nullable)
268-
{
269-
return !IsNoneNull;
270-
}
271-
else
292+
var result = new List<BaseType> { this };
293+
294+
if (OfType != null)
272295
{
273-
return !IsNoneNull && (Kind == TypeKind.Enum || (CSharpType != null && CSharpTypeName != "string"));
296+
result.AddRange(OfType.GetAllTypes());
274297
}
275298

299+
return result;
276300
}
277301

278-
public string CSharpTypeNameFullNeverNull
302+
public BaseType GetBaseBaseType()
279303
{
280-
get
281-
{
282-
var result = CSharpTypeName;
283-
284-
if (IsList)
285-
{
286-
return $"List<{result}>";
287-
}
288-
289-
return result;
290-
}
304+
if (OfType == null) return this;
305+
return OfType.GetBaseBaseType();
291306
}
292307

293-
public string CSharpTypeNameFull
308+
309+
public CoreType GetCoreType()
294310
{
295-
get
296-
{
297-
var result = CSharpTypeName + (CSharpNullQuestion() ? "?" : "");
311+
var result = new CoreType();
312+
313+
bool currentNoneNull = false;
298314

299-
if (IsList)
315+
foreach (var type in GetAllTypes())
316+
{
317+
switch (type.Kind)
300318
{
301-
return $"List<{result}>";
319+
case TypeKind.NonNull:
320+
currentNoneNull = true;
321+
break;
322+
case TypeKind.List:
323+
result.Lists.Add(new CoreTypeList { NoneNull = currentNoneNull });
324+
currentNoneNull = false;
325+
break;
326+
default:
327+
328+
result.NoneNull = currentNoneNull;
329+
result.BaseType = type;
330+
currentNoneNull = false;
331+
break;
332+
302333
}
334+
}
303335

304-
return result;
336+
result.Lists.Reverse();
337+
338+
if (Helpers.TypeMapping.TryGetValue(result.BaseType.Name, out var typeMapping))
339+
{
340+
result.CSharpTypeName = typeMapping.Name;
341+
result.CSharpType = typeMapping.type;
342+
}
343+
else
344+
{
345+
result.CSharpTypeName = result.BaseType.Name.ToPascalCase();
305346
}
347+
348+
result.OuterNoneNull = result.Lists.FirstOrDefault()?.NoneNull ?? result.NoneNull;
349+
350+
result.SetCSharpTypeDefinition();
351+
result.SetGraphQLTypeDefinition();
352+
353+
return result;
354+
306355
}
307356

308-
public bool IsEnum { get; set; }
357+
309358
}
310359

311360

312-
public class Arg : BaseField
361+
public class CoreTypeList
313362
{
314-
public string DefaultValue { get; set; }
363+
public bool NoneNull { get; set; }
315364
}
316365

366+
public class CoreType
367+
{
368+
public BaseType BaseType { get; set; }
369+
public bool NoneNull { get; set; }
370+
public bool OuterNoneNull { get; set; }
371+
public string CSharpTypeName { get; set; }
372+
public Type CSharpType { get; set; }
373+
public List<CoreTypeList> Lists { get; set; } = [];
374+
public bool IsInList => Lists.Count != 0;
317375

376+
public bool IsEnum => BaseType.Kind == TypeKind.Enum;
318377

319-
public class BaseType
320-
{
321-
public string Name { get; set; }
322-
public TypeKind Kind { get; set; }
323-
public string Description { get; set; }
378+
public string CSharpTypeDefinition { get; set; }
379+
public string CSharpTypeDefinitionNeverNull
380+
{
381+
get
382+
{
383+
if (!string.IsNullOrWhiteSpace(CSharpTypeDefinition) && CSharpTypeDefinition.EndsWith('?'))
384+
{
385+
return CSharpTypeDefinition.RemoveFromEnd("?");
386+
}
387+
return CSharpTypeDefinition;
388+
}
389+
}
390+
public string GraphQLTypeDefinition { get; set; }
324391

325-
public string CSharpName => Name?.ToPascalCase();
326-
public string FileName => CSharpName + ".cs";
392+
public void SetGraphQLTypeDefinition()
393+
{
394+
var result = BaseType.Name;
327395

328-
public BaseType OfType { get; set; }
396+
if (NoneNull) { result += "!"; }
329397

398+
foreach (var list in Lists)
399+
{
400+
result = $"[{result}]";
401+
if (list.NoneNull) { result += "!"; }
402+
}
330403

404+
GraphQLTypeDefinition = result;
331405

332-
public List<BaseType> GetAllTypes()
406+
}
407+
408+
private bool UseSharpNoneNull()
333409
{
334-
var result = new List<BaseType> { this };
410+
411+
if (GeneratorSettings.Current.Nullable || NoneNull)
412+
{
413+
return NoneNull;
414+
}
335415

336-
if (OfType != null)
416+
//If Nullable
417+
if (BaseType.Kind == TypeKind.Enum || (CSharpType != null && CSharpTypeName != "string"))
337418
{
338-
result.AddRange(OfType.GetAllTypes());
419+
return NoneNull;
420+
421+
// return NoneNull && !(BaseType.Kind == TypeKind.Enum || (CSharpType != null && CSharpTypeName != "string"));
339422
}
340423

341-
return result;
424+
return true;
342425
}
343426

344-
public BaseType GetBaseBaseType()
427+
428+
public void SetCSharpTypeDefinition()
345429
{
346-
if (OfType == null) return this;
347-
return OfType.GetBaseBaseType();
430+
var result = CSharpTypeName;
431+
432+
if (!UseSharpNoneNull()) { result += "?"; }
433+
434+
foreach (var list in Lists)
435+
{
436+
result = $"List<{result}>";
437+
if (!list.NoneNull && GeneratorSettings.Current.Nullable) { result += "?"; }
438+
}
439+
440+
CSharpTypeDefinition = result;
441+
348442
}
349443

350444
}

0 commit comments

Comments
 (0)