Skip to content

Commit 326b09f

Browse files
committed
use a stringified wrapper class for handling enums to prevent new values breaking old generated code
1 parent 0a1dff0 commit 326b09f

File tree

16 files changed

+285
-26
lines changed

16 files changed

+285
-26
lines changed

Samples/ClientSample/Client/GitHub/currentUser.gql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@ query {
22
viewer {
33
login,
44
bio,
5+
56
}
67
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"class": "Sample.Client.GitHub.GitHubClient",
33
"schema": "schema.json",
4+
45
"root":true
56
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
2+
query q{
3+
test(id: "safsa")
4+
{
5+
nullable,
6+
nonnullable
7+
}
8+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"class": "Sample.Client.TestClient",
3+
"schema": "schema.gql",
4+
5+
"root": true,
6+
"templateSettings": {
7+
}
8+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
2+
schema {
3+
query: Query
4+
mutation: Mutation
5+
}
6+
7+
type Query {
8+
test(id: string!): Droid
9+
}
10+
11+
type Droid {
12+
nullable: Episode
13+
nonnullable: Episode!
14+
}
15+
16+
enum Episode {
17+
NEWHOPE
18+
EMPIRE
19+
JEDI
20+
}

Samples/ClientSample/Program.cs

Lines changed: 61 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Collections.Generic;
23
using System.Linq;
34
using System.Net.Http;
45
using System.Threading.Tasks;
@@ -9,28 +10,71 @@ class Program
910
{
1011
static async Task Main(string[] args)
1112
{
13+
var epp = Client.TestClient.Episode.Empire;
1214
// lets create a simple console application that can sho github data
1315

14-
var accesstoken = args[0];
16+
//var accesstoken = args[0];
1517

16-
var httpClient = new HttpClient()
18+
//var httpClient = new HttpClient()
19+
//{
20+
// BaseAddress = new Uri("https://api.github.com/graphql"),
21+
// DefaultRequestHeaders = {
22+
// { "User-Agent", "Tocsoft.GraphQLCodeGen.Sample" },
23+
// { "Authorization", $"Bearer {accesstoken}" }
24+
// }
25+
//};
26+
27+
//httpClient.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
28+
//var client = new Client.GitHub.GitHubClient(httpClient);
29+
//var result = await client.UsersRepositoresAsync("tocsoft", 10);
30+
//var repo = result.User.First.Nodes.First();
31+
//var id = repo.Id;
32+
33+
////var hasStared = await client.AddStarAsync(id);
34+
//var user = await client.CurrentUserAsync();
35+
//var b = user.Viewer.Bio;
36+
}
37+
}
38+
39+
public class EnumClass
40+
{
41+
static Dictionary<string, EnumClass> lookup;
42+
43+
public string Value { get; }
44+
public bool IsDefined { get; }
45+
46+
private EnumClass(string value, bool isDefined)
47+
{
48+
this.Value = value;
49+
this.IsDefined = isDefined;
50+
}
51+
52+
private static EnumClass Create(string value, bool isDefined)
53+
{
54+
if (lookup.TryGetValue(value, out var val))
55+
{
56+
return val;
57+
}
58+
59+
lock (lookup)
1760
{
18-
BaseAddress = new Uri("https://api.github.com/graphql"),
19-
DefaultRequestHeaders = {
20-
{ "User-Agent", "Tocsoft.GraphQLCodeGen.Sample" },
21-
{ "Authorization", $"Bearer {accesstoken}" }
61+
if (lookup.TryGetValue(value, out val))
62+
{
63+
return val;
2264
}
23-
};
24-
25-
httpClient.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
26-
var client = new Client.GitHub.GitHubClient(httpClient);
27-
var result = await client.UsersRepositoresAsync("tocsoft", 10);
28-
var repo = result.User.First.Nodes.First();
29-
var id = repo.Id;
30-
31-
//var hasStared = await client.AddStarAsync(id);
32-
var user = await client.CurrentUserAsync();
33-
var b = user.Viewer.Bio;
65+
66+
val = new EnumClass(value, isDefined);
67+
lookup.Add(value, val);
68+
69+
return val;
70+
}
3471
}
72+
73+
public static implicit operator EnumClass(string value) => Create(value, false);
74+
public static implicit operator string(EnumClass value) => value.Value;
75+
76+
public static EnumClass Value1 = EnumClass.Create("VALUE1", true);
77+
public static EnumClass Value2 = EnumClass.Create("VALUE2", true);
78+
public static EnumClass Value3 = EnumClass.Create("VALUE3", true);
3579
}
3680
}

Samples/ClientSample/Sample.csproj

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<Import Project="$(MSBuildProjectDirectory)\..\..\Tocsoft.GraphQLCodeGen.MsBuild\test.props" />
4-
<Import Project="$(MSBuildProjectDirectory)\..\OutOfPathSettingsFileOverlay\overridesettings.props" />
4+
<!-- <Import Project="$(MSBuildProjectDirectory)\..\OutOfPathSettingsFileOverlay\overridesettings.props" /> -->
55
<PropertyGroup>
66
<OutputType>Exe</OutputType>
77
<TargetFramework>netcoreapp3.1</TargetFramework>
@@ -17,12 +17,18 @@
1717
<LangVersion>latest</LangVersion>
1818
</PropertyGroup>
1919

20+
<ItemGroup>
21+
<Compile Remove="Client\GitHub\**" />
22+
<EmbeddedResource Remove="Client\GitHub\**" />
23+
<None Remove="Client\GitHub\**" />
24+
</ItemGroup>
25+
2026
<ItemGroup>
2127
<PackageReference Include="Newtonsoft.Json" Version="10.0.3" />
2228
</ItemGroup>
2329

2430
<ItemGroup>
25-
<None Update="Client\GitHub\gqlsettings.json">
31+
<None Update="Client\test\gqlsettings.json">
2632
<Generator>MSBuild:GenerateGraphQLClient</Generator>
2733
</None>
2834
</ItemGroup>

Tocsoft.GraphQLCodeGen.Cli/CodeGeneratorSettingsLoader.cs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,13 @@ public IEnumerable<CodeGeneratorSettings> GenerateSettings(CodeGeneratorSettings
5050
sourceFiles.Add(loaded);
5151

5252
}
53-
54-
var grouped = sourceFiles.GroupBy(x => new
55-
{
56-
hash = x.SettingsHash()
57-
}).ToList();
58-
53+
var schemaFiles = sourceFiles.Select(x => x.SchemaSource?.Location);
54+
var grouped = sourceFiles
55+
.Where(x => !schemaFiles.Contains(x.Path)) // exclude files linked in a schemas
56+
.GroupBy(x => new
57+
{
58+
hash = x.SettingsHash()
59+
}).ToList();
5960

6061
return grouped.Select(x =>
6162
{

Tocsoft.GraphQLCodeGen.Cli/ObjectModel/EnumType.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
using System.Text;
44
using GraphQLParser.AST;
55
using System.Linq;
6+
using System.Diagnostics;
67

78
namespace Tocsoft.GraphQLCodeGen.ObjectModel
89
{
10+
[DebuggerDisplay(@"\{{Name}\}")]
911
internal class EnumType : IGraphQLInitter, IGraphQLType
1012
{
1113
private GraphQLEnumTypeDefinition op;

Tocsoft.GraphQLCodeGen.Cli/ObjectModel/GraphQLDocument.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public GraphQLDocument(GraphQLParser.AST.GraphQLDocument ast, IEnumerable<Locate
6868
this.settings = settings;
6969
List<object> items = ast.Definitions.Select(this.Visit).ToList();
7070
this.Operations = items.OfType<Operation>().ToList();
71-
this.types = items.OfType<IGraphQLType>().OrderBy(x => x.Name).ToList();
71+
this.types = items.OfType<IGraphQLType>().GroupBy(x=>x.Name).Select(x=>x.First()).ToList();
7272
this.astPrinter = new AstPrinter(settings.TypeNameDirective);
7373
foreach (IGraphQLInitter i in items.OfType<IGraphQLInitter>().Where(x => !(x is Operation)))
7474
{

0 commit comments

Comments
 (0)