Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion docs/Linq2GraphQL.Docs/Components/GenerateClient.razor
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@
<DataGridItem Title="Nullable client">
<Checkbox Switch @bind-Value="options.Nullable" />
</DataGridItem>


<DataGridItem Title="Include Deprecated">
<Checkbox Switch @bind-Value="options.IncludeDeprecated" />
</DataGridItem>

</DataGrid>

Expand Down
18 changes: 14 additions & 4 deletions docs/Linq2GraphQL.Docs/Components/GenerateClient.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,18 @@ protected override void OnInitialized()

private async Task CopyIntrospection()
{
string query;
if (options.IncludeDeprecated)
{
query = Generator.General.IntrospectionQueryIncludeDeprecated;
}
else
{
query = Generator.General.IntrospectionQuery;
}


await tablerService.CopyToClipboard(Generator.General.IntrospectionQuery);
await tablerService.CopyToClipboard(query);
await toastService.AddToastAsync(new ToastModel
{
Title = "Copy Complete",
Expand Down Expand Up @@ -92,7 +102,7 @@ private async Task GenerateClientJson()
try
{
isLoading = true;
var generator = new Generator.ClientGenerator(options.Namespace, options.ClientName, options.IncludeSubscriptions, EnumGeneratorStrategy.FailIfMissing, options.Nullable);
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);

Expand All @@ -114,7 +124,7 @@ private async Task GenerateClientAsync()
try
{
isLoading = true;
var generator = new ClientGenerator(options.Namespace, options.ClientName, options.IncludeSubscriptions, options.EnumGeneratorStrategy, options.Nullable);
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);
}
Expand All @@ -138,7 +148,7 @@ public class GenerateOptions
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; }
Expand Down
18 changes: 15 additions & 3 deletions src/Linq2GraphQL.Client/GraphClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@ public class GraphClient
{
private readonly IMemoryCache cache;
private readonly IOptions<GraphClientOptions> options;

public GraphClient(HttpClient httpClient, IOptions<GraphClientOptions> options, IServiceProvider provider)
private readonly bool includeDeprecated;

public GraphClient(HttpClient httpClient, IOptions<GraphClientOptions> options, IServiceProvider provider, bool includeDeprecated = false)
{
this.options = options;
this.includeDeprecated = includeDeprecated;
if (options.Value.UseSafeMode)
{
cache = provider.GetRequiredService<IMemoryCache>();
Expand Down Expand Up @@ -73,7 +75,17 @@ public async Task<GraphQLSchema> GetSchemaForSafeModeAsync()
{
var executor = new QueryExecutor<GraphQLSchema>(this);

var graphRequest = new GraphQLRequest { Query = Helpers.SchemaQueryIncludeDeprecated };
string query;
if (includeDeprecated)
{
query = Helpers.SchemaQueryIncludeDeprecated;
}
else
{
query = Helpers.SchemaQuery;
}

var graphRequest = new GraphQLRequest { Query = query };
return await executor.ExecuteRequestAsync("__schema", graphRequest);
});
}
Expand Down
35 changes: 16 additions & 19 deletions src/Linq2GraphQL.Generator/ClientGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,11 @@

namespace Linq2GraphQL.Generator
{
public class ClientGenerator
public class ClientGenerator(string namespaceName, string clientName, bool includeSubscriptions,
EnumGeneratorStrategy enumGeneratorStrategy, bool nullable, bool includeDeprecated)
{
private readonly string namespaceName;
private readonly string clientName;
private readonly bool includeSubscriptions;
private readonly EnumGeneratorStrategy enumGeneratorStrategy;
private readonly bool nullable;
private readonly List<FileEntry> entries = new();

public ClientGenerator(string namespaceName, string clientName, bool includeSubscriptions,
EnumGeneratorStrategy enumGeneratorStrategy, bool nullable)
{
this.namespaceName = namespaceName;
this.clientName = clientName;
this.includeSubscriptions = includeSubscriptions;
this.enumGeneratorStrategy = enumGeneratorStrategy;
this.nullable = nullable;
}

private void AddFile(string directory, string fileName, string content)
{
var infoText = $@"//---------------------------------------------------------------------
Expand All @@ -55,7 +41,18 @@ public async Task<List<FileEntry>> GenerateAsync(Uri uri, string authToken = nul
}

httpClient.DefaultRequestHeaders.UserAgent.Add(new ProductInfoHeaderValue("Linq2GraphQL", "1.0"));
using var response = await httpClient.PostAsJsonAsync(uri, new { query = General.IntrospectionQuery });

string query = "";
if (includeDeprecated)
{
query = General.IntrospectionQueryIncludeDeprecated;
}
else
{
query = General.IntrospectionQuery;
}

using var response = await httpClient.PostAsJsonAsync(uri, new { query = query });
if (!response.IsSuccessStatusCode)
{
throw new Exception(
Expand All @@ -72,7 +69,7 @@ public List<FileEntry> Generate(string schemaJson)
{
entries.Clear();

GeneratorSettings.Current = new GeneratorSettings { Nullable = this.nullable };
GeneratorSettings.Current = new GeneratorSettings { Nullable = nullable };

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


Console.WriteLine("Generate Client...");
var templateText = new ClientTemplate(namespaceName, clientName, queryType, mutationType, subscriptionType)
var templateText = new ClientTemplate(namespaceName, clientName, queryType, mutationType, subscriptionType, includeDeprecated)
.TransformText();
var fileName = clientName + ".cs";
AddFile(clientDirName, fileName, templateText);
Expand Down
87 changes: 87 additions & 0 deletions src/Linq2GraphQL.Generator/General.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,93 @@ fragment BaseType on __Type {
...OfTypeReqursive
}

query Into {
__schema {
types {
...BaseType

interfaces {
name
}
enumValues {
name
description
isDeprecated
deprecationReason
}

fields {
name
description
isDeprecated
deprecationReason
type {
...BaseType
}

args {
name
description
type {
...BaseType
}
}
}
inputFields {
name
description
isDeprecated
deprecationReason
type {
...BaseType
}
}
}
queryType {
name
}
mutationType {
name
}
subscriptionType {
name
}
}
}
";

public const string IntrospectionQueryIncludeDeprecated = @"fragment OfTypeFields on __Type {
name
kind
}

fragment OfTypeReqursive on __Type {
...OfTypeFields
ofType {
...OfTypeFields
ofType {
...OfTypeFields
ofType {
...OfTypeFields
ofType {
...OfTypeFields
ofType {
...OfTypeFields
}
}
}
}
}
}

fragment BaseType on __Type {
name
description
kind

...OfTypeReqursive
}

query Into {
__schema {
types {
Expand Down
12 changes: 8 additions & 4 deletions src/Linq2GraphQL.Generator/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ private static async Task Main(string[] args)

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

var includeDeprecated = new Option<bool>(new[] { "--deprecated", "-d" }, "Include Deprecated as Obsolete");

var rootCommand = new RootCommand("Generate GraphQL client")
{
uriArgument,
Expand All @@ -32,7 +34,8 @@ private static async Task Main(string[] args)
authToken,
includeSubscriptions,
enumStrategy,
nullable
nullable,
includeDeprecated
};

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

await GenerateClientAsync(uriValue, outputFolderValue, namespaceValue, clientNameValue,
includeSubscriptionsValue, authTokenValue, enumStrategyValue, nullableValue);
includeSubscriptionsValue, authTokenValue, enumStrategyValue, nullableValue, deprecatedValue);
}
);

Expand All @@ -58,13 +62,13 @@ await GenerateClientAsync(uriValue, outputFolderValue, namespaceValue, clientNam
}

private static async Task GenerateClientAsync(Uri uri, string outputFolder, string namespaceName, string name,
bool includeSubscriptions, string authToken, string enumStrategy, bool nullable)
bool includeSubscriptions, string authToken, string enumStrategy, bool nullable, bool includeDeprecated)
{
var enumStrat = enumStrategy != null && enumStrategy.Equals("AddUnknownOption", StringComparison.InvariantCultureIgnoreCase)
? EnumGeneratorStrategy.AddUnknownOption
: EnumGeneratorStrategy.FailIfMissing;

var generator = new ClientGenerator(namespaceName, name, includeSubscriptions, enumStrat, nullable);
var generator = new ClientGenerator(namespaceName, name, includeSubscriptions, enumStrat, nullable, includeDeprecated);
var entries = await generator.GenerateAsync(uri, authToken);

var outputPath = Path.GetFullPath(outputFolder, Environment.CurrentDirectory);
Expand Down
2 changes: 1 addition & 1 deletion src/Linq2GraphQL.Generator/Properties/launchSettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"profiles": {
"Linq2GraphQL.Generator": {
"commandName": "Project",
"commandLineArgs": "https://localhost:7184/graphql/ -c=\"SampleClient\" -n=\"Linq2GraphQL.TestClient\" -o=\"C:\\Code\\Github\\Linq2GraphQL.Client\\test\\Linq2GraphQL.TestClient\\Generated\" -s=true"
"commandLineArgs": "https://localhost:7184/graphql/ -c=\"SampleClient\" -n=\"Linq2GraphQL.TestClient\" -o=\"C:\\Code\\Github\\Linq2GraphQL.Client\\test\\Linq2GraphQL.TestClient\\Generated\" -s=true -d=true"
}
}
}
Loading
Loading