Skip to content

Commit

Permalink
Merge pull request #104 from byme8/feature/warnings
Browse files Browse the repository at this point in the history
Improve warnings handling during the client generation.
  • Loading branch information
byme8 authored May 4, 2024
2 parents aac1317 + b79195d commit f4f6e1b
Show file tree
Hide file tree
Showing 15 changed files with 133 additions and 58 deletions.
10 changes: 10 additions & 0 deletions schema.verified.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,16 @@
}
]
},
"warningsToIgnore": {
"type": [
"array",
"null"
],
"description": "The warnings to ignore when generating the client",
"items": {
"type": "string"
}
},
"scalars": {
"type": [
"null",
Expand Down
12 changes: 7 additions & 5 deletions src/TestApp/TestStandardLibrary/Generated/GraphQL.g.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
// 555c59849405436847c6bba08a02c152
// This file generated for ZeroQL.
// <auto-generated/>
#pragma warning disable 8618
// 2938f381f05ea9f538686268fde7023c
// <auto-generated>
// This file generated for ZeroQL 1.0.0.0.
//
// Do not modify this file manually. Any changes will be lost after regeneration.
// </auto-generated>
#pragma warning disable 8618, CS8603, CS1066
#nullable enable
namespace GraphQL.TestServer
{
Expand Down Expand Up @@ -1478,7 +1481,6 @@ public static void Init()
}
}
}
#pragma warning restore 8618

// Netstandard compatibility
namespace System.Runtime.CompilerServices
Expand Down
14 changes: 8 additions & 6 deletions src/TestApp/ZeroQL.TestApp/Generated/GraphQL.g.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
// 4d479b39e03556dad074684160a6bb5b
// This file generated for ZeroQL.
// <auto-generated/>
#pragma warning disable 8618
// e3ffb4224ceaa474687dabfe4a87e72c
// <auto-generated>
// This file generated for ZeroQL 1.0.0.0.
//
// Do not modify this file manually. Any changes will be lost after regeneration.
// </auto-generated>
#pragma warning disable 8618, CS8603, CS1066
#nullable enable
namespace GraphQL.TestServer
{
Expand Down Expand Up @@ -1445,5 +1448,4 @@ public static void Init()
global::ZeroQL.Json.ZeroQLJsonSerializersStore.Converters[typeof(global::GraphQL.TestServer.PostContent)] = new ZeroQLPostContentConverter();
}
}
}
#pragma warning restore 8618
}
23 changes: 21 additions & 2 deletions src/ZeroQL.CLI/Commands/GenerateCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
using ZeroQL.Bootstrap;
using ZeroQL.CLI.Converters;
using ZeroQL.Config;
using ZeroQL.Core.Config;
using ZeroQL.Core.Enums;
using ZeroQL.Extensions;
using ZeroQL.Internal;

namespace ZeroQL.CLI.Commands;
Expand Down Expand Up @@ -41,6 +41,11 @@ public class GenerateCommand : ICommand
[CommandOption("visibility", 'v', Description = "The visibility within the assembly for the generated client")]
public ClientVisibility? Visibility { get; set; }

[CommandOption(
"warnings-to-ignore",
Description = "The warnings to ignore when generating the client. Example: --warnings-to-ignore=CS0168,CS0219")]
public string? WarningsToIgnore { get; set; }

[CommandOption(
"scalars",
Description =
Expand Down Expand Up @@ -90,7 +95,8 @@ await console.Error.WriteLineAsync(
{
ClientName = ClientName,
NetstandardCompatibility = NetstandardCompatibility,
Scalars = scalars
Scalars = scalars,
WarningsToIgnore = WarningsToIgnore?.Split(","),
};

var outputPath = Path.IsPathRooted(Output) ? Output : Path.GetFullPath(Output);
Expand All @@ -117,6 +123,7 @@ await console.Output.WriteLineAsync(
Directory.CreateDirectory(outputFolder);
}

Exception? exception = null;
var count = 10;
for (int i = 0; i < count; i++)
{
Expand All @@ -129,11 +136,18 @@ await console.Output.WriteLineAsync(
}
catch (Exception e)
{
exception = e;
await console.Error.WriteLineAsync($"Failed write to '{outputPath}'");
await console.Error.WriteLineAsync($"Attempting in 1 second. {i + 1} / {count}");
await Task.Delay(1000);
}
}

if (exception is not null)
{
await console.Error.WriteLineAsync("Failed to write to file.");
await console.Error.WriteLineAsync(exception.Message);
}
}

private async Task<bool> Validate(IConsole console)
Expand Down Expand Up @@ -205,6 +219,11 @@ public async Task<bool> ReadConfig(IConsole console)
{
Visibility = config.Visibility;
}

if (WarningsToIgnore is null)
{
WarningsToIgnore = config.WarningsToIgnore?.Join(",");
}

if (string.IsNullOrEmpty(Output) && !string.IsNullOrEmpty(config.Output))
{
Expand Down
5 changes: 5 additions & 0 deletions src/ZeroQL.Core/Config/ZeroQLFileConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ public class ZeroQLFileConfig
/// </summary>
public ClientVisibility? Visibility { get; set; }

/// <summary>
/// The warnings to ignore when generating the client
/// </summary>
public string[]? WarningsToIgnore { get; set; }

/// <summary>
/// The custom scalars to use when generating the client
/// </summary>
Expand Down
2 changes: 1 addition & 1 deletion src/ZeroQL.Runtime/GraphQLClientLambdaExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,6 @@ public static async Task<GraphQLResult<TResult>> Mutation<TQuery, TMutation, TRe
.Where(o => !o.Name.StartsWith("<>"))
.ToDictionary(o => o.Name, o => o.GetValue(query.Target));

return variables;
return variables!;
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
// a0aa84fe842288ea40877ce0679472cd
// This file generated for ZeroQL.
// <auto-generated/>
#pragma warning disable 8618
// 81bc49ae79a8451869f4dc3676ce6968
// <auto-generated>
// This file generated for ZeroQL 1.0.0.0.
//
// Do not modify this file manually. Any changes will be lost after regeneration.
// </auto-generated>
#pragma warning disable 8618, CS8603, CS1066
#nullable enable
namespace TestApp
{
Expand Down Expand Up @@ -130,5 +133,4 @@ namespace TestApp
{
}
}
}
#pragma warning restore 8618
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
hashCodeLine
// This file generated for ZeroQL.
// <auto-generated/>
#pragma warning disable 8618
// <auto-generated>
// This file generated for ZeroQL 1.0.0.0.
//
// Do not modify this file manually. Any changes will be lost after regeneration.
// </auto-generated>
#pragma warning disable 8618, CS8603, CS1066
#nullable enable
namespace GraphQLClient
{
Expand Down Expand Up @@ -1477,5 +1480,4 @@ namespace GraphQLClient
global::ZeroQL.Json.ZeroQLJsonSerializersStore.Converters[typeof(global::GraphQLClient.PostContent)] = new ZeroQLPostContentConverter();
}
}
}
#pragma warning restore 8618
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
hashCodeLine
// This file generated for ZeroQL.
// <auto-generated/>
#pragma warning disable 8618
// <auto-generated>
// This file generated for ZeroQL 1.0.0.0.
//
// Do not modify this file manually. Any changes will be lost after regeneration.
// </auto-generated>
#pragma warning disable 8618, CS8603, CS1066
#nullable enable
namespace GraphQLClient
{
Expand Down Expand Up @@ -1477,5 +1480,4 @@ namespace GraphQLClient
global::ZeroQL.Json.ZeroQLJsonSerializersStore.Converters[typeof(global::GraphQLClient.PostContent)] = new ZeroQLPostContentConverter();
}
}
}
#pragma warning restore 8618
}
62 changes: 45 additions & 17 deletions src/ZeroQL.Tests/CLI/CLITests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ namespace ZeroQL.Tests.CLI;
public class CliTests : IntegrationTest
{
const string GeneratedFileName = "GraphQL.g.cs";

[Fact]
public async Task CanPullSchemaFromRemoteServer()
{
using var console = new FakeInMemoryConsole();

var tempFile = Path.GetTempFileName();
var pullCommand = new PullSchemaCommand
{
Expand All @@ -29,7 +29,7 @@ public async Task CanPullSchemaFromRemoteServer()
var schema = await File.ReadAllTextAsync(tempFile);
await Verify(schema);
}

[Fact]
public async Task GeneratedCodeShouldCompile()
{
Expand All @@ -48,24 +48,50 @@ public async Task GeneratedCodeShouldCompile()
await generateCommand.ExecuteAsync(console);

console.ReadErrorString().Should().BeEmpty();

var project = TestProject.Project;

var generatedCode = await File.ReadAllTextAsync(tempFile);
var document = project.Documents.Single(x => x.Name == GeneratedFileName);

await project
.RemoveDocument(document.Id)
.AddDocument(GeneratedFileName, generatedCode)
.Project
.CompileToRealAssembly();
}

[Fact]
public async Task GeneratedCodeShouldDisableCustomWarnings()
{
using var console = new FakeInMemoryConsole();

var tempFile = Path.GetTempFileName();
var generateCommand = new GenerateCommand
{
Schema = "../../../../TestApp/ZeroQL.TestApp/schema.graphql",
Namespace = "GraphQL.TestServer",
ClientName = "TestServerClient",
Output = tempFile,
WarningsToIgnore = "CS0168,CS0219",
Force = true
};

await generateCommand.ExecuteAsync(console);

console.ReadErrorString().Should().BeEmpty();

var generatedCode = await File.ReadAllLinesAsync(tempFile);

await Verify(generatedCode[6]);
}


[Fact]
public async Task ShouldNotGenerateCodeIfNotNeeded()
{
var outputFile = Path.GetTempFileName();

using var console = new FakeInMemoryConsole();
var generateCommand = new GenerateCommand
{
Expand All @@ -74,11 +100,11 @@ public async Task ShouldNotGenerateCodeIfNotNeeded()
ClientName = "TestServerClient",
Output = outputFile,
};


// should generate file the first time
await generateCommand.ExecuteAsync(console);

File.Exists(outputFile).Should().BeTrue();
console.ReadErrorString().Should().BeEmpty();
var lastWriteTime = File.GetLastWriteTime(outputFile);
Expand All @@ -88,9 +114,10 @@ public async Task ShouldNotGenerateCodeIfNotNeeded()
await generateCommand.ExecuteAsync(console);
File.Exists(outputFile).Should().BeTrue();
File.GetLastWriteTime(outputFile).Should().Be(lastWriteTime);
console.ReadOutputString().Should().Contain("The source code is up-to-date with graphql schema. Skipping code generation.");
console.ReadOutputString().Should()
.Contain("The source code is up-to-date with graphql schema. Skipping code generation.");
console.Clear();

// setting updated. should trigger new code generation
generateCommand.ClientName = "UpdatedClientName";
await generateCommand.ExecuteAsync(console);
Expand All @@ -108,7 +135,7 @@ public async Task Extract()
var path = Path.GetDirectoryName(fileName)!;
Directory.CreateDirectory(path);
await File.WriteAllBytesAsync(fileName, bytes);

using var console = new FakeInMemoryConsole();
var generateCommand = new ExtractQueriesCommand();
generateCommand.AssemblyFile = fileName;
Expand All @@ -120,21 +147,22 @@ public async Task Extract()
console.ReadErrorString().Should().BeEmpty();
Directory.EnumerateFiles(generateCommand.Output).Should().NotBeEmpty();
}

[Fact]
public async Task<ExtractQueriesCommand> ExtractMutationAndQuery()
{
var uniqueId = Guid.NewGuid().ToString("N");
var fileName = $"../../../../TestApp/ZeroQL.TestApp/bin/Debug/net6.0/{uniqueId}/TestApp.dll";
var project = await TestProject.Project
.ReplacePartOfDocumentAsync("Program.cs", "// place to replace", @"await qlClient.Mutation(static q => q.AddUser(""Jon"", ""Smith"", o => o.Id));");

.ReplacePartOfDocumentAsync("Program.cs", "// place to replace",
@"await qlClient.Mutation(static q => q.AddUser(""Jon"", ""Smith"", o => o.Id));");

var bytes = await project.CompileToRealAssemblyAsBytes();

var path = Path.GetDirectoryName(fileName)!;
Directory.CreateDirectory(path);
await File.WriteAllBytesAsync(fileName, bytes);

using var console = new FakeInMemoryConsole();
var command = new ExtractQueriesCommand();
command.AssemblyFile = fileName;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#pragma warning disable CS0168, CS0219
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
ClientName: ServiceZeroQLClient,
Output: QL.g.cs,
Visibility: Internal,
WarningsToIgnore: CS0168,CS0219,
Scalars: [
{
Point: Geometry.Point
Expand Down
1 change: 1 addition & 0 deletions src/ZeroQL.Tests/CLI/ConfigCliTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ public async Task ConfigCanBeParsed()
"clientName": "ServiceZeroQLClient",
"visibility": "Internal",
"output": "QL.g.cs",
"warningsToIgnore": ["CS0168", "CS0219"],
"scalars": {
"Point": "Geometry.Point",
"Rect": "Geometry.Rect"
Expand Down
Loading

0 comments on commit f4f6e1b

Please sign in to comment.