Skip to content

Commit

Permalink
Fixed internal field generation in Strawberry Shake (#3542)
Browse files Browse the repository at this point in the history
  • Loading branch information
PascalSenn authored Apr 19, 2021
1 parent 93d04a3 commit a217b70
Show file tree
Hide file tree
Showing 10 changed files with 16,485 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ typeDescriptor as ComplexTypeDescriptor ??
constructorBuilder.AddCode(
AssignmentBuilder
.New()
.SetLefthandSide(prop.Name)
.SetLefthandSide(GetLeftPropertyAssignment(prop.Name))
.SetRighthandSide(paramName));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ namespace StrawberryShake.CodeGeneration.CSharp.Generators
{
public class ResultTypeGenerator : CodeGenerator<ObjectTypeDescriptor>
{
protected override void Generate(ObjectTypeDescriptor descriptor,
protected override void Generate(
ObjectTypeDescriptor descriptor,
CSharpSyntaxGeneratorSettings settings,
CodeWriter writer,
out string fileName,
Expand Down Expand Up @@ -47,11 +48,7 @@ protected override void Generate(ObjectTypeDescriptor descriptor,
.AddParameter(paramName, x => x.SetType(propTypeBuilder))
.AddCode(AssignmentBuilder
.New()
.SetLefthandSide(
(prop.Name.Value is WellKnownNames.TypeName
? "this."
: string.Empty) +
prop.Name)
.SetLefthandSide(GetLeftPropertyAssignment(prop.Name))
.SetRighthandSide(paramName));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,14 +101,25 @@ public static string GetFieldName(string fieldName)
return "_" + GetParamNameUnsafe(fieldName);
}

public static string GetLeftPropertyAssignment(string property)
{
if (property is { Length: >0 } && property[0] == '_')
{
return $"this.{property}";

}

return property;
}

public static string GetParameterName(string parameterName)
{
return Keywords.ToSafeName(GetParamNameUnsafe(parameterName));
}

public static string GetParamNameUnsafe(string parameterName)
{
if (parameterName == WellKnownNames.TypeName)
if (parameterName.Length > 0 && parameterName[0] == '_')
{
return parameterName;
}
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using HotChocolate.AspNetCore.Utilities;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Snapshooter.Xunit;
using StrawberryShake.Transport.WebSockets;
using Xunit;

namespace StrawberryShake.CodeGeneration.CSharp.Integration.StarWarsIntrospection
{
public class StarWarsIntrospectionTest : ServerTestBase
{
public StarWarsIntrospectionTest(TestServerFactory serverFactory) : base(serverFactory)
{
}

[Fact]
public async Task Execute_StarWarsIntrospection_Test()
{
// arrange
CancellationToken ct = new CancellationTokenSource(20_000).Token;
using IWebHost host = TestServerHelper.CreateServer(
_ => { },
out var port);
var serviceCollection = new ServiceCollection();
serviceCollection.AddHttpClient(
StarWarsIntrospectionClient.ClientName,
c => c.BaseAddress = new Uri("http://localhost:" + port + "/graphql"));
serviceCollection.AddWebSocketClient(
StarWarsIntrospectionClient.ClientName,
c => c.Uri = new Uri("ws://localhost:" + port + "/graphql"));
serviceCollection.AddStarWarsIntrospectionClient();
IServiceProvider services = serviceCollection.BuildServiceProvider();
StarWarsIntrospectionClient client = services.GetRequiredService<StarWarsIntrospectionClient>();

// act
IOperationResult<IIntrospectionQueryResult> result =
await client.IntrospectionQuery.ExecuteAsync(ct);


// assert
result.MatchSnapshot();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using ChilliCream.Testing;
using StrawberryShake.Tools.Configuration;
using Xunit;
using static StrawberryShake.CodeGeneration.CSharp.GeneratorTestHelper;
Expand Down Expand Up @@ -169,5 +170,11 @@ type Quox2 {
union Bar = Baz | Quox | Baz2 | Quox2
",
"extend schema @key(fields: \"id\")");

[Fact]
public void StarWarsIntrospection() =>
AssertStarWarsResult(
CreateIntegrationTest(),
FileResource.Open("IntrospectionQuery.graphql"));
}
}
Loading

0 comments on commit a217b70

Please sign in to comment.