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 9, 2023
1 parent 388f5c9 commit 3a47dde
Show file tree
Hide file tree
Showing 18 changed files with 945 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 = settings.AccessModifier,
StrictSchemaValidation = settings.StrictValidation,
RequestStrategy = settings.RequestStrategy,
TransportProfiles = settings.Profiles,
Expand Down Expand Up @@ -197,6 +199,7 @@ public static void AssertStarWarsResult(
public static AssertSettings CreateIntegrationTest(
RequestStrategyGen requestStrategy = RequestStrategyGen.Default,
TransportProfile[]? profiles = null,
AccessModifier accessModifier = AccessModifier.Public,
bool noStore = false,
[CallerMemberName] string? testName = null)
{
Expand All @@ -219,6 +222,7 @@ public static AssertSettings CreateIntegrationTest(
{
ClientName = testName! + "Client",
Namespace = ns,
AccessModifier = accessModifier,
StrictValidation = true,
SnapshotFile = System.IO.Path.Combine(
snapshotFullName.FolderPath,
Expand Down Expand Up @@ -262,6 +266,9 @@ public class AssertSettings

public string? Namespace { get; set; }

public AccessModifier AccessModifier { get; set; }
= AccessModifier.Public;

public bool StrictValidation { get; set; }

public string? SnapshotFile { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,22 @@ public void Generate_StarWarsIntegrationTest()
FileResource.Open("QueryWithSubscription.graphql"));
}

[Fact]
public void Generate_Client_With_Internal_Access_Modifier()
{
AssertStarWarsResult(
new AssertSettings {
StrictValidation = true,
AccessModifier = AccessModifier.Internal
},
@"query GetHero {
hero(episode: NEW_HOPE) {
name
appearsIn
}
}");
}

[Fact]
public void StarWarsTypeNameOnUnions() =>
AssertStarWarsResult(
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
Loading

0 comments on commit 3a47dde

Please sign in to comment.