Skip to content

Commit fb4e5ff

Browse files
committed
fix templates so they actually compile
improved template blocks
1 parent 62b58f7 commit fb4e5ff

File tree

2 files changed

+80
-41
lines changed

2 files changed

+80
-41
lines changed

Tocsoft.GraphQLCodeGen.Cli/TemplateEngine.cs

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,32 @@ public TemplateEngine(IEnumerable<string> templates, IDictionary<string, string>
4747
writer.WriteSafeString(args[0].ToString().Replace(toReplace, toReplaceWith));
4848
});
4949

50-
this.engine.RegisterHelper("ifTemplateSet", (writer, context, args) =>
50+
HandlebarsBlockHelper ifTemplateHelper = (output, options, context, args) =>
5151
{
52-
var val = this.engine.Compile("{{> " + args[0].ToString() + "}}")(context);
53-
writer.WriteSafeString(string.IsNullOrWhiteSpace(val) ? "" : args[1]);
54-
});
52+
var val = this.engine.Compile("{{> " + args[0].ToString() + "}}")((object)context)?.ToString()?.Trim();
53+
var isSet = !string.IsNullOrWhiteSpace(val);
54+
55+
if (args.Length > 1)
56+
{
57+
isSet = val.Equals(args[1]?.ToString());
58+
}
59+
60+
if (isSet)
61+
{
62+
options.Template(output, context);
63+
}
64+
else
65+
{
66+
options.Inverse(output, context);
67+
}
68+
};
69+
this.engine.RegisterHelper("ifTemplate", ifTemplateHelper);
70+
71+
//this.engine.RegisterHelper("ifTemplateSet", (writer, context, args) =>
72+
//{
73+
// var val = this.engine.Compile("{{> " + args[0].ToString() + "}}")(context);
74+
// writer.WriteSafeString(string.IsNullOrWhiteSpace(val) ? "" : args[1]);
75+
//});
5576

5677

5778
this.engine.Configuration.TextEncoder = new NullEncoder();

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

Lines changed: 55 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ using System.Collections.Generic;
66
using System.Linq;
77
using System.Net.Http;
88
using System.Threading.Tasks;
9+
using Newtonsoft.Json;
910

1011
#line hidden
1112
namespace {{Namespace}}
@@ -14,16 +15,6 @@ namespace {{Namespace}}
1415

1516
{{> ClientClass}}
1617

17-
{{> ClientException}}
18-
19-
{{#each Types}}
20-
{{> TypeOrInterface}}
21-
{{/each}}
22-
23-
{{#each Enums}}
24-
{{> Enum}}
25-
{{/each}}
26-
2718
{{#if Namespace~}}
2819
}
2920
{{~/if~}}
@@ -32,23 +23,41 @@ namespace {{Namespace}}
3223
{{!# ClientExceptionBaseClass}} Exception
3324
{{!# interfaceBase }}
3425
{{!# jsonConverters }}
35-
26+
{{!# BeforeInterfaceOperation}}
3627

3728
{{!# ClientException}}
3829
public sealed class {{ClassName}}Exception : {{> ClientExceptionBaseClass}}
39-
{
30+
{
31+
{{ifTemplate 'ClientExceptionBaseClass' 'Exception'}}
32+
public HttpResponseMessage Response { get; private set; }
33+
public IEnumerable<string> ErrorMessages { get; private set; }
34+
35+
public {{ClassName}}Exception(HttpResponseMessage response)
36+
: base("Error running graphql query, see response for more details")
37+
{
38+
this.Response = response;
39+
}
40+
41+
public {{ClassName}}Exception(IEnumerable<string> errorMessages, HttpResponseMessage response)
42+
: base("Error running graphql query, see error messages or response for more details")
43+
{
44+
this.Response = response;
45+
this.ErrorMessages = errorMessages;
46+
}
47+
{{else}}
4048
public {{ClassName}}Exception(HttpResponseMessage response)
41-
: base(response)
42-
{
43-
}
44-
45-
public {{ClassName}}Exception(IEnumerable<string> errorMessages, HttpResponseMessage response)
46-
: base(errorMessages, response)
47-
{
48-
}
49+
: base(response)
50+
{
51+
}
52+
53+
public {{ClassName}}Exception(IEnumerable<string> errorMessages, HttpResponseMessage response)
54+
: base(errorMessages, response)
55+
{
56+
}
57+
{{/ifTemplate}}
4958
}
5059

51-
{{!# ClientInterfaceBase}} {{ifTemplateSet 'InterfaceBase' ':'}} {{ > InterfaceBase}}
60+
{{!# ClientInterfaceBase}} {{ifTemplate 'InterfaceBase'}}: {{ > InterfaceBase}} {{/ifTemplate}}
5261

5362
{{!# ClientInterface}}
5463

@@ -76,9 +85,10 @@ namespace {{Namespace}}
7685
return JsonConvert.SerializeObject(request, jsonSettings);
7786
}
7887

79-
private string DeserializeBody<T>(GraphQLRequest request)
88+
private async Task<GraphQLResponseData<T>> DeserializeBodyAsync<T>(HttpResponseMessage response)
8089
{
81-
var errorsResult = JsonConvert.DeserializeObject<GraphQLResponseErrors<T>>(jsonResult, jsonSettings);
90+
var jsonResult = await response.Content.ReadAsStringAsync();
91+
var errorsResult = JsonConvert.DeserializeObject<GraphQLResponseErrors<T>>(jsonResult, jsonSettings);
8292

8393
if (errorsResult.errors?.Any() == true) {
8494
throw new {{ClassName}}Exception(errorsResult.errors.Select(x => x.Message), response);
@@ -117,10 +127,18 @@ namespace {{Namespace}}
117127
public string Message { get; set; }
118128
}
119129

130+
{{> ClientException}}
131+
120132
{{#each Operations}}
121133
{{> Operation}}
134+
{{/each}}
135+
136+
{{#each Types}}
137+
{{> TypeOrInterface}}
138+
{{/each}}
122139

123-
140+
{{#each Enums}}
141+
{{> Enum}}
124142
{{/each}}
125143
}
126144

@@ -154,27 +172,29 @@ namespace {{Namespace}}
154172
{{/each}}
155173
}
156174
{{!# InterfaceOperation}}
175+
{{ > BeforeInterfaceOperation}}
157176
Task<{{> TypeReference ResultType}}> {{pascalCase Name}}Async({{> ArgumentList Arguments}});
158177

159178
{{!# Operation}}
160-
public async Task<{{> TypeReference ResultType}}> {{pascalCase Name}}Async({{> ArgumentList Arguments}})
161-
{
162-
var query = @"{{replace Query '"' '""'}}";
163179

164-
var variables = new Dictionary<string, object>
180+
public Task<{{> TypeReference ResultType}}> {{pascalCase Name}}Async({{> ArgumentList Arguments}})
181+
{
182+
return _{{Name}}Async(new Dictionary<string, object>
165183
{
166184
{{#each Arguments}}
167185
{ @"{{replace Name '"' '""'}}", {{camelCase Name}} },
168186
{{/each}}
169-
};
170-
171-
var stringContent = SerializeBody(new GraphQLRequest()
187+
});
188+
}
189+
190+
private async Task<{{> TypeReference ResultType}}> _{{Name}}Async(Dictionary<string, object> variables)
191+
{
192+
const string query = @"{{replace Query '"' '""'}}";
193+
var stringContent = SerializeBody(new GraphQLRequest()
172194
{
173195
query = query,
174196
variables = variables,
175197
});
176-
177-
178198
var request = new HttpRequestMessage();
179199

180200
var content = new StringContent(stringContent);
@@ -193,17 +213,15 @@ namespace {{Namespace}}
193213
throw new {{../ClassName}}Exception(response);
194214
}
195215

196-
var jsonResult = await response.Content.ReadAsStringAsync();
197-
198-
var result = DeserializeBody<{{> TypeReference ResultType}}>(jsonResult)
216+
var result = await DeserializeBodyAsync<{{> TypeReference ResultType}}>(response);
199217

200218
if (result == null)
201219
{
202220
throw new {{../ClassName}}Exception(response);
203221
}
204222

205223
return result.data;
206-
}
224+
}
207225

208226
{{!# ArgumentList}}
209227
{{~#each~ .}}

0 commit comments

Comments
 (0)