Skip to content
Draft
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 @@ -51,12 +51,13 @@ private void AddUpdateEntityMethod(
}
else if (namedTypeDescriptor is ObjectTypeDescriptor objectTypeDescriptor)
{
BuildTryGetEntityIf(
methodBuilder.AddCode(
BuildTryGetEntityIf(
CreateEntityType(
objectTypeDescriptor.Name,
objectTypeDescriptor.RuntimeType.NamespaceWithoutGlobal))
.AddCode(CreateEntityConstructorCall(objectTypeDescriptor, false))
.AddElse(CreateEntityConstructorCall(objectTypeDescriptor, true));
objectTypeDescriptor.Name,
objectTypeDescriptor.RuntimeType.NamespaceWithoutGlobal))
.AddCode(CreateEntityConstructorCall(objectTypeDescriptor, false))
.AddElse(CreateEntityConstructorCall(objectTypeDescriptor, true)));

methodBuilder.AddEmptyLine();
methodBuilder.AddCode($"return {EntityId};");
Expand Down Expand Up @@ -109,21 +110,75 @@ private static ICode CreateEntityConstructorCall(
}
}

var newEntity = MethodCallBuilder
.Inline()
.SetNew()
.SetMethodName(objectType.EntityTypeDescriptor.RuntimeType.ToString());
var codeBlock = GenerateArgumentsFromResponse(objectType, propertyLookup, fragments);
if (assignDefault)
{
// Merge: Check whether the same entity was already stored while evaluating an argument
// An entity may reference itself but with a different selection set.
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 +197,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
Expand Up @@ -1088,11 +1088,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 @@ -1111,11 +1120,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
Original file line number Diff line number Diff line change
Expand Up @@ -2432,11 +2432,22 @@ public GetHeroBuilder(global::StrawberryShake.IEntityStore entityStore, global::
{
if (session.CurrentSnapshot.TryGetEntity(entityId, out global::StrawberryShake.CodeGeneration.CSharp.Integration.MultiProfile.State.DroidEntity? entity))
{
session.SetEntity(entityId, new global::StrawberryShake.CodeGeneration.CSharp.Integration.MultiProfile.State.DroidEntity(Deserialize_NonNullableString(global::StrawberryShake.Json.JsonElementExtensions.GetPropertyOrNull(obj, "name")), Deserialize_IGetHero_Hero_Friends(session, global::StrawberryShake.Json.JsonElementExtensions.GetPropertyOrNull(obj, "friends"), entityIds)));
var arg0 = Deserialize_NonNullableString(global::StrawberryShake.Json.JsonElementExtensions.GetPropertyOrNull(obj, "name"));
var arg1 = Deserialize_IGetHero_Hero_Friends(session, global::StrawberryShake.Json.JsonElementExtensions.GetPropertyOrNull(obj, "friends"), entityIds);
session.SetEntity(entityId, new global::StrawberryShake.CodeGeneration.CSharp.Integration.MultiProfile.State.DroidEntity(arg0, arg1));
}
else
{
session.SetEntity(entityId, new global::StrawberryShake.CodeGeneration.CSharp.Integration.MultiProfile.State.DroidEntity(Deserialize_NonNullableString(global::StrawberryShake.Json.JsonElementExtensions.GetPropertyOrNull(obj, "name")), Deserialize_IGetHero_Hero_Friends(session, global::StrawberryShake.Json.JsonElementExtensions.GetPropertyOrNull(obj, "friends"), entityIds)));
var arg0 = Deserialize_NonNullableString(global::StrawberryShake.Json.JsonElementExtensions.GetPropertyOrNull(obj, "name"));
var arg1 = Deserialize_IGetHero_Hero_Friends(session, global::StrawberryShake.Json.JsonElementExtensions.GetPropertyOrNull(obj, "friends"), entityIds);
if (session.CurrentSnapshot.TryGetEntity(entityId, out entity))
{
session.SetEntity(entityId, new global::StrawberryShake.CodeGeneration.CSharp.Integration.MultiProfile.State.DroidEntity(arg0, arg1));
}
else
{
session.SetEntity(entityId, new global::StrawberryShake.CodeGeneration.CSharp.Integration.MultiProfile.State.DroidEntity(arg0, arg1));
}
}

return entityId;
Expand All @@ -2446,11 +2457,22 @@ public GetHeroBuilder(global::StrawberryShake.IEntityStore entityStore, global::
{
if (session.CurrentSnapshot.TryGetEntity(entityId, out global::StrawberryShake.CodeGeneration.CSharp.Integration.MultiProfile.State.HumanEntity? entity))
{
session.SetEntity(entityId, new global::StrawberryShake.CodeGeneration.CSharp.Integration.MultiProfile.State.HumanEntity(Deserialize_NonNullableString(global::StrawberryShake.Json.JsonElementExtensions.GetPropertyOrNull(obj, "name")), Deserialize_IGetHero_Hero_Friends(session, global::StrawberryShake.Json.JsonElementExtensions.GetPropertyOrNull(obj, "friends"), entityIds)));
var arg0 = Deserialize_NonNullableString(global::StrawberryShake.Json.JsonElementExtensions.GetPropertyOrNull(obj, "name"));
var arg1 = Deserialize_IGetHero_Hero_Friends(session, global::StrawberryShake.Json.JsonElementExtensions.GetPropertyOrNull(obj, "friends"), entityIds);
session.SetEntity(entityId, new global::StrawberryShake.CodeGeneration.CSharp.Integration.MultiProfile.State.HumanEntity(arg0, arg1));
}
else
{
session.SetEntity(entityId, new global::StrawberryShake.CodeGeneration.CSharp.Integration.MultiProfile.State.HumanEntity(Deserialize_NonNullableString(global::StrawberryShake.Json.JsonElementExtensions.GetPropertyOrNull(obj, "name")), Deserialize_IGetHero_Hero_Friends(session, global::StrawberryShake.Json.JsonElementExtensions.GetPropertyOrNull(obj, "friends"), entityIds)));
var arg0 = Deserialize_NonNullableString(global::StrawberryShake.Json.JsonElementExtensions.GetPropertyOrNull(obj, "name"));
var arg1 = Deserialize_IGetHero_Hero_Friends(session, global::StrawberryShake.Json.JsonElementExtensions.GetPropertyOrNull(obj, "friends"), entityIds);
if (session.CurrentSnapshot.TryGetEntity(entityId, out entity))
{
session.SetEntity(entityId, new global::StrawberryShake.CodeGeneration.CSharp.Integration.MultiProfile.State.HumanEntity(arg0, arg1));
}
else
{
session.SetEntity(entityId, new global::StrawberryShake.CodeGeneration.CSharp.Integration.MultiProfile.State.HumanEntity(arg0, arg1));
}
}

return entityId;
Expand Down Expand Up @@ -2534,11 +2556,20 @@ public GetHeroBuilder(global::StrawberryShake.IEntityStore entityStore, global::
{
if (session.CurrentSnapshot.TryGetEntity(entityId, out global::StrawberryShake.CodeGeneration.CSharp.Integration.MultiProfile.State.DroidEntity? entity))
{
session.SetEntity(entityId, new global::StrawberryShake.CodeGeneration.CSharp.Integration.MultiProfile.State.DroidEntity(Deserialize_NonNullableString(global::StrawberryShake.Json.JsonElementExtensions.GetPropertyOrNull(obj, "name")), entity.Friends));
var arg0 = Deserialize_NonNullableString(global::StrawberryShake.Json.JsonElementExtensions.GetPropertyOrNull(obj, "name"));
session.SetEntity(entityId, new global::StrawberryShake.CodeGeneration.CSharp.Integration.MultiProfile.State.DroidEntity(arg0, entity.Friends));
}
else
{
session.SetEntity(entityId, new global::StrawberryShake.CodeGeneration.CSharp.Integration.MultiProfile.State.DroidEntity(Deserialize_NonNullableString(global::StrawberryShake.Json.JsonElementExtensions.GetPropertyOrNull(obj, "name")), default !));
var arg0 = Deserialize_NonNullableString(global::StrawberryShake.Json.JsonElementExtensions.GetPropertyOrNull(obj, "name"));
if (session.CurrentSnapshot.TryGetEntity(entityId, out entity))
{
session.SetEntity(entityId, new global::StrawberryShake.CodeGeneration.CSharp.Integration.MultiProfile.State.DroidEntity(arg0, entity.Friends));
}
else
{
session.SetEntity(entityId, new global::StrawberryShake.CodeGeneration.CSharp.Integration.MultiProfile.State.DroidEntity(arg0, default !));
}
}

return entityId;
Expand All @@ -2548,11 +2579,20 @@ public GetHeroBuilder(global::StrawberryShake.IEntityStore entityStore, global::
{
if (session.CurrentSnapshot.TryGetEntity(entityId, out global::StrawberryShake.CodeGeneration.CSharp.Integration.MultiProfile.State.HumanEntity? entity))
{
session.SetEntity(entityId, new global::StrawberryShake.CodeGeneration.CSharp.Integration.MultiProfile.State.HumanEntity(Deserialize_NonNullableString(global::StrawberryShake.Json.JsonElementExtensions.GetPropertyOrNull(obj, "name")), entity.Friends));
var arg0 = Deserialize_NonNullableString(global::StrawberryShake.Json.JsonElementExtensions.GetPropertyOrNull(obj, "name"));
session.SetEntity(entityId, new global::StrawberryShake.CodeGeneration.CSharp.Integration.MultiProfile.State.HumanEntity(arg0, entity.Friends));
}
else
{
session.SetEntity(entityId, new global::StrawberryShake.CodeGeneration.CSharp.Integration.MultiProfile.State.HumanEntity(Deserialize_NonNullableString(global::StrawberryShake.Json.JsonElementExtensions.GetPropertyOrNull(obj, "name")), default !));
var arg0 = Deserialize_NonNullableString(global::StrawberryShake.Json.JsonElementExtensions.GetPropertyOrNull(obj, "name"));
if (session.CurrentSnapshot.TryGetEntity(entityId, out entity))
{
session.SetEntity(entityId, new global::StrawberryShake.CodeGeneration.CSharp.Integration.MultiProfile.State.HumanEntity(arg0, entity.Friends));
}
else
{
session.SetEntity(entityId, new global::StrawberryShake.CodeGeneration.CSharp.Integration.MultiProfile.State.HumanEntity(arg0, default !));
}
}

return entityId;
Expand Down
Loading