Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed internal field generation in Strawberry Shake #3542

Merged
merged 3 commits into from
Apr 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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