This repository was archived by the owner on Jul 18, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathProgram.cs
More file actions
194 lines (180 loc) · 5.6 KB
/
Program.cs
File metadata and controls
194 lines (180 loc) · 5.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
using System;
using System.CommandLine;
using System.CommandLine.Invocation;
using System.IO;
using System.Net.Http;
using System.Net.Http.Json;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
using GraphQL;
using GraphQL.Client.Http;
using GraphQL.Client.Serializer.SystemTextJson;
using Spectre.Console;
namespace GraphQLinq.Scaffolding
{
class Program
{
private const string IntrospectionQuery = @"{
__schema {
types {
name
interfaces {
name
}
description
kind
enumValues {
name
}
description
fields {
name
description
type {
name
kind
ofType {
name
kind
ofType {
name
kind
ofType {
name
kind
ofType {
name
kind
ofType {
name
kind
}
}
}
}
}
}
args {
name
description
type {
kind
name
description
ofType {
name
kind
ofType {
name
kind
ofType {
name
kind
}
}
}
}
}
}
inputFields {
name
description
type {
name
kind
ofType {
name
kind
ofType {
name
kind
ofType {
name
kind
}
}
}
}
}
}
queryType {
name
}
mutationType {
name
}
subscriptionType {
name
}
}
}
";
private static async Task Main(string[] args)
{
var generate = new RootCommand("Scaffold GraphQL client code")
{
new Argument<Uri>("endpoint", "Endpoint of the GraphQL service"),
new Option<string>(new []{ "--output", "-o" }, () => "", "Output folder"),
new Option<string>(new []{ "--namespace", "-n" }, () => "","Namespace of generated classes"),
new Option<string>(new []{ "--context", "-c" }, () => "Query","Name of the generated context classes"),
};
generate.Handler = CommandHandler.Create<Uri, string, string, string, IConsole>(HandleGenerate);
await generate.InvokeAsync(args);
}
private static async Task HandleGenerate(Uri endpoint, string output, string @namespace, string context, IConsole console)
{
//var webClient = new WebClient();
//webClient.Headers.Add("Content-Type", "application/json");
//var downloadString = webClient.UploadString("endpoint", query);
AnsiConsole.MarkupLine("[bold]Welcome to GraphQL Client Scaffolding tool[/]");
AnsiConsole.WriteLine();
string outputFolder = Path.IsPathRooted(output) ? output : Path.Combine(Environment.CurrentDirectory, output);
AnsiConsole.MarkupLine("Scaffolding GraphQL client code for [bold]{0}[/] to [bold]{1}[/]", endpoint, outputFolder);
var data = await AnsiConsole.Status().StartAsync("Performing introspection", async ctx =>
{
try
{
AnsiConsole.WriteLine("Running introspection query ...");
GraphQLHttpClient client = new GraphQLHttpClient(endpoint, new SystemTextJsonSerializer());
var request = new GraphQLRequest
{
Query = IntrospectionQuery
};
var responseMessage = await client.SendQueryAsync<Data>(request);
AnsiConsole.WriteLine("Reading and deserializing schema information ...");
return responseMessage.Data;
//var schemaJson = await responseMessage;
//return JsonSerializer.Deserialize<RootSchemaObject>(schemaJson, new JsonSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.CamelCase });
}
catch (Exception ex)
{
throw ex;
}
});
AnsiConsole.WriteLine();
var contextClassFullName = AnsiConsole.Status().Start($"Scaffolding GraphQL client code {endpoint}", statusContext =>
{
var codeGenerationOptions = new CodeGenerationOptions
{
Namespace = @namespace,
NormalizeCasing = true,
OutputDirectory = outputFolder,
ContextName = context
};
var graphQLClassesGenerator = new GraphQLClassesGenerator(codeGenerationOptions);
return graphQLClassesGenerator.GenerateClient(data.Schema, endpoint.AbsoluteUri);
});
AnsiConsole.WriteLine();
AnsiConsole.MarkupLine("[bold]Scaffolding complete[/]");
AnsiConsole.MarkupLine("Use [bold]{0}[/] to run strongly typed LINQ queries", contextClassFullName);
}
}
class CodeGenerationOptions
{
public string? Namespace { get; set; } = "";
public string ContextName { get; set; } = "";
public string OutputDirectory { get; set; } = "";
public bool NormalizeCasing { get; set; }
}
}