Skip to content
Open
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 @@ -71,9 +71,12 @@ public MethodCallBuilder AddArgument(string value)

public MethodCallBuilder AddOutArgument(
string value,
string typeReference)
string? typeReference)
{
_arguments.Add(CodeInlineBuilder.New().SetText($"out {typeReference}? {value}"));
var code = typeReference is null
? $"out {value}"
: $"out {typeReference}? {value}";
_arguments.Add(CodeInlineBuilder.New().SetText(code));
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,21 +109,78 @@ private static ICode CreateEntityConstructorCall(
}
}

var newEntity = MethodCallBuilder
.Inline()
.SetNew()
.SetMethodName(objectType.EntityTypeDescriptor.RuntimeType.ToString());
var codeBlock = GenerateArgumentsFromResponse(objectType, propertyLookup, fragments);

if (assignDefault)
{
// Evaluating the arguments above may have written the same entity to the store
// (an entity can reference itself with a different selection set). Re-check the
// store so those already deserialized fields are preserved instead of being
// overwritten with defaults.
codeBlock.AddCode(
BuildTryGetEntityIf(null)
.AddCode(CreateSetEntityMethodCall(objectType, false, propertyLookup, fragments))
.AddElse(CreateSetEntityMethodCall(objectType, true, propertyLookup, fragments)));
}
else
{
codeBlock.AddCode(
CreateSetEntityMethodCall(objectType, assignDefault, propertyLookup, fragments));
}

return codeBlock;
}

private static CodeBlockBuilder GenerateArgumentsFromResponse(
ObjectTypeDescriptor objectType,
Dictionary<string, PropertyDescriptor> propertyLookup,
Dictionary<string, DeferredFragmentDescriptor> fragments)
{
var codeBlockBuilder = CodeBlockBuilder.New();
var argumentIndex = 0;
foreach (var property in
objectType.EntityTypeDescriptor.Properties.Values)
{
if (propertyLookup.TryGetValue(property.Name, out var prop))
{
newEntity.AddArgument(BuildUpdateMethodCall(prop));
codeBlockBuilder.AddCode(
AssignmentBuilder
.New()
.SetLeftHandSide($"var arg{argumentIndex++}")
.SetRightHandSide(BuildUpdateMethodCall(prop)));
}
else if (fragments.TryGetValue(property.Name, out var frag))
{
newEntity.AddArgument(BuildFragmentMethodCall(frag));
codeBlockBuilder.AddCode(
AssignmentBuilder
.New()
.SetLeftHandSide($"var arg{argumentIndex++}")
.SetRightHandSide(BuildFragmentMethodCall(frag)));
}
}

return codeBlockBuilder;
}

private static MethodCallBuilder CreateSetEntityMethodCall(
ObjectTypeDescriptor objectType,
bool assignDefault,
Dictionary<string, PropertyDescriptor> propertyLookup,
Dictionary<string, DeferredFragmentDescriptor> fragments)
{
var newEntity = MethodCallBuilder
.Inline()
.SetNew()
.SetMethodName(objectType.EntityTypeDescriptor.RuntimeType.ToString());

var argumentIndex = 0;
foreach (var property in
objectType.EntityTypeDescriptor.Properties.Values)
{
if (propertyLookup.ContainsKey(property.Name)
|| fragments.ContainsKey(property.Name))
{
newEntity.AddArgument($"arg{argumentIndex++}");
}
else if (assignDefault)
{
Expand All @@ -142,15 +199,15 @@ private static ICode CreateEntityConstructorCall(
.AddArgument(newEntity);
}

private static IfBuilder BuildTryGetEntityIf(RuntimeTypeInfo entityType)
private static IfBuilder BuildTryGetEntityIf(RuntimeTypeInfo? entityType)
{
return IfBuilder
.New()
.SetCondition(MethodCallBuilder
.Inline()
.SetMethodName(Session, "CurrentSnapshot", "TryGetEntity")
.AddArgument(EntityId)
.AddOutArgument(Entity, entityType.ToString()));
.AddOutArgument(Entity, entityType?.ToString()));
}

private static PropertyDescriptor EnsureDeferredFieldIsNullable(PropertyDescriptor property)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// ReSharper disable ArrangeObjectCreationWhenTypeEvident
// ReSharper disable ArrangeObjectCreationWhenTypeEvident
// ReSharper disable BuiltInTypeReferenceStyle
// ReSharper disable ConvertToAutoProperty
// ReSharper disable InconsistentNaming
Expand Down Expand Up @@ -944,11 +944,20 @@ public GetFooBuilder(global::StrawberryShake.IEntityStore entityStore, global::S
{
if (session.CurrentSnapshot.TryGetEntity(entityId, out global::StrawberryShake.CodeGeneration.CSharp.Integration.EntityIdOrData.State.BazEntity? entity))
{
session.SetEntity(entityId, new global::StrawberryShake.CodeGeneration.CSharp.Integration.EntityIdOrData.State.BazEntity(Deserialize_String(global::StrawberryShake.Json.JsonElementExtensions.GetPropertyOrNull(obj, "id"))));
var arg0 = Deserialize_String(global::StrawberryShake.Json.JsonElementExtensions.GetPropertyOrNull(obj, "id"));
session.SetEntity(entityId, new global::StrawberryShake.CodeGeneration.CSharp.Integration.EntityIdOrData.State.BazEntity(arg0));
}
else
{
session.SetEntity(entityId, new global::StrawberryShake.CodeGeneration.CSharp.Integration.EntityIdOrData.State.BazEntity(Deserialize_String(global::StrawberryShake.Json.JsonElementExtensions.GetPropertyOrNull(obj, "id"))));
var arg0 = Deserialize_String(global::StrawberryShake.Json.JsonElementExtensions.GetPropertyOrNull(obj, "id"));
if (session.CurrentSnapshot.TryGetEntity(entityId, out entity))
{
session.SetEntity(entityId, new global::StrawberryShake.CodeGeneration.CSharp.Integration.EntityIdOrData.State.BazEntity(arg0));
}
else
{
session.SetEntity(entityId, new global::StrawberryShake.CodeGeneration.CSharp.Integration.EntityIdOrData.State.BazEntity(arg0));
}
}

return new global::StrawberryShake.EntityIdOrData(entityId);
Expand All @@ -967,11 +976,20 @@ public GetFooBuilder(global::StrawberryShake.IEntityStore entityStore, global::S
{
if (session.CurrentSnapshot.TryGetEntity(entityId, out global::StrawberryShake.CodeGeneration.CSharp.Integration.EntityIdOrData.State.Baz2Entity? entity))
{
session.SetEntity(entityId, new global::StrawberryShake.CodeGeneration.CSharp.Integration.EntityIdOrData.State.Baz2Entity(Deserialize_String(global::StrawberryShake.Json.JsonElementExtensions.GetPropertyOrNull(obj, "id"))));
var arg0 = Deserialize_String(global::StrawberryShake.Json.JsonElementExtensions.GetPropertyOrNull(obj, "id"));
session.SetEntity(entityId, new global::StrawberryShake.CodeGeneration.CSharp.Integration.EntityIdOrData.State.Baz2Entity(arg0));
}
else
{
session.SetEntity(entityId, new global::StrawberryShake.CodeGeneration.CSharp.Integration.EntityIdOrData.State.Baz2Entity(Deserialize_String(global::StrawberryShake.Json.JsonElementExtensions.GetPropertyOrNull(obj, "id"))));
var arg0 = Deserialize_String(global::StrawberryShake.Json.JsonElementExtensions.GetPropertyOrNull(obj, "id"));
if (session.CurrentSnapshot.TryGetEntity(entityId, out entity))
{
session.SetEntity(entityId, new global::StrawberryShake.CodeGeneration.CSharp.Integration.EntityIdOrData.State.Baz2Entity(arg0));
}
else
{
session.SetEntity(entityId, new global::StrawberryShake.CodeGeneration.CSharp.Integration.EntityIdOrData.State.Baz2Entity(arg0));
}
}

return new global::StrawberryShake.EntityIdOrData(entityId);
Expand Down
Loading
Loading