Skip to content

Commit

Permalink
Fixed enum discovery in StrawBerry Shake (#3552)
Browse files Browse the repository at this point in the history
  • Loading branch information
PascalSenn authored Apr 19, 2021
1 parent a217b70 commit cb60fbd
Show file tree
Hide file tree
Showing 12 changed files with 1,402 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,12 @@ private void AddEntityOrUnionDataHandler(

IfBuilder ifBuilder = IfBuilder
.New()
.SetCondition($"{parameterName}.EntityId is {{ }}")
.SetCondition($"{parameterName}.EntityId is {{ }} id")
.AddCode(MethodCallBuilder
.New()
.SetReturn()
.SetMethodName(entityDataHandlerMethodName)
.AddArgument($"{parameterName}.EntityId")
.AddArgument("id")
.AddArgument(_snapshot))
.AddIfElse(IfBuilder
.New()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,5 +146,5 @@ private static void EnqueueFields(
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ public void Analyze(DocumentNode document)
}

protected override void VisitOperationDefinition(
OperationDefinitionNode node, object? context)
OperationDefinitionNode node,
object? context)
{
ObjectType operationType = _schema.GetOperationType(node.Operation);

Expand All @@ -41,8 +42,8 @@ protected override void VisitVariableDefinition(
VariableDefinitionNode node,
object? context)
{
if (_schema.TryGetType(node.Type.NamedType().Name.Value, out INamedType type)
&& type is IInputType inputType)
if (_schema.TryGetType(node.Type.NamedType().Name.Value, out INamedType type) &&
type is IInputType inputType)
{
VisitInputType(inputType);
}
Expand All @@ -52,8 +53,8 @@ protected override void VisitField(FieldNode node, object? context)
{
IType currentType = _typeContext.Peek();

if (currentType is IComplexOutputType complexType
&& complexType.Fields.TryGetField(node.Name.Value, out IOutputField? field))
if (currentType is IComplexOutputType complexType &&
complexType.Fields.TryGetField(node.Name.Value, out IOutputField? field))
{
INamedType fieldType = field.Type.NamedType();
if (fieldType is IInputType inputType)
Expand All @@ -75,8 +76,7 @@ protected override void VisitFragmentDefinition(
FragmentDefinitionNode node,
object? context)
{
INamedType type = _schema!.GetType<INamedType>(
node.TypeCondition.Name.Value);
INamedType type = _schema!.GetType<INamedType>(node.TypeCondition.Name.Value);

_typeContext.Push(type);

Expand Down Expand Up @@ -117,5 +117,23 @@ private void VisitInputObjectType(InputObjectType type)
VisitInputType(field.Type);
}
}

protected override void VisitInlineFragment(
InlineFragmentNode node,
object? context)
{
if (node.TypeCondition != null)
{
INamedType type = _schema!.GetType<INamedType>(node.TypeCondition.Name.Value);
_typeContext.Push(type);
}

base.VisitInlineFragment(node, context);

if (node.TypeCondition != null)
{
_typeContext.Pop();
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using ChilliCream.Testing;
using Xunit;
using static StrawberryShake.CodeGeneration.CSharp.GeneratorTestHelper;

Expand Down Expand Up @@ -101,6 +102,63 @@ type Quox2 {
"extend schema @key(fields: \"id\")");
}

[Fact]
public void UnionWithNestedObject()
{
AssertResult(
@"
mutation StoreUserSettingFor(
$userId: Int!,
$customerId: Int!,
$input: StoreUserSettingForInput!) {
storeUserSettingFor(userId: $userId, customerId: $customerId, input: $input) {
... on UserSettingSuccess {id}
... on UserSettingError {errors {code message}}
}
}
",
@"
type Query {
foo: String
}
type Mutation {
storeUserSettingFor(
userId: Int!
customerId: Int!
input: StoreUserSettingForInput!): UserSettingResult!
}
union UserSettingResult = UserSettingSuccess | UserSettingError
input StoreUserSettingForInput {
portal: String
mobile: String
}
type UserSettingSuccess {
id: Int!
}
type UserSettingError {
errors: [ErrorNode!]!
}
type ErrorNode {
code: ErrorCode
message: String
}
enum ErrorCode {
UNKNOWN
MISSING_ARGUMENT
INVALID_ARGUMENT
FAILED
}
",
"extend schema @key(fields: \"id\")");
}

[Fact]
public void UnionListInEntity()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -791,9 +791,9 @@ public GetFooResult Create(global::StrawberryShake.IOperationResultDataInfo data
{
}

)
id)
{
return MapIGetFoo_FooData(data.Value.EntityId, snapshot);
return MapIGetFoo_FooData(id, snapshot);
}
else if (data.Value.Data is global::StrawberryShake.CodeGeneration.CSharp.Integration.EntityIdOrData.State.IBarData d)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -747,9 +747,9 @@ namespace Foo.Bar.State
{
}

)
id)
{
return MapIGetFoo_FooData(data.Value.EntityId, snapshot);
return MapIGetFoo_FooData(id, snapshot);
}
else if (data.Value.Data is global::Foo.Bar.State.IBarData d)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -769,9 +769,9 @@ namespace Foo.Bar.State
{
}

)
id)
{
return MapIGetFoo_FooData(data.Value.EntityId, snapshot);
return MapIGetFoo_FooData(id, snapshot);
}
else if (data.Value.Data is global::Foo.Bar.State.IBarData d)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -690,9 +690,9 @@ namespace Foo.Bar.State
{
}

)
id)
{
return MapIGetFoo_FooData(data.Value.EntityId, snapshot);
return MapIGetFoo_FooData(id, snapshot);
}
else if (data.Value.Data is global::Foo.Bar.State.IBarData d)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -684,9 +684,9 @@ namespace Foo.Bar.State
{
}

)
id)
{
return MapIGetFoo_FooData(data.Value.EntityId, snapshot);
return MapIGetFoo_FooData(id, snapshot);
}
else if (data.Value.Data is global::Foo.Bar.State.IBarData d)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -706,9 +706,9 @@ namespace Foo.Bar.State
{
}

)
id)
{
return MapIGetFoo_FooData(data.Value.EntityId, snapshot);
return MapIGetFoo_FooData(id, snapshot);
}
else if (data.Value.Data is global::Foo.Bar.State.IBarData d)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -893,9 +893,9 @@ namespace Foo.Bar.State
{
}

)
id)
{
return MapIGetFoo_Test_FooData(data.Value.EntityId, snapshot);
return MapIGetFoo_Test_FooData(id, snapshot);
}
else if (data.Value.Data is global::Foo.Bar.State.IBarData d)
{
Expand Down
Loading

0 comments on commit cb60fbd

Please sign in to comment.