Skip to content

Commit f91cb2b

Browse files
authored
Merge pull request #26 from rjrudman/master
Generate types and fields in a stable order
2 parents 77e328f + 91f457c commit f91cb2b

File tree

4 files changed

+25
-11
lines changed

4 files changed

+25
-11
lines changed

src/dotnet-gqlgen/Generator.cs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,18 @@ public class GeneratorOptions
2323
public bool NoGeneratedTimestamp { get; set; }
2424
public bool ConvertToUnixLineEnding { get; set; } = true;
2525
}
26+
27+
public class ModelType
28+
{
29+
public string Namespace { get; set; }
30+
public string SchemaFile { get; set; }
31+
public Dictionary<string, TypeInfo> Types { get; set; }
32+
public Dictionary<string, List<EnumInfo>> Enums { get; set; }
33+
public TypeInfo Mutation { get; set; }
34+
public string CmdArgs { get; set; }
35+
public string Usings { get; set; }
36+
public bool NoGeneratedTimestamp { get; set; }
37+
}
2638

2739
public static class Generator
2840
{
@@ -101,7 +113,7 @@ public static async Task Generate(GeneratorOptions options)
101113

102114
var allTypes = typeInfo.Types.Concat(typeInfo.Inputs).ToDictionary(k => k.Key, v => v.Value);
103115

104-
string resultTypes = await engine.CompileRenderAsync("resultTypes.cshtml", new
116+
string resultTypes = await engine.CompileRenderAsync("resultTypes.cshtml", new ModelType
105117
{
106118
Namespace = options.Namespace,
107119
SchemaFile = options.Source,
@@ -110,20 +122,20 @@ public static async Task Generate(GeneratorOptions options)
110122
Mutation = typeInfo.Mutation,
111123
CmdArgs = $"-n {options.Namespace} -c {options.ClientClassName} -m {options.ScalarMapping} -u {options.Usings.Replace("\n", "\\n")}",
112124
Usings = options.Usings,
113-
options.NoGeneratedTimestamp
125+
NoGeneratedTimestamp = options.NoGeneratedTimestamp
114126
});
115127
Directory.CreateDirectory(options.OutputDir);
116128
await NormalizeAndWriteIfChanged($"{options.OutputDir}/GeneratedResultTypes.cs", resultTypes, options.ConvertToUnixLineEnding);
117129

118-
string queryTypes = await engine.CompileRenderAsync("queryTypes.cshtml", new
130+
string queryTypes = await engine.CompileRenderAsync("queryTypes.cshtml", new ModelType
119131
{
120132
Namespace = options.Namespace,
121133
SchemaFile = options.Source,
122134
Types = allTypes,
123135
Mutation = typeInfo.Mutation,
124136
CmdArgs = $"-n {options.Namespace} -c {options.ClientClassName} -m {options.ScalarMapping} -u {options.Usings.Replace("\n", "\\n")}",
125137
Usings = options.Usings,
126-
options.NoGeneratedTimestamp
138+
NoGeneratedTimestamp = options.NoGeneratedTimestamp
127139
});
128140
Directory.CreateDirectory(options.OutputDir);
129141
await NormalizeAndWriteIfChanged($"{options.OutputDir}/GeneratedQueryTypes.cs", queryTypes, options.ConvertToUnixLineEnding);

src/dotnet-gqlgen/dotnet-gqlgen.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<TargetFramework>netstandard2.1</TargetFramework>
55
<RootNamespace>dotnet_gqlgen</RootNamespace>
66
<PreserveCompilationContext>true</PreserveCompilationContext>
7-
<PackageVersion>0.6.5</PackageVersion>
7+
<PackageVersion>0.7.5</PackageVersion>
88
</PropertyGroup>
99

1010
<PropertyGroup>

src/dotnet-gqlgen/queryTypes.cshtml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
@{
22
DisableEncoding = true;
33
}
4+
@model ModelType
45
@using System.IO
56
@using dotnet_gqlgen;
67

@@ -22,7 +23,7 @@ using DotNetGqlClient;
2223
namespace @Model.Namespace
2324
{
2425

25-
@foreach(var gqlType in Model.Types.Values)
26+
@foreach(var gqlType in Model.Types.Values.OrderBy(t => t.QueryName, StringComparer.Ordinal))
2627
{
2728
if (!string.IsNullOrEmpty(gqlType.Description))
2829
{
@@ -33,7 +34,7 @@ namespace @Model.Namespace
3334

3435
@:public abstract class @gqlType.QueryName : @gqlType.Name
3536
@:{
36-
@foreach(var field in gqlType.Fields)
37+
@foreach(var field in gqlType.Fields.OrderBy(f => f.Name, StringComparer.Ordinal))
3738
{
3839
@if (!field.ShouldBeProperty && !gqlType.IsInput)
3940
{

src/dotnet-gqlgen/resultTypes.cshtml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
@{
22
DisableEncoding = true;
33
}
4+
@model ModelType
45
@using System.IO
56
@using dotnet_gqlgen;
67

@@ -22,18 +23,18 @@ using DotNetGqlClient;
2223
namespace @Model.Namespace
2324
{
2425

25-
@foreach(var kvp in Model.Enums)
26+
@foreach(var kvp in Model.Enums.OrderBy(e => e.Key, StringComparer.Ordinal))
2627
{
2728
@:public enum @kvp.Key {
28-
@foreach(var field in kvp.Value)
29+
@foreach(var field in kvp.Value.OrderBy(t => t.Name, StringComparer.Ordinal))
2930
{
3031
@:[GqlFieldName("@field.Name")]
3132
@:@field.DotNetName,
3233
}
3334
@:}
3435
}
3536

36-
@foreach(var gqlType in Model.Types.Values)
37+
@foreach(var gqlType in Model.Types.Values.OrderBy(t => t.Name, StringComparer.Ordinal))
3738
{
3839
if (!string.IsNullOrEmpty(gqlType.Description))
3940
{
@@ -44,7 +45,7 @@ namespace @Model.Namespace
4445
@* We make interfaces as classes for now *@
4546
@:public class @gqlType.Name
4647
@:{
47-
@foreach(var field in gqlType.Fields)
48+
@foreach(var field in gqlType.Fields.OrderBy(f => f.Name, StringComparer.Ordinal))
4849
{
4950
@if (field.ShouldBeProperty || gqlType.IsInput)
5051
{

0 commit comments

Comments
 (0)