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

Issue 6594 nested resolver key #6596

Merged
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 @@ -31,6 +31,7 @@ private static bool TryGetValue<T>(
int i,
out T? value)
{
type = type is NonNullType nonNullType ? nonNullType.Type : type;
switch (valueNode.Kind)
{
case SyntaxKind.ObjectValue:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,66 @@ public async void TestResolveViaEntityResolver_NoResolverFound()
await Assert.ThrowsAsync<SchemaException>(ShouldThrow);
}

[Fact]
public async Task TestDetailFieldResolver_Required()
{
var schema = SchemaBuilder.New()
.AddApolloFederation()
.AddQueryType<Query>()
.AddType<FederatedTypeWithRequiredDetail>()
.Create();

var context = CreateResolverContext(schema);

var representations = new List<Representation>
{
new("FederatedTypeWithRequiredDetail",
new ObjectValueNode(new[]
{
new ObjectFieldNode("detail",
new ObjectValueNode(new[] { new ObjectFieldNode("id", "testId") }))
}))
};

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

Assert.Equal(1, result.Count);
var obj = Assert.IsType<FederatedTypeWithRequiredDetail>(result[0]);

Assert.Equal("testId", obj.Id);
Assert.Equal("testId", obj.Detail.Id);
}

[Fact]
public async Task TestDetailFieldResolver_Optional()
{
var schema = SchemaBuilder.New()
.AddApolloFederation()
.AddQueryType<Query>()
.AddType<FederatedTypeWithOptionalDetail>()
.Create();

var context = CreateResolverContext(schema);

var representations = new List<Representation>
{
new("FederatedTypeWithOptionalDetail",
new ObjectValueNode(new[]
{
new ObjectFieldNode("detail",
new ObjectValueNode(new[] { new ObjectFieldNode("id", "testId") }))
}))
};

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

Assert.Equal(1, result.Count);
var obj = Assert.IsType<FederatedTypeWithOptionalDetail>(result[0]);

Assert.Equal("testId", obj.Id);
Assert.Equal("testId", obj.Detail!.Id);
}

public class Query
{
public ForeignType ForeignType { get; set; } = default!;
Expand Down Expand Up @@ -291,4 +351,30 @@ protected override Task<IReadOnlyDictionary<string, FederatedType>> LoadBatchAsy
return Task.FromResult<IReadOnlyDictionary<string, FederatedType>>(result);
}
}

public class FederatedTypeWithRequiredDetail
{
public string Id { get; set; } = default!;

public FederatedTypeDetail Detail { get; set; } = default!;

[ReferenceResolver]
public static FederatedTypeWithRequiredDetail ReferenceResolver([Map("detail.id")] string detailId) => new() { Id = detailId, Detail = new FederatedTypeDetail { Id = detailId } };
}

public class FederatedTypeWithOptionalDetail
{
public string Id { get; set; } = default!;

public FederatedTypeDetail? Detail { get; set; } = default!;

[ReferenceResolver]
public static FederatedTypeWithOptionalDetail ReferenceResolver([Map("detail.id")] string detailId) => new() { Id = detailId, Detail = new FederatedTypeDetail { Id = detailId } };

}

public class FederatedTypeDetail
{
public string Id { get; set; } = default!;
}
}
Loading