Skip to content
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 @@ -132,7 +132,26 @@ private static bool TryGetValue<T>(
case SyntaxKind.IntValue:
case SyntaxKind.FloatValue:
case SyntaxKind.BooleanValue:
if (type.NamedType() is not ScalarType scalarType)
var namedType = type.NamedType();

if (namedType is EnumType stringEnumType
&& valueNode is StringValueNode stringValue
&& stringEnumType.TryGetRuntimeValue(stringValue.Value, out var enumValue))
{
if (enumValue is T castedEnum)
{
value = castedEnum;
return true;
}

if (DefaultTypeConverter.Default.TryConvert(typeof(T), enumValue, out var convertedEnum))
{
value = (T)convertedEnum;
return true;
}
}

if (namedType is not ScalarType scalarType)
{
break;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
using HotChocolate.ApolloFederation.Resolvers;
using HotChocolate.ApolloFederation.Types;
using HotChocolate.Execution;
using HotChocolate.Language;
using Microsoft.Extensions.DependencyInjection;
using static HotChocolate.ApolloFederation.TestHelper;

namespace HotChocolate.ApolloFederation;

public class Issue8756ReproTests
{
[Fact]
public async Task Enum_Value_Is_Parsed_In_Reference_Resolver_When_Representation_Uses_String()
{
var schema = await new ServiceCollection()
.AddGraphQL()
.AddApolloFederation()
.AddQueryType<Issue8756Query>()
.BuildSchemaAsync();

var context = CreateResolverContext(schema);

var representations = new List<Representation>
{
new(
nameof(Issue8756Entity),
new ObjectValueNode(
new ObjectFieldNode("id", "1"),
new ObjectFieldNode("enumValue", "B")))
};

var result = await EntitiesResolver.ResolveAsync(schema, representations, context);

var entity = Assert.IsType<Issue8756Entity>(result[0]);
Assert.Equal(Issue8756Enum.B, entity.EnumValue);
}

public class Issue8756Query
{
public Issue8756Entity Issue8756Entity { get; set; } = null!;
}

[ReferenceResolver(EntityResolver = nameof(Resolve))]
public class Issue8756Entity
{
public string Id { get; set; } = null!;

public Issue8756Enum EnumValue { get; set; }

public static Issue8756Entity Resolve(string id, Issue8756Enum enumValue)
=> new()
{
Id = id,
EnumValue = enumValue
};
}

public enum Issue8756Enum
{
A,
B
}
}
Loading