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

RewriteType does not work with multiple rewrites #2607

Merged
merged 6 commits into from
Nov 21, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,8 @@ internal static class ContextDataExtensions
if (current is IDictionary<(NameString, NameString), NameString> dict)
{
dict[(newTypeName, schemaName)] = originalTypeName;

return dict;
}

return new Dictionary<(NameString, NameString), NameString>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
using System.Collections.Generic;
using HotChocolate.Configuration;
using HotChocolate.Types;
using HotChocolate.Types.Descriptors;
using HotChocolate.Types.Descriptors.Definitions;
using Xunit;

namespace HotChocolate.Stitching.Configuration
{
public class ContextDataExtensionsTest
{
[Fact]
public void AddNameLookup_Single()
{
// arrange
ISchemaBuilder schemaBuilder = SchemaBuilder.New().AddQueryType<CustomQueryType>();

// act
schemaBuilder.AddNameLookup("OriginalType1", "NewType1", "Schema1");
schemaBuilder.AddNameLookup("OriginalType2", "NewType2", "Schema2");

// assert
IReadOnlyDictionary<(NameString, NameString), NameString> lookup =
schemaBuilder
.Create()
.GetType<CustomQueryType>(nameof(CustomQueryType))
.Context
.GetNameLookup();
Assert.Equal("OriginalType1", lookup[("NewType1", "Schema1")]);
Assert.Equal("OriginalType2", lookup[("NewType2", "Schema2")]);
}

[Fact]
public void AddNameLookup_Multiple()
{
// arrange
ISchemaBuilder schemaBuilder = SchemaBuilder.New().AddQueryType<CustomQueryType>();
var dict = new Dictionary<(NameString, NameString), NameString>
{
{ ("NewType1", "Schema1"), "OriginalType1" },
{ ("NewType2", "Schema2"), "OriginalType2" }
};

// act
schemaBuilder.AddNameLookup(dict);

// assert
IReadOnlyDictionary<(NameString, NameString), NameString> lookup =
schemaBuilder
.Create()
.GetType<CustomQueryType>(nameof(CustomQueryType))
.Context
.GetNameLookup();
Assert.Equal("OriginalType1", lookup[("NewType1", "Schema1")]);
Assert.Equal("OriginalType2", lookup[("NewType2", "Schema2")]);
}

public class CustomQueryType : ObjectType
{
public IDescriptorContext Context { get; set; }

protected override void Configure(IObjectTypeDescriptor descriptor)
{
descriptor.Field("foo").Resolve("bar");
}

protected override ObjectTypeDefinition CreateDefinition(ITypeDiscoveryContext context)
{
Context = context.DescriptorContext;

return base.CreateDefinition(context);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using HotChocolate.Configuration;
using HotChocolate.Execution;
using HotChocolate.Execution.Configuration;
using HotChocolate.Types;
using HotChocolate.Types.Descriptors;
using HotChocolate.Types.Descriptors.Definitions;
using Microsoft.Extensions.DependencyInjection;
using Xunit;

namespace HotChocolate.Stitching.Configuration
{
public class HotChocolateStitchingRequestExecutorExtensionsTests
{
[Fact]
public async Task RewriteType()
{
// arrange
IRequestExecutorBuilder executorBuilder =
new ServiceCollection().AddGraphQL().AddQueryType<CustomQueryType>();

// act
executorBuilder.RewriteType("OriginalType1", "NewType1", "Schema1");
executorBuilder.RewriteType("OriginalType2", "NewType2", "Schema2");

// assert
ISchema schema = await executorBuilder.BuildSchemaAsync();
IReadOnlyDictionary<(NameString, NameString), NameString> lookup =
schema
.GetType<CustomQueryType>(nameof(CustomQueryType))
.Context
.GetNameLookup();
Assert.Equal("OriginalType1", lookup[("NewType1", "Schema1")]);
Assert.Equal("OriginalType2", lookup[("NewType2", "Schema2")]);
}
}

public class CustomQueryType : ObjectType
{
public IDescriptorContext Context { get; set; }

protected override void Configure(IObjectTypeDescriptor descriptor)
{
descriptor.Field("foo").Resolve("bar");
}

protected override ObjectTypeDefinition CreateDefinition(ITypeDiscoveryContext context)
{
Context = context.DescriptorContext;

return base.CreateDefinition(context);
}
}
}