Skip to content

Commit d9c4f9c

Browse files
committed
Made Include Deprecated opt-in
1 parent 1b7514b commit d9c4f9c

File tree

12 files changed

+190
-82
lines changed

12 files changed

+190
-82
lines changed

docs/Linq2GraphQL.Docs/Components/GenerateClient.razor

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,10 @@
3131
<DataGridItem Title="Nullable client">
3232
<Checkbox Switch @bind-Value="options.Nullable" />
3333
</DataGridItem>
34-
34+
35+
<DataGridItem Title="Include Deprecated">
36+
<Checkbox Switch @bind-Value="options.IncludeDeprecated" />
37+
</DataGridItem>
3538

3639
</DataGrid>
3740

docs/Linq2GraphQL.Docs/Components/GenerateClient.razor.cs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,18 @@ protected override void OnInitialized()
5252

5353
private async Task CopyIntrospection()
5454
{
55+
string query;
56+
if (options.IncludeDeprecated)
57+
{
58+
query = Generator.General.IntrospectionQueryIncludeDeprecated;
59+
}
60+
else
61+
{
62+
query = Generator.General.IntrospectionQuery;
63+
}
64+
5565

56-
await tablerService.CopyToClipboard(Generator.General.IntrospectionQuery);
66+
await tablerService.CopyToClipboard(query);
5767
await toastService.AddToastAsync(new ToastModel
5868
{
5969
Title = "Copy Complete",
@@ -92,7 +102,7 @@ private async Task GenerateClientJson()
92102
try
93103
{
94104
isLoading = true;
95-
var generator = new Generator.ClientGenerator(options.Namespace, options.ClientName, options.IncludeSubscriptions, EnumGeneratorStrategy.FailIfMissing, options.Nullable);
105+
var generator = new Generator.ClientGenerator(options.Namespace, options.ClientName, options.IncludeSubscriptions, EnumGeneratorStrategy.FailIfMissing, options.Nullable, options.IncludeDeprecated);
96106
var entries = generator.Generate(options.Schema);
97107
await SaveEntriesAsync(entries);
98108

@@ -114,7 +124,7 @@ private async Task GenerateClientAsync()
114124
try
115125
{
116126
isLoading = true;
117-
var generator = new ClientGenerator(options.Namespace, options.ClientName, options.IncludeSubscriptions, options.EnumGeneratorStrategy, options.Nullable);
127+
var generator = new ClientGenerator(options.Namespace, options.ClientName, options.IncludeSubscriptions, options.EnumGeneratorStrategy, options.Nullable, options.IncludeDeprecated);
118128
var entries = await generator.GenerateAsync(new Uri(options.Url), options.Token);
119129
await SaveEntriesAsync(entries);
120130
}
@@ -138,7 +148,7 @@ public class GenerateOptions
138148
public bool Nullable { get; set; }
139149
public string Url { get; set; }
140150
public string Token { get; set; }
141-
151+
public bool IncludeDeprecated { get; set; }
142152
public EnumGeneratorStrategy EnumGeneratorStrategy { get; set; }
143153

144154
public string Schema { get; set; }

src/Linq2GraphQL.Client/GraphClient.cs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,12 @@ public class GraphClient
1212
{
1313
private readonly IMemoryCache cache;
1414
private readonly IOptions<GraphClientOptions> options;
15-
16-
public GraphClient(HttpClient httpClient, IOptions<GraphClientOptions> options, IServiceProvider provider)
15+
private readonly bool includeDeprecated;
16+
17+
public GraphClient(HttpClient httpClient, IOptions<GraphClientOptions> options, IServiceProvider provider, bool includeDeprecated = false)
1718
{
1819
this.options = options;
20+
this.includeDeprecated = includeDeprecated;
1921
if (options.Value.UseSafeMode)
2022
{
2123
cache = provider.GetRequiredService<IMemoryCache>();
@@ -73,7 +75,17 @@ public async Task<GraphQLSchema> GetSchemaForSafeModeAsync()
7375
{
7476
var executor = new QueryExecutor<GraphQLSchema>(this);
7577

76-
var graphRequest = new GraphQLRequest { Query = Helpers.SchemaQueryIncludeDeprecated };
78+
string query;
79+
if (includeDeprecated)
80+
{
81+
query = Helpers.SchemaQueryIncludeDeprecated;
82+
}
83+
else
84+
{
85+
query = Helpers.SchemaQuery;
86+
}
87+
88+
var graphRequest = new GraphQLRequest { Query = query };
7789
return await executor.ExecuteRequestAsync("__schema", graphRequest);
7890
});
7991
}

src/Linq2GraphQL.Generator/ClientGenerator.cs

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,25 +10,11 @@
1010

1111
namespace Linq2GraphQL.Generator
1212
{
13-
public class ClientGenerator
13+
public class ClientGenerator(string namespaceName, string clientName, bool includeSubscriptions,
14+
EnumGeneratorStrategy enumGeneratorStrategy, bool nullable, bool includeDeprecated)
1415
{
15-
private readonly string namespaceName;
16-
private readonly string clientName;
17-
private readonly bool includeSubscriptions;
18-
private readonly EnumGeneratorStrategy enumGeneratorStrategy;
19-
private readonly bool nullable;
2016
private readonly List<FileEntry> entries = new();
2117

22-
public ClientGenerator(string namespaceName, string clientName, bool includeSubscriptions,
23-
EnumGeneratorStrategy enumGeneratorStrategy, bool nullable)
24-
{
25-
this.namespaceName = namespaceName;
26-
this.clientName = clientName;
27-
this.includeSubscriptions = includeSubscriptions;
28-
this.enumGeneratorStrategy = enumGeneratorStrategy;
29-
this.nullable = nullable;
30-
}
31-
3218
private void AddFile(string directory, string fileName, string content)
3319
{
3420
var infoText = $@"//---------------------------------------------------------------------
@@ -55,7 +41,18 @@ public async Task<List<FileEntry>> GenerateAsync(Uri uri, string authToken = nul
5541
}
5642

5743
httpClient.DefaultRequestHeaders.UserAgent.Add(new ProductInfoHeaderValue("Linq2GraphQL", "1.0"));
58-
using var response = await httpClient.PostAsJsonAsync(uri, new { query = General.IntrospectionQuery });
44+
45+
string query = "";
46+
if (includeDeprecated)
47+
{
48+
query = General.IntrospectionQueryIncludeDeprecated;
49+
}
50+
else
51+
{
52+
query = General.IntrospectionQuery;
53+
}
54+
55+
using var response = await httpClient.PostAsJsonAsync(uri, new { query = query });
5956
if (!response.IsSuccessStatusCode)
6057
{
6158
throw new Exception(
@@ -72,7 +69,7 @@ public List<FileEntry> Generate(string schemaJson)
7269
{
7370
entries.Clear();
7471

75-
GeneratorSettings.Current = new GeneratorSettings { Nullable = this.nullable };
72+
GeneratorSettings.Current = new GeneratorSettings { Nullable = nullable };
7673

7774
var rootSchema = JsonSerializer.Deserialize<RootSchema>(schemaJson,
7875
new JsonSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.CamelCase });
@@ -175,7 +172,7 @@ public List<FileEntry> Generate(string schemaJson)
175172

176173

177174
Console.WriteLine("Generate Client...");
178-
var templateText = new ClientTemplate(namespaceName, clientName, queryType, mutationType, subscriptionType)
175+
var templateText = new ClientTemplate(namespaceName, clientName, queryType, mutationType, subscriptionType, includeDeprecated)
179176
.TransformText();
180177
var fileName = clientName + ".cs";
181178
AddFile(clientDirName, fileName, templateText);

src/Linq2GraphQL.Generator/General.cs

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,93 @@ fragment BaseType on __Type {
3636
...OfTypeReqursive
3737
}
3838
39+
query Into {
40+
__schema {
41+
types {
42+
...BaseType
43+
44+
interfaces {
45+
name
46+
}
47+
enumValues {
48+
name
49+
description
50+
isDeprecated
51+
deprecationReason
52+
}
53+
54+
fields {
55+
name
56+
description
57+
isDeprecated
58+
deprecationReason
59+
type {
60+
...BaseType
61+
}
62+
63+
args {
64+
name
65+
description
66+
type {
67+
...BaseType
68+
}
69+
}
70+
}
71+
inputFields {
72+
name
73+
description
74+
isDeprecated
75+
deprecationReason
76+
type {
77+
...BaseType
78+
}
79+
}
80+
}
81+
queryType {
82+
name
83+
}
84+
mutationType {
85+
name
86+
}
87+
subscriptionType {
88+
name
89+
}
90+
}
91+
}
92+
";
93+
94+
public const string IntrospectionQueryIncludeDeprecated = @"fragment OfTypeFields on __Type {
95+
name
96+
kind
97+
}
98+
99+
fragment OfTypeReqursive on __Type {
100+
...OfTypeFields
101+
ofType {
102+
...OfTypeFields
103+
ofType {
104+
...OfTypeFields
105+
ofType {
106+
...OfTypeFields
107+
ofType {
108+
...OfTypeFields
109+
ofType {
110+
...OfTypeFields
111+
}
112+
}
113+
}
114+
}
115+
}
116+
}
117+
118+
fragment BaseType on __Type {
119+
name
120+
description
121+
kind
122+
123+
...OfTypeReqursive
124+
}
125+
39126
query Into {
40127
__schema {
41128
types {

src/Linq2GraphQL.Generator/Program.cs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ private static async Task Main(string[] args)
2323

2424
var nullable = new Option<bool>(new[] { "--nullable", "-nu" }, "Nullable client");
2525

26+
var includeDeprecated = new Option<bool>(new[] { "--deprecated", "-d" }, "Include Deprecated as Obsolete");
27+
2628
var rootCommand = new RootCommand("Generate GraphQL client")
2729
{
2830
uriArgument,
@@ -32,7 +34,8 @@ private static async Task Main(string[] args)
3234
authToken,
3335
includeSubscriptions,
3436
enumStrategy,
35-
nullable
37+
nullable,
38+
includeDeprecated
3639
};
3740

3841
rootCommand.SetHandler(async context =>
@@ -46,9 +49,10 @@ private static async Task Main(string[] args)
4649
var includeSubscriptionsValue = result.GetValueForOption(includeSubscriptions);
4750
var enumStrategyValue = result.GetValueForOption(enumStrategy);
4851
var nullableValue = result.GetValueForOption(nullable);
52+
var deprecatedValue = result.GetValueForOption(includeDeprecated);
4953

5054
await GenerateClientAsync(uriValue, outputFolderValue, namespaceValue, clientNameValue,
51-
includeSubscriptionsValue, authTokenValue, enumStrategyValue, nullableValue);
55+
includeSubscriptionsValue, authTokenValue, enumStrategyValue, nullableValue, deprecatedValue);
5256
}
5357
);
5458

@@ -58,13 +62,13 @@ await GenerateClientAsync(uriValue, outputFolderValue, namespaceValue, clientNam
5862
}
5963

6064
private static async Task GenerateClientAsync(Uri uri, string outputFolder, string namespaceName, string name,
61-
bool includeSubscriptions, string authToken, string enumStrategy, bool nullable)
65+
bool includeSubscriptions, string authToken, string enumStrategy, bool nullable, bool includeDeprecated)
6266
{
6367
var enumStrat = enumStrategy != null && enumStrategy.Equals("AddUnknownOption", StringComparison.InvariantCultureIgnoreCase)
6468
? EnumGeneratorStrategy.AddUnknownOption
6569
: EnumGeneratorStrategy.FailIfMissing;
6670

67-
var generator = new ClientGenerator(namespaceName, name, includeSubscriptions, enumStrat, nullable);
71+
var generator = new ClientGenerator(namespaceName, name, includeSubscriptions, enumStrat, nullable, includeDeprecated);
6872
var entries = await generator.GenerateAsync(uri, authToken);
6973

7074
var outputPath = Path.GetFullPath(outputFolder, Environment.CurrentDirectory);

src/Linq2GraphQL.Generator/Properties/launchSettings.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"profiles": {
33
"Linq2GraphQL.Generator": {
44
"commandName": "Project",
5-
"commandLineArgs": "https://localhost:7184/graphql/ -c=\"SampleClient\" -n=\"Linq2GraphQL.TestClient\" -o=\"C:\\Code\\Github\\Linq2GraphQL.Client\\test\\Linq2GraphQL.TestClient\\Generated\" -s=true"
5+
"commandLineArgs": "https://localhost:7184/graphql/ -c=\"SampleClient\" -n=\"Linq2GraphQL.TestClient\" -o=\"C:\\Code\\Github\\Linq2GraphQL.Client\\test\\Linq2GraphQL.TestClient\\Generated\" -s=true -d=true"
66
}
77
}
88
}

0 commit comments

Comments
 (0)