Skip to content

Commit 9df8b4f

Browse files
committed
fixes #9
1 parent 0e6fb84 commit 9df8b4f

12 files changed

Lines changed: 232 additions & 16 deletions

File tree

GraphQLCodeGen.v3.ncrunchsolution

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<SolutionConfiguration>
2+
<Settings>
3+
<AllowParallelTestExecution>True</AllowParallelTestExecution>
4+
<SolutionConfigured>True</SolutionConfigured>
5+
</Settings>
6+
</SolutionConfiguration>

Tocsoft.GraphQLCodeGen.Cli/CodeGenerator.cs

Lines changed: 46 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,13 @@ public CodeGenerator(ILogger logger, CodeGeneratorSettings settings, IEnumerable
3939
this.introspectionProviders = introspectionProviders;
4040
}
4141

42-
public async Task<bool> GenerateAsync()
42+
internal IEnumerable<NamedSource> Sources { get; set; }
43+
internal ObjectModel.GraphQLDocument Document { get; set; }
44+
internal Models.ViewModel Model { get; set; }
45+
internal string GeneratedCode { get; set; }
46+
internal bool HasParsingErrors { get; set; }
47+
48+
internal async Task LoadSource()
4349
{
4450
IIntrosepctionProvider provider = this.introspectionProviders.Single(x => x.SchemaType == this.settings.Schema.SchemaType());
4551

@@ -52,28 +58,57 @@ public async Task<bool> GenerateAsync()
5258
{
5359
sources.Add(s);
5460
}
55-
// we want to track the file that the operation is loaded from
56-
// lets make a locatino index look up table and provide it
57-
ObjectModel.GraphQLDocument doc = Parse(sources);
5861

59-
if (doc.Errors.Any())
62+
Sources = sources;
63+
}
64+
65+
internal void Export()
66+
{
67+
Directory.CreateDirectory(Path.GetDirectoryName(this.settings.OutputPath));
68+
File.WriteAllText(this.settings.OutputPath, GeneratedCode);
69+
}
70+
71+
internal void Render()
72+
{
73+
this.GeneratedCode = new TemplateEngine(this.settings.Templates, logger).Generate(Model);
74+
}
75+
76+
internal void Parse()
77+
{
78+
Document = IntrospectedSchemeParser.Parse(Sources);
79+
80+
if (Document.Errors.Any())
6081
{
61-
foreach (var error in doc.Errors)
82+
foreach (var error in Document.Errors)
6283
{
6384
logger.Error(error.ToString());
6485
}
86+
HasParsingErrors = true;
87+
return;
88+
}
89+
90+
Model = new Models.ViewModel(Document, this.settings);
91+
92+
HasParsingErrors = false;
93+
}
94+
95+
public async Task<bool> GenerateAsync()
96+
{
97+
await LoadSource();
98+
99+
Parse();
100+
101+
if (HasParsingErrors)
102+
{
65103
return false;
66104
}
67105

68-
Models.ViewModel model = new Models.ViewModel(doc, this.settings);
106+
Render();
69107

70-
string fileResult = new TemplateEngine(this.settings.Templates, logger).Generate(model);
108+
Export();
71109

72-
Directory.CreateDirectory(Path.GetDirectoryName(this.settings.OutputPath));
73-
File.WriteAllText(this.settings.OutputPath, fileResult);
74110
return true;
75111
}
76-
77112
}
78113

79114
public class CodeGeneratorSettings

Tocsoft.GraphQLCodeGen.Cli/ObjectModel/GraphQLDocument.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ private void UnPackType(GraphQLType type, ValueTypeReference target)
197197
{
198198
if (type is GraphQLNonNullType nonNullType)
199199
{
200-
target.CanValueBeNull = true;
200+
target.CanValueBeNull = false;
201201
UnPackType(nonNullType.Type, target);
202202
}
203203
else if (type is GraphQLListType listType)
@@ -275,15 +275,15 @@ internal enum WellknownScalarType
275275

276276
internal class ValueTypeReference
277277
{
278-
public bool CanValueBeNull { get; set; }
279-
public bool IsCollection { get; set; }
280-
public bool CanCollectionBeNull { get; set; }
278+
public bool CanValueBeNull { get; set; } = true;
279+
public bool IsCollection { get; set; } = false;
280+
public bool CanCollectionBeNull { get; set; } = true;
281281
public IGraphQLType Type { get; set; }
282282

283283
public override string ToString()
284284
{
285285
string val = this.Type.Name;
286-
if (this.CanValueBeNull)
286+
if (!this.CanValueBeNull)
287287
{
288288
val += "!";
289289
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
using System.Runtime.CompilerServices;
2+
[assembly:InternalsVisibleTo("Tocsoft.GraphQLCodeGen.Tests")]

Tocsoft.GraphQLCodeGen.Cli/Templates/cs/CSharp.template

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,9 @@ namespace {{Namespace}}
152152
{{!# TypeReference_DateTime}}
153153
{{~> RenderTypeReference name='DateTime' isValueType=true fixCase=false isCollection=IsCollection nullable=CanValueBeNull }}
154154

155+
{{!# TypeReference_Date}}
156+
{{~> RenderTypeReference name='DateTime' isValueType=true fixCase=false isCollection=IsCollection nullable=CanValueBeNull }}
157+
155158
{{!# TypeReference_GUID}}
156159
{{~> RenderTypeReference name='Guid' isValueType=true fixCase=false isCollection=IsCollection nullable=CanValueBeNull }}
157160

Tocsoft.GraphQLCodeGen.Cli/Templates/ts/typescript.template

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,9 @@ export class {{pascalCase Name}} {
147147

148148
{{!# TypeReference_DateTime}}
149149
{{~> RenderTypeReference typename='Date' isValueType=true fixCase=false isCollection=IsCollection nullable=CanValueBeNull collectionNullable=CanCollectionBeNull }}
150+
151+
{{!# TypeReference_Date}}
152+
{{~> RenderTypeReference typename='Date' isValueType=true fixCase=false isCollection=IsCollection nullable=CanValueBeNull collectionNullable=CanCollectionBeNull }}
150153

151154
{{!# RenderTypeReference}}
152155
{{~#if isCollection }}Array<{{/if~}}
@@ -177,6 +180,11 @@ export class {{pascalCase Name}} {
177180
{{~#if Type.IsCollection~}}
178181
if(json["{{camelCase Name}}"]){ result.{{camelCase Name}} = json["{{camelCase Name}}"].map(v=> v ? new Date(v) : null); }{{/if~}}
179182
{{~#unless Type.IsCollection~}}result.{{camelCase Name}} = json["{{camelCase Name}}"] ? new Date(json["{{camelCase Name}}"]) : null;{{~/unless~}}
183+
184+
{{!# FieldSetter_Date}}
185+
{{~#if Type.IsCollection~}}
186+
if(json["{{camelCase Name}}"]){ result.{{camelCase Name}} = json["{{camelCase Name}}"].map(v=> v ? new Date(v) : null); }{{/if~}}
187+
{{~#unless Type.IsCollection~}}result.{{camelCase Name}} = json["{{camelCase Name}}"] ? new Date(json["{{camelCase Name}}"]) : null;{{~/unless~}}
180188

181189
{{!# RenderFieldSetter}}
182190
{{~#if isCollection~}}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<ProjectConfiguration>
2+
<Settings>
3+
<IgnoredTests>
4+
<AllTestsSelector />
5+
</IgnoredTests>
6+
</Settings>
7+
</ProjectConfiguration>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#! schema: Schema.gql
2+
#! class: Test
3+
#! output: test.cs
4+
5+
query{
6+
test(id: "safsa"){
7+
nullable,
8+
nonnullable
9+
}
10+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#! schema: ../GitHubSchema.json
2+
#! class: Test
3+
#! output: test.cs
4+
5+
query {
6+
user(login: "tocsoft"){
7+
login,
8+
bio,
9+
repositories(first : 1){
10+
nodes{
11+
databaseId,
12+
createdAt
13+
}
14+
}
15+
}
16+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
schema {
2+
query: Query
3+
mutation: Mutation
4+
}
5+
6+
type Query {
7+
test(id: string!): Droid
8+
}
9+
type Droid {
10+
nullable: Date
11+
nonnullable: Date!
12+
}
13+
scalar Date

0 commit comments

Comments
 (0)