diff --git a/src/HotChocolate/ApolloFederation/src/ApolloFederation/FederationTypeInterceptor.cs b/src/HotChocolate/ApolloFederation/src/ApolloFederation/FederationTypeInterceptor.cs index b737233188b..15d0c5c686b 100644 --- a/src/HotChocolate/ApolloFederation/src/ApolloFederation/FederationTypeInterceptor.cs +++ b/src/HotChocolate/ApolloFederation/src/ApolloFederation/FederationTypeInterceptor.cs @@ -294,7 +294,7 @@ private void RegisterExportedDirectives() var typeReference = _typeInspector.GetTypeRef(exportedDirective); if (_typeRegistry.TryGetType(typeReference, out var exportedDirectiveType)) { - composeDirectives.Add(new ComposeDirective(exportedDirectiveType.Type.Name)); + composeDirectives.Add(new ComposeDirective($"@{exportedDirectiveType.Type.Name}")); } } diff --git a/src/HotChocolate/ApolloFederation/test/ApolloFederation.Tests/Directives/ComposeDirectiveTests.cs b/src/HotChocolate/ApolloFederation/test/ApolloFederation.Tests/Directives/ComposeDirectiveTests.cs index b969cec0266..c6733f3fa8f 100644 --- a/src/HotChocolate/ApolloFederation/test/ApolloFederation.Tests/Directives/ComposeDirectiveTests.cs +++ b/src/HotChocolate/ApolloFederation/test/ApolloFederation.Tests/Directives/ComposeDirectiveTests.cs @@ -10,7 +10,7 @@ namespace HotChocolate.ApolloFederation; public class ComposeDirectiveTests { [Fact] - public async Task TestServiceTypeEmptyQueryTypePureCodeFirst() + public async Task ExportDirectiveUsingTypeCodeFirst() { // arrange var schema = await new ServiceCollection() @@ -32,6 +32,29 @@ public async Task TestServiceTypeEmptyQueryTypePureCodeFirst() .MatchSnapshot(); } + [Fact] + public async Task ExportDirectiveUsingNameCodeFirst() + { + // arrange + var schema = await new ServiceCollection() + .AddGraphQL() + .AddApolloFederation() + .AddQueryType() + .AddType
() + .ExportDirective("@custom") + .BuildSchemaAsync(); + + var entityType = schema.GetType(FederationTypeNames.ServiceType_Name); + var sdlResolver = entityType.Fields[WellKnownFieldNames.Sdl].Resolver!; + + // act + var value = await sdlResolver(TestHelper.CreateResolverContext(schema)); + + Utf8GraphQLParser + .Parse((string)value!) + .MatchSnapshot(); + } + [Key("field")] public class Address { diff --git a/src/HotChocolate/ApolloFederation/test/ApolloFederation.Tests/Directives/__snapshots__/ComposeDirectiveTests.TestServiceTypeEmptyQueryTypePureCodeFirst.graphql b/src/HotChocolate/ApolloFederation/test/ApolloFederation.Tests/Directives/__snapshots__/ComposeDirectiveTests.ExportDirectiveUsingNameCodeFirst.graphql similarity index 86% rename from src/HotChocolate/ApolloFederation/test/ApolloFederation.Tests/Directives/__snapshots__/ComposeDirectiveTests.TestServiceTypeEmptyQueryTypePureCodeFirst.graphql rename to src/HotChocolate/ApolloFederation/test/ApolloFederation.Tests/Directives/__snapshots__/ComposeDirectiveTests.ExportDirectiveUsingNameCodeFirst.graphql index da0786b488a..92387ea4b4a 100644 --- a/src/HotChocolate/ApolloFederation/test/ApolloFederation.Tests/Directives/__snapshots__/ComposeDirectiveTests.TestServiceTypeEmptyQueryTypePureCodeFirst.graphql +++ b/src/HotChocolate/ApolloFederation/test/ApolloFederation.Tests/Directives/__snapshots__/ComposeDirectiveTests.ExportDirectiveUsingNameCodeFirst.graphql @@ -1,4 +1,4 @@ -schema @composeDirective(name: "custom") @link(url: "https:\/\/specs.apollo.dev\/federation\/v2.6", import: [ "@key", "@tag", "FieldSet", "@composeDirective" ]) @link(url: "https:\/\/specs.custom.dev\/custom\/v1.0", import: [ "@custom" ]) { +schema @composeDirective(name: "@custom") @link(url: "https:\/\/specs.apollo.dev\/federation\/v2.6", import: [ "@key", "@tag", "FieldSet", "@composeDirective" ]) @link(url: "https:\/\/specs.custom.dev\/custom\/v1.0", import: [ "@custom" ]) { query: Query } diff --git a/src/HotChocolate/ApolloFederation/test/ApolloFederation.Tests/Directives/__snapshots__/ComposeDirectiveTests.ExportDirectiveUsingTypeCodeFirst.graphql b/src/HotChocolate/ApolloFederation/test/ApolloFederation.Tests/Directives/__snapshots__/ComposeDirectiveTests.ExportDirectiveUsingTypeCodeFirst.graphql new file mode 100644 index 00000000000..92387ea4b4a --- /dev/null +++ b/src/HotChocolate/ApolloFederation/test/ApolloFederation.Tests/Directives/__snapshots__/ComposeDirectiveTests.ExportDirectiveUsingTypeCodeFirst.graphql @@ -0,0 +1,37 @@ +schema @composeDirective(name: "@custom") @link(url: "https:\/\/specs.apollo.dev\/federation\/v2.6", import: [ "@key", "@tag", "FieldSet", "@composeDirective" ]) @link(url: "https:\/\/specs.custom.dev\/custom\/v1.0", import: [ "@custom" ]) { + query: Query +} + +type Address @key(fields: "field") { + field: String! @custom +} + +type Query { + _service: _Service! + _entities(representations: [_Any!]!): [_Entity]! +} + +"This type provides a field named sdl: String! which exposes the SDL of the service's schema. This SDL (schema definition language) is a printed version of the service's schema including the annotations of federation directives. This SDL does not include the additions of the federation spec." +type _Service { + sdl: String! +} + +"Union of all types that key directive applied. This information is needed by the Apollo federation gateway." +union _Entity = Address + +"Marks underlying custom directive to be included in the Supergraph schema." +directive @composeDirective(name: String!) repeatable on SCHEMA + +directive @custom on FIELD_DEFINITION + +"Used to indicate a combination of fields that can be used to uniquely identify and fetch an object or interface." +directive @key(fields: FieldSet! resolvable: Boolean = true) repeatable on OBJECT | INTERFACE + +"Links definitions within the document to external schemas." +directive @link("Gets imported specification url." url: String! "Gets optional list of imported element names." import: [String!]) repeatable on SCHEMA + +"Scalar representing a set of fields." +scalar FieldSet + +"The _Any scalar is used to pass representations of entities from external services into the root _entities field for execution. Validation of the _Any scalar is done by matching the __typename and @external fields defined in the schema." +scalar _Any