diff --git a/src/StrawberryShake/CodeGeneration/src/CodeGeneration.CSharp/CSharpGenerator.cs b/src/StrawberryShake/CodeGeneration/src/CodeGeneration.CSharp/CSharpGenerator.cs index 72efc4330be..a43ddbf0c87 100644 --- a/src/StrawberryShake/CodeGeneration/src/CodeGeneration.CSharp/CSharpGenerator.cs +++ b/src/StrawberryShake/CodeGeneration/src/CodeGeneration.CSharp/CSharpGenerator.cs @@ -312,6 +312,7 @@ private static IReadOnlyList GenerateCSharpDocuments( CSharpGeneratorSettings settings) { var generatorSettings = new CSharpSyntaxGeneratorSettings( + settings.AccessModifier, settings.NoStore, settings.InputRecords, settings.EntityRecords, diff --git a/src/StrawberryShake/CodeGeneration/src/CodeGeneration.CSharp/CSharpGeneratorSettings.cs b/src/StrawberryShake/CodeGeneration/src/CodeGeneration.CSharp/CSharpGeneratorSettings.cs index e815fae4efd..baa27b68871 100644 --- a/src/StrawberryShake/CodeGeneration/src/CodeGeneration.CSharp/CSharpGeneratorSettings.cs +++ b/src/StrawberryShake/CodeGeneration/src/CodeGeneration.CSharp/CSharpGeneratorSettings.cs @@ -18,6 +18,11 @@ public class CSharpGeneratorSettings /// The root namespace of the client. /// public string Namespace { get; set; } = "StrawberryShake.GraphQL"; + + /// + /// The access modifier of the client. + /// + public AccessModifier AccessModifier { get; set; } = AccessModifier.Internal; /// /// Defines if a schema needs to be fully valid. diff --git a/src/StrawberryShake/CodeGeneration/src/CodeGeneration.CSharp/Generators/ClientGenerator.cs b/src/StrawberryShake/CodeGeneration/src/CodeGeneration.CSharp/Generators/ClientGenerator.cs index e370cab8883..2924a89bff8 100644 --- a/src/StrawberryShake/CodeGeneration/src/CodeGeneration.CSharp/Generators/ClientGenerator.cs +++ b/src/StrawberryShake/CodeGeneration/src/CodeGeneration.CSharp/Generators/ClientGenerator.cs @@ -1,3 +1,4 @@ +using System; using StrawberryShake.CodeGeneration.CSharp.Builders; using StrawberryShake.CodeGeneration.Descriptors; using StrawberryShake.CodeGeneration.Descriptors.Operations; @@ -21,6 +22,7 @@ protected override void Generate( var classBuilder = ClassBuilder .New() + .SetAccessModifier(settings.AccessModifier) .SetName(fileName) .SetComment(descriptor.Documentation) .AddImplements(descriptor.InterfaceType.ToString()); diff --git a/src/StrawberryShake/CodeGeneration/src/CodeGeneration.CSharp/Builders/AccessModifier.cs b/src/StrawberryShake/CodeGeneration/src/CodeGeneration/AccessModifier.cs similarity index 63% rename from src/StrawberryShake/CodeGeneration/src/CodeGeneration.CSharp/Builders/AccessModifier.cs rename to src/StrawberryShake/CodeGeneration/src/CodeGeneration/AccessModifier.cs index 6511a5d3b9c..42a318f8bf9 100644 --- a/src/StrawberryShake/CodeGeneration/src/CodeGeneration.CSharp/Builders/AccessModifier.cs +++ b/src/StrawberryShake/CodeGeneration/src/CodeGeneration/AccessModifier.cs @@ -1,4 +1,4 @@ -namespace StrawberryShake.CodeGeneration.CSharp.Builders; +namespace StrawberryShake.CodeGeneration; public enum AccessModifier { diff --git a/src/StrawberryShake/CodeGeneration/src/CodeGeneration/CSharpSyntaxGeneratorSettings.cs b/src/StrawberryShake/CodeGeneration/src/CodeGeneration/CSharpSyntaxGeneratorSettings.cs index 7bd7b4e80f1..ca75c16d710 100644 --- a/src/StrawberryShake/CodeGeneration/src/CodeGeneration/CSharpSyntaxGeneratorSettings.cs +++ b/src/StrawberryShake/CodeGeneration/src/CodeGeneration/CSharpSyntaxGeneratorSettings.cs @@ -9,15 +9,23 @@ public class CSharpSyntaxGeneratorSettings /// Creates a new code generator settings instance. /// public CSharpSyntaxGeneratorSettings( + AccessModifier accessModifier, bool noStore, bool inputRecords, - bool entityRecords, bool razorComponents) + bool entityRecords, + bool razorComponents) { + AccessModifier = accessModifier; NoStore = noStore; InputRecords = inputRecords; EntityRecords = entityRecords; RazorComponents = razorComponents; } + + /// + /// Generates the client with specified access modifier. + /// + public AccessModifier AccessModifier { get; } /// /// Generates the client without a store diff --git a/src/StrawberryShake/CodeGeneration/test/CodeGeneration.CSharp.Tests/GeneratorTestHelper.cs b/src/StrawberryShake/CodeGeneration/test/CodeGeneration.CSharp.Tests/GeneratorTestHelper.cs index 3a5063aa415..0468c4143c7 100644 --- a/src/StrawberryShake/CodeGeneration/test/CodeGeneration.CSharp.Tests/GeneratorTestHelper.cs +++ b/src/StrawberryShake/CodeGeneration/test/CodeGeneration.CSharp.Tests/GeneratorTestHelper.cs @@ -24,7 +24,8 @@ public static IReadOnlyList AssertError(params string[] fileNames) new CSharpGeneratorSettings { Namespace = "Foo.Bar", - ClientName = "FooClient" + ClientName = "FooClient", + AccessModifier = AccessModifier.Public }) .Result; @@ -86,6 +87,7 @@ public static void AssertResult( { Namespace = settings.Namespace ?? "Foo.Bar", ClientName = settings.ClientName ?? "FooClient", + AccessModifier = AccessModifier.Public, StrictSchemaValidation = settings.StrictValidation, RequestStrategy = settings.RequestStrategy, TransportProfiles = settings.Profiles, diff --git a/src/StrawberryShake/CodeGeneration/test/CodeGeneration.CSharp.Tests/__snapshots__/EntityGeneratorTests.Generate_StarWars_Client_With_Defer.snap b/src/StrawberryShake/CodeGeneration/test/CodeGeneration.CSharp.Tests/__snapshots__/EntityGeneratorTests.Generate_StarWars_Client_With_Defer.snap index 745ee9cb0be..0a302e5fa82 100644 --- a/src/StrawberryShake/CodeGeneration/test/CodeGeneration.CSharp.Tests/__snapshots__/EntityGeneratorTests.Generate_StarWars_Client_With_Defer.snap +++ b/src/StrawberryShake/CodeGeneration/test/CodeGeneration.CSharp.Tests/__snapshots__/EntityGeneratorTests.Generate_StarWars_Client_With_Defer.snap @@ -1,4 +1,4 @@ -// ReSharper disable BuiltInTypeReferenceStyle +// ReSharper disable BuiltInTypeReferenceStyle // ReSharper disable RedundantNameQualifier // ReSharper disable ArrangeObjectCreationWhenTypeEvident // ReSharper disable UnusedType.Global diff --git a/src/StrawberryShake/CodeGeneration/test/CodeGeneration.CSharp.Tests/__snapshots__/EntityIdFactoryGeneratorTests.Simple_Uuid_Entity.snap b/src/StrawberryShake/CodeGeneration/test/CodeGeneration.CSharp.Tests/__snapshots__/EntityIdFactoryGeneratorTests.Simple_UUID_Entity.snap similarity index 100% rename from src/StrawberryShake/CodeGeneration/test/CodeGeneration.CSharp.Tests/__snapshots__/EntityIdFactoryGeneratorTests.Simple_Uuid_Entity.snap rename to src/StrawberryShake/CodeGeneration/test/CodeGeneration.CSharp.Tests/__snapshots__/EntityIdFactoryGeneratorTests.Simple_UUID_Entity.snap diff --git a/src/StrawberryShake/CodeGeneration/test/CodeGeneration.CSharp.Tests/__snapshots__/ErrorGeneratorTests.ClosePaymentsMutationErrors.snap b/src/StrawberryShake/CodeGeneration/test/CodeGeneration.CSharp.Tests/__snapshots__/ErrorGeneratorTests.ClosePaymentsMutationErrors.snap index de9c7691c1b..23439a7ca76 100644 --- a/src/StrawberryShake/CodeGeneration/test/CodeGeneration.CSharp.Tests/__snapshots__/ErrorGeneratorTests.ClosePaymentsMutationErrors.snap +++ b/src/StrawberryShake/CodeGeneration/test/CodeGeneration.CSharp.Tests/__snapshots__/ErrorGeneratorTests.ClosePaymentsMutationErrors.snap @@ -1,4 +1,4 @@ -// ReSharper disable BuiltInTypeReferenceStyle +// ReSharper disable BuiltInTypeReferenceStyle // ReSharper disable RedundantNameQualifier // ReSharper disable ArrangeObjectCreationWhenTypeEvident // ReSharper disable UnusedType.Global diff --git a/src/StrawberryShake/CodeGeneration/test/CodeGeneration.CSharp.Tests/__snapshots__/InputGeneratorTests.Operation_With_Comments_With_Input_Records.snap b/src/StrawberryShake/CodeGeneration/test/CodeGeneration.CSharp.Tests/__snapshots__/InputGeneratorTests.Operation_With_Comments_With_Input_Records.snap index ab28a7f154a..7bb9ea43286 100644 --- a/src/StrawberryShake/CodeGeneration/test/CodeGeneration.CSharp.Tests/__snapshots__/InputGeneratorTests.Operation_With_Comments_With_Input_Records.snap +++ b/src/StrawberryShake/CodeGeneration/test/CodeGeneration.CSharp.Tests/__snapshots__/InputGeneratorTests.Operation_With_Comments_With_Input_Records.snap @@ -1,4 +1,4 @@ -// ReSharper disable BuiltInTypeReferenceStyle +// ReSharper disable BuiltInTypeReferenceStyle // ReSharper disable RedundantNameQualifier // ReSharper disable ArrangeObjectCreationWhenTypeEvident // ReSharper disable UnusedType.Global diff --git a/src/StrawberryShake/CodeGeneration/test/CodeGeneration.CSharp.Tests/__snapshots__/ScalarGeneratorTests.Complete_Schema_With_Uuid_And_DateTime.snap b/src/StrawberryShake/CodeGeneration/test/CodeGeneration.CSharp.Tests/__snapshots__/ScalarGeneratorTests.Complete_Schema_With_UUID_And_DateTime.snap similarity index 100% rename from src/StrawberryShake/CodeGeneration/test/CodeGeneration.CSharp.Tests/__snapshots__/ScalarGeneratorTests.Complete_Schema_With_Uuid_And_DateTime.snap rename to src/StrawberryShake/CodeGeneration/test/CodeGeneration.CSharp.Tests/__snapshots__/ScalarGeneratorTests.Complete_Schema_With_UUID_And_DateTime.snap diff --git a/src/StrawberryShake/Tooling/src/Configuration/StrawberryShakeSettings.cs b/src/StrawberryShake/Tooling/src/Configuration/StrawberryShakeSettings.cs index 74bcea34cd7..cfca6673ade 100644 --- a/src/StrawberryShake/Tooling/src/Configuration/StrawberryShakeSettings.cs +++ b/src/StrawberryShake/Tooling/src/Configuration/StrawberryShakeSettings.cs @@ -22,6 +22,11 @@ public class StrawberryShakeSettings /// public string? Url { get; set; } + /// + /// Gets or sets the access modifier of the client.. + /// + public string? AccessModifier { get; set; } + /// /// Defines if the generator shall generate dependency injection code. /// diff --git a/src/StrawberryShake/Tooling/src/dotnet-graphql/GeneratorHelpers.cs b/src/StrawberryShake/Tooling/src/dotnet-graphql/GeneratorHelpers.cs index 0b9b69c7ca9..54e54d18d7e 100644 --- a/src/StrawberryShake/Tooling/src/dotnet-graphql/GeneratorHelpers.cs +++ b/src/StrawberryShake/Tooling/src/dotnet-graphql/GeneratorHelpers.cs @@ -62,6 +62,7 @@ public static CSharpGeneratorSettings CreateSettings( { ClientName = configSettings.Name, Namespace = configSettings.Namespace ?? args.RootNamespace ?? rootNamespace, + AccessModifier = GetAccessModifier(configSettings.AccessModifier), StrictSchemaValidation = configSettings.StrictSchemaValidation ?? args.StrictSchemaValidation, NoStore = configSettings.NoStore ?? args.NoStore, @@ -75,6 +76,20 @@ public static CSharpGeneratorSettings CreateSettings( }; } + private static AccessModifier GetAccessModifier(string? accessModifier) + { + if (string.IsNullOrWhiteSpace(accessModifier)) + { + return AccessModifier.Internal; + } + else if (Enum.TryParse(accessModifier, true, out var result)) + { + return result; + } + + throw new NotSupportedException($"The access modifier `{accessModifier}` is not supported."); + } + private static IDocumentHashProvider GetHashProvider(string hashAlgorithm) => hashAlgorithm.ToLowerInvariant() switch { diff --git a/src/StrawberryShake/Tooling/test/Configuration.Tests/GraphQLConfigTests.cs b/src/StrawberryShake/Tooling/test/Configuration.Tests/GraphQLConfigTests.cs index a6b0c61398f..6846b69c921 100644 --- a/src/StrawberryShake/Tooling/test/Configuration.Tests/GraphQLConfigTests.cs +++ b/src/StrawberryShake/Tooling/test/Configuration.Tests/GraphQLConfigTests.cs @@ -28,6 +28,7 @@ public void Load_Json() ""extensions"": { ""strawberryShake"": { ""name"": ""Client"", + ""accessModifier"": ""public"", ""dependencyInjection"": true, ""strictSchemaValidation"": true, ""hashAlgorithm"": ""md5"", @@ -60,6 +61,7 @@ public void Load_Json_With_Transport_Profiles() ""extensions"": { ""strawberryShake"": { ""name"": ""Client"", + ""accessModifier"": ""public"", ""dependencyInjection"": true, ""strictSchemaValidation"": true, ""hashAlgorithm"": ""md5"", @@ -94,6 +96,7 @@ public void Load_Json_With_Records() ""extensions"": { ""strawberryShake"": { ""name"": ""Client"", + ""accessModifier"": ""public"", ""dependencyInjection"": true, ""strictSchemaValidation"": true, ""hashAlgorithm"": ""md5"", diff --git a/src/StrawberryShake/Tooling/test/Configuration.Tests/__snapshots__/GraphQLConfigTests.Load_Json.snap b/src/StrawberryShake/Tooling/test/Configuration.Tests/__snapshots__/GraphQLConfigTests.Load_Json.snap index 15d711c2d49..b99bdbbfcb9 100644 --- a/src/StrawberryShake/Tooling/test/Configuration.Tests/__snapshots__/GraphQLConfigTests.Load_Json.snap +++ b/src/StrawberryShake/Tooling/test/Configuration.Tests/__snapshots__/GraphQLConfigTests.Load_Json.snap @@ -7,6 +7,7 @@ "Name": "Client", "Namespace": null, "Url": null, + "AccessModifier": "public", "DependencyInjection": true, "StrictSchemaValidation": true, "HashAlgorithm": "md5", diff --git a/src/StrawberryShake/Tooling/test/Configuration.Tests/__snapshots__/GraphQLConfigTests.Load_Json_With_Records.snap b/src/StrawberryShake/Tooling/test/Configuration.Tests/__snapshots__/GraphQLConfigTests.Load_Json_With_Records.snap index 7766acdf949..cfb96fd12f7 100644 --- a/src/StrawberryShake/Tooling/test/Configuration.Tests/__snapshots__/GraphQLConfigTests.Load_Json_With_Records.snap +++ b/src/StrawberryShake/Tooling/test/Configuration.Tests/__snapshots__/GraphQLConfigTests.Load_Json_With_Records.snap @@ -7,6 +7,7 @@ "Name": "Client", "Namespace": null, "Url": null, + "AccessModifier": "public", "DependencyInjection": true, "StrictSchemaValidation": true, "HashAlgorithm": "md5", diff --git a/src/StrawberryShake/Tooling/test/Configuration.Tests/__snapshots__/GraphQLConfigTests.Load_Json_With_Transport_Profiles.snap b/src/StrawberryShake/Tooling/test/Configuration.Tests/__snapshots__/GraphQLConfigTests.Load_Json_With_Transport_Profiles.snap index eb1a1970ad4..bddede28d00 100644 --- a/src/StrawberryShake/Tooling/test/Configuration.Tests/__snapshots__/GraphQLConfigTests.Load_Json_With_Transport_Profiles.snap +++ b/src/StrawberryShake/Tooling/test/Configuration.Tests/__snapshots__/GraphQLConfigTests.Load_Json_With_Transport_Profiles.snap @@ -7,6 +7,7 @@ "Name": "Client", "Namespace": null, "Url": null, + "AccessModifier": "public", "DependencyInjection": true, "StrictSchemaValidation": true, "HashAlgorithm": "md5", diff --git a/website/src/docs/strawberryshake/v13/configuration.md b/website/src/docs/strawberryshake/v13/configuration.md index 034d83a6d37..4674d2b6ee3 100644 --- a/website/src/docs/strawberryshake/v13/configuration.md +++ b/website/src/docs/strawberryshake/v13/configuration.md @@ -23,6 +23,8 @@ Here is a full configuration with all possibilities: "namespace": "Demo", // The URL of the GraphQL api you want to consume with the client "url": "https://workshop.chillicream.com/graphql/", + // The access level modifier of the generated client + "accessModifier": "public", // Shall your client be based on dependency injection? If yes, all needed setup code // will be generated for you, so that you only have to add the client to your DI container. "dependencyInjection": true