-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathGenerateClient.razor.cs
More file actions
156 lines (135 loc) · 5.32 KB
/
GenerateClient.razor.cs
File metadata and controls
156 lines (135 loc) · 5.32 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
using Linq2GraphQL.Generator;
using Microsoft.AspNetCore.Components;
using System.IO.Compression;
using TabBlazor;
using TabBlazor.Services;
namespace Linq2GraphQL.Docs.Components
{
public partial class GenerateClient
{
[Inject] private TablerService tablerService { get; set; }
[Inject] private ToastService toastService { get; set; }
[Inject] private IModalService modalService { get; set; }
private GenerateOptions options = new();
private bool isLoading;
private List<GenerateOptions> demoOptions = new();
protected override void OnInitialized()
{
demoOptions.Add(new GenerateOptions
{
Namespace = "MyNamespace",
ClientName = "MyClient"
});
demoOptions.Add(new GenerateOptions
{
Url = "https://swapi-graphql.netlify.app/graphql",
Namespace = "StarWars.Client",
ClientName = "StarWarsClient"
});
demoOptions.Add(new GenerateOptions
{
Url = "https://spacex-production.up.railway.app/",
Namespace = "SpaceX",
ClientName = "SpaceXClient"
});
demoOptions.Add(new GenerateOptions
{
Url = "https://api.github.com/graphql",
Namespace = "Github",
ClientName = "GithubClient",
Token = "[Your Token]"
});
//
options = demoOptions.First();
}
private async Task CopyIntrospection()
{
string query;
if (options.IncludeDeprecated)
{
query = Generator.General.IntrospectionQueryIncludeDeprecated;
}
else
{
query = Generator.General.IntrospectionQuery;
}
await tablerService.CopyToClipboard(query);
await toastService.AddToastAsync(new ToastModel
{
Title = "Copy Complete",
Message = "Introspection query has been copied to the clipboard"
});
}
private async Task SaveEntriesAsync(List<FileEntry> entries)
{
using var memoryStream = new MemoryStream();
using (var archive = new ZipArchive(memoryStream, ZipArchiveMode.Create, true))
{
foreach (var entry in entries)
{
var file = archive.CreateEntry(entry.DirectoryName + "/" + entry.FileName);
using var entryStream = file.Open();
using var streamWriter = new StreamWriter(entryStream);
streamWriter.Write(entry.Content);
}
}
memoryStream.Seek(0, SeekOrigin.Begin);
await tablerService.SaveAsBinary($"{options.ClientName}.zip", "application/zip", memoryStream.ToArray());
await toastService.AddToastAsync(new ToastModel
{
Title = "Generate Complete",
Message = $"{options.ClientName}.zip has been been created! Please check you downloads."
});
}
private async Task GenerateClientJson()
{
try
{
isLoading = true;
var generator = new Generator.ClientGenerator(options.Namespace, options.ClientName, options.IncludeSubscriptions, EnumGeneratorStrategy.FailIfMissing, options.Nullable, options.IncludeDeprecated);
var entries = generator.Generate(options.Schema);
await SaveEntriesAsync(entries);
}
catch (Exception ex)
{
var component = new RenderComponent<ExceptionModal>().Set(e => e.Exception, ex);
var result = await modalService.ShowAsync("Error", component, new ModalOptions { Size = ModalSize.Large });
}
finally
{
isLoading = false;
}
}
private async Task GenerateClientAsync()
{
try
{
isLoading = true;
var generator = new ClientGenerator(options.Namespace, options.ClientName, options.IncludeSubscriptions, options.EnumGeneratorStrategy, options.Nullable, options.IncludeDeprecated);
var entries = await generator.GenerateAsync(new Uri(options.Url), options.Token);
await SaveEntriesAsync(entries);
}
catch (Exception ex)
{
var component = new RenderComponent<ExceptionModal>().Set(e => e.Exception, ex);
var result = await modalService.ShowAsync("Error", component, new ModalOptions { Size = ModalSize.Large });
}
finally
{
isLoading = false;
}
}
}
public class GenerateOptions
{
public string Namespace { get; set; }
public string ClientName { get; set; }
public bool IncludeSubscriptions { get; set; }
public bool Nullable { get; set; }
public string Url { get; set; }
public string Token { get; set; }
public bool IncludeDeprecated { get; set; }
public EnumGeneratorStrategy EnumGeneratorStrategy { get; set; }
public string Schema { get; set; }
}
}