Skip to content

Commit

Permalink
Add configurable access for generated clients (ChilliCream#6374)
Browse files Browse the repository at this point in the history
  • Loading branch information
Fernando committed Aug 6, 2023
1 parent 388f5c9 commit 9abd8b1
Show file tree
Hide file tree
Showing 18 changed files with 52 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,7 @@ private static IReadOnlyList<GeneratorResult> GenerateCSharpDocuments(
CSharpGeneratorSettings settings)
{
var generatorSettings = new CSharpSyntaxGeneratorSettings(
settings.AccessModifier,
settings.NoStore,
settings.InputRecords,
settings.EntityRecords,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ public class CSharpGeneratorSettings
/// The root namespace of the client.
/// </summary>
public string Namespace { get; set; } = "StrawberryShake.GraphQL";

/// <summary>
/// The access modifier of the client.
/// </summary>
public AccessModifier AccessModifier { get; set; } = AccessModifier.Internal;

/// <summary>
/// Defines if a schema needs to be fully valid.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using StrawberryShake.CodeGeneration.CSharp.Builders;
using StrawberryShake.CodeGeneration.Descriptors;
using StrawberryShake.CodeGeneration.Descriptors.Operations;
Expand All @@ -21,6 +22,7 @@ protected override void Generate(

var classBuilder = ClassBuilder
.New()
.SetAccessModifier(settings.AccessModifier)
.SetName(fileName)
.SetComment(descriptor.Documentation)
.AddImplements(descriptor.InterfaceType.ToString());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace StrawberryShake.CodeGeneration.CSharp.Builders;
namespace StrawberryShake.CodeGeneration;

public enum AccessModifier
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,23 @@ public class CSharpSyntaxGeneratorSettings
/// Creates a new code generator settings instance.
/// </summary>
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;
}

/// <summary>
/// Generates the client with specified access modifier.
/// </summary>
public AccessModifier AccessModifier { get; }

/// <summary>
/// Generates the client without a store
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ public static IReadOnlyList<IError> AssertError(params string[] fileNames)
new CSharpGeneratorSettings
{
Namespace = "Foo.Bar",
ClientName = "FooClient"
ClientName = "FooClient",
AccessModifier = AccessModifier.Public
})
.Result;

Expand Down Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// ReSharper disable BuiltInTypeReferenceStyle
// ReSharper disable BuiltInTypeReferenceStyle
// ReSharper disable RedundantNameQualifier
// ReSharper disable ArrangeObjectCreationWhenTypeEvident
// ReSharper disable UnusedType.Global
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// ReSharper disable BuiltInTypeReferenceStyle
// ReSharper disable BuiltInTypeReferenceStyle
// ReSharper disable RedundantNameQualifier
// ReSharper disable ArrangeObjectCreationWhenTypeEvident
// ReSharper disable UnusedType.Global
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// ReSharper disable BuiltInTypeReferenceStyle
// ReSharper disable BuiltInTypeReferenceStyle
// ReSharper disable RedundantNameQualifier
// ReSharper disable ArrangeObjectCreationWhenTypeEvident
// ReSharper disable UnusedType.Global
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ public class StrawberryShakeSettings
/// </summary>
public string? Url { get; set; }

/// <summary>
/// Gets or sets the access modifier of the client..
/// </summary>
public string? AccessModifier { get; set; }

/// <summary>
/// Defines if the generator shall generate dependency injection code.
/// </summary>
Expand Down
15 changes: 15 additions & 0 deletions src/StrawberryShake/Tooling/src/dotnet-graphql/GeneratorHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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>(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
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public void Load_Json()
""extensions"": {
""strawberryShake"": {
""name"": ""Client"",
""accessModifier"": ""public"",
""dependencyInjection"": true,
""strictSchemaValidation"": true,
""hashAlgorithm"": ""md5"",
Expand Down Expand Up @@ -60,6 +61,7 @@ public void Load_Json_With_Transport_Profiles()
""extensions"": {
""strawberryShake"": {
""name"": ""Client"",
""accessModifier"": ""public"",
""dependencyInjection"": true,
""strictSchemaValidation"": true,
""hashAlgorithm"": ""md5"",
Expand Down Expand Up @@ -94,6 +96,7 @@ public void Load_Json_With_Records()
""extensions"": {
""strawberryShake"": {
""name"": ""Client"",
""accessModifier"": ""public"",
""dependencyInjection"": true,
""strictSchemaValidation"": true,
""hashAlgorithm"": ""md5"",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"Name": "Client",
"Namespace": null,
"Url": null,
"AccessModifier": "public",
"DependencyInjection": true,
"StrictSchemaValidation": true,
"HashAlgorithm": "md5",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"Name": "Client",
"Namespace": null,
"Url": null,
"AccessModifier": "public",
"DependencyInjection": true,
"StrictSchemaValidation": true,
"HashAlgorithm": "md5",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"Name": "Client",
"Namespace": null,
"Url": null,
"AccessModifier": "public",
"DependencyInjection": true,
"StrictSchemaValidation": true,
"HashAlgorithm": "md5",
Expand Down
2 changes: 2 additions & 0 deletions website/src/docs/strawberryshake/v13/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 9abd8b1

Please sign in to comment.