-
-
Notifications
You must be signed in to change notification settings - Fork 748
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed Apollo Federation v1 schema output (#7349)
- Loading branch information
1 parent
26344d1
commit c16f3d5
Showing
21 changed files
with
805 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
...late/ApolloFederation/src/ApolloFederation/Types/V1/FieldSetTypeLegacySupportAttribute.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
using HotChocolate.Types.Descriptors; | ||
|
||
namespace HotChocolate.ApolloFederation.Types; | ||
|
||
internal sealed class FieldSetTypeLegacySupportAttribute : ScalarTypeDescriptorAttribute | ||
{ | ||
protected override void OnConfigure( | ||
IDescriptorContext context, | ||
IScalarTypeDescriptor descriptor, | ||
Type type) | ||
{ | ||
// federation v1 uses a different name for the scalar. | ||
if (descriptor.GetFederationVersion() == FederationVersion.Federation10) | ||
{ | ||
descriptor.Extend().Definition.Name = FederationTypeNames.LegacyFieldSetType_Name; | ||
} | ||
} | ||
} |
82 changes: 82 additions & 0 deletions
82
.../ApolloFederation/test/ApolloFederation.Tests/Directives/Legacy/ExternalDirectiveTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
using CookieCrumble; | ||
using HotChocolate.ApolloFederation.Types; | ||
using HotChocolate.Execution; | ||
using HotChocolate.Types; | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
namespace HotChocolate.ApolloFederation.Directives.Legacy; | ||
|
||
public class ExternalDirectiveTests : FederationTypesTestBase | ||
{ | ||
[Fact] | ||
public async Task AnnotateExternalToTypeFieldCodeFirst() | ||
{ | ||
// arrange | ||
var schema = await new ServiceCollection() | ||
.AddGraphQL() | ||
.AddApolloFederation(FederationVersion.Federation10) | ||
.AddQueryType(o => o | ||
.Name("Query") | ||
.Field("entity") | ||
.Argument("id", a => a.Type<IntType>()) | ||
.Type("User") | ||
.Resolve(_ => new { Id = 1 }) | ||
) | ||
.AddObjectType( | ||
o => | ||
{ | ||
o.Name("User") | ||
.Key("id"); | ||
o.Field("id") | ||
.Type<IntType>() | ||
.Resolve(_ => 1); | ||
o.Field("idCode") | ||
.Type<StringType>() | ||
.Resolve(_ => default!) | ||
.External(); | ||
}) | ||
.BuildSchemaAsync(); | ||
|
||
// act | ||
var query = schema.GetType<ObjectType>("User"); | ||
|
||
// assert | ||
var directive = Assert.Single(query.Fields["idCode"].Directives); | ||
Assert.Equal(FederationTypeNames.ExternalDirective_Name, directive.Type.Name); | ||
|
||
schema.MatchSnapshot(); | ||
} | ||
|
||
[Fact] | ||
public async Task AnnotateExternalToTypeFieldAnnotationBased() | ||
{ | ||
// arrange | ||
var schema = await new ServiceCollection() | ||
.AddGraphQL() | ||
.AddApolloFederation(FederationVersion.Federation10) | ||
.AddQueryType<Query>() | ||
.BuildSchemaAsync(); | ||
|
||
// act | ||
var query = schema.GetType<ObjectType>("User"); | ||
|
||
// assert | ||
var directive = Assert.Single(query.Fields["idCode"].Directives); | ||
Assert.Equal(FederationTypeNames.ExternalDirective_Name, directive.Type.Name); | ||
|
||
schema.MatchSnapshot(); | ||
} | ||
} | ||
|
||
public class Query | ||
{ | ||
public User GetEntity(int id) => default!; | ||
} | ||
|
||
public class User | ||
{ | ||
[Key] | ||
public int Id { get; set; } | ||
[External] | ||
public string IdCode { get; set; } = default!; | ||
} |
142 changes: 142 additions & 0 deletions
142
...olate/ApolloFederation/test/ApolloFederation.Tests/Directives/Legacy/KeyDirectiveTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
using CookieCrumble; | ||
using HotChocolate.ApolloFederation.Types; | ||
using HotChocolate.Execution; | ||
using HotChocolate.Types; | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
namespace HotChocolate.ApolloFederation.Directives.Legacy; | ||
|
||
public class KeyDirectiveTests : FederationTypesTestBase | ||
{ | ||
[Fact] | ||
public async Task AnnotateKeyToObjectTypeCodeFirst() | ||
{ | ||
// arrange | ||
var schema = await new ServiceCollection() | ||
.AddGraphQL() | ||
.AddApolloFederation(FederationVersion.Federation10) | ||
.AddQueryType(o => o | ||
.Name("Query") | ||
.Field("someField") | ||
.Argument("a", a => a.Type<IntType>()) | ||
.Type("TestType") | ||
.Resolve(_ => new { Id = 1, Name = "bar" }) | ||
) | ||
.AddObjectType( | ||
o => | ||
{ | ||
o.Name("TestType") | ||
.Key("id"); | ||
o.Field("id") | ||
.Type<IntType>() | ||
.Resolve(_ => 1); | ||
o.Field("name") | ||
.Type<StringType>() | ||
.Resolve(_ => "bar"); | ||
}) | ||
.BuildSchemaAsync(); | ||
|
||
// act | ||
var testType = schema.GetType<ObjectType>("TestType"); | ||
|
||
// assert | ||
var keyDirective = Assert.Single(testType.Directives); | ||
Assert.Equal(FederationTypeNames.KeyDirective_Name, keyDirective.Type.Name); | ||
Assert.Equal("fields", keyDirective.AsSyntaxNode().Arguments[0].Name.ToString()); | ||
Assert.Equal("\"id\"", keyDirective.AsSyntaxNode().Arguments[0].Value.ToString()); | ||
|
||
schema.MatchSnapshot(); | ||
} | ||
|
||
[Fact] | ||
public async Task AnnotateKeyToObjectTypeAnnotationBased() | ||
{ | ||
// arrange | ||
var schema = await new ServiceCollection() | ||
.AddGraphQL() | ||
.AddApolloFederation(FederationVersion.Federation10) | ||
.AddQueryType<Query<TestTypeClassDirective>>() | ||
.BuildSchemaAsync(); | ||
|
||
// act | ||
var testType = schema.GetType<ObjectType>("TestTypeClassDirective"); | ||
|
||
// assert | ||
var keyDirective = Assert.Single(testType.Directives); | ||
Assert.Equal(FederationTypeNames.KeyDirective_Name, keyDirective.Type.Name); | ||
Assert.Equal("fields", keyDirective.AsSyntaxNode().Arguments[0].Name.ToString()); | ||
Assert.Equal("\"id\"", keyDirective.AsSyntaxNode().Arguments[0].Value.ToString()); | ||
|
||
schema.MatchSnapshot(); | ||
} | ||
|
||
[Fact] | ||
public async Task AnnotateKeyToClassAttributeAnnotationBased() | ||
{ | ||
// arrange | ||
var schema = await new ServiceCollection() | ||
.AddGraphQL() | ||
.AddApolloFederation(FederationVersion.Federation10) | ||
.AddQueryType<Query<TestTypePropertyDirective>>() | ||
.BuildSchemaAsync(); | ||
|
||
// act | ||
var testType = schema.GetType<ObjectType>("TestTypePropertyDirective"); | ||
|
||
// assert | ||
var keyDirective = Assert.Single(testType.Directives); | ||
Assert.Equal(FederationTypeNames.KeyDirective_Name, keyDirective.Type.Name); | ||
Assert.Equal("fields", keyDirective.AsSyntaxNode().Arguments[0].Name.ToString()); | ||
Assert.Equal("\"id\"", keyDirective.AsSyntaxNode().Arguments[0].Value.ToString()); | ||
|
||
schema.MatchSnapshot(); | ||
} | ||
|
||
[Fact] | ||
public async Task AnnotateKeyToClassAttributesAnnotationBased() | ||
{ | ||
// arrange | ||
var schema = await new ServiceCollection() | ||
.AddGraphQL() | ||
.AddApolloFederation(FederationVersion.Federation10) | ||
.AddQueryType<Query<TestTypePropertyDirectives>>() | ||
.BuildSchemaAsync(); | ||
|
||
// act | ||
var testType = schema.GetType<ObjectType>("TestTypePropertyDirectives"); | ||
|
||
// assert | ||
var keyDirective = Assert.Single(testType.Directives); | ||
Assert.Equal(FederationTypeNames.KeyDirective_Name, keyDirective.Type.Name); | ||
Assert.Equal("fields", keyDirective.AsSyntaxNode().Arguments[0].Name.ToString()); | ||
Assert.Equal("\"id name\"", keyDirective.AsSyntaxNode().Arguments[0].Value.ToString()); | ||
|
||
schema.MatchSnapshot(); | ||
} | ||
|
||
public class Query<T> | ||
{ | ||
// ReSharper disable once InconsistentNaming | ||
public T someField(int id) => default!; | ||
} | ||
|
||
[Key("id")] | ||
public class TestTypeClassDirective | ||
{ | ||
public int Id { get; set; } | ||
} | ||
|
||
public class TestTypePropertyDirective | ||
{ | ||
[Key] | ||
public int Id { get; set; } | ||
} | ||
|
||
public class TestTypePropertyDirectives | ||
{ | ||
[Key] | ||
public int Id { get; set; } | ||
[Key] | ||
public string Name { get; set; } = default!; | ||
} | ||
} |
Oops, something went wrong.