Skip to content

Commit

Permalink
Added '@' prefix when exporting directives in Apollo. (#7812)
Browse files Browse the repository at this point in the history
  • Loading branch information
danielreynolds1 authored and michaelstaib committed Dec 16, 2024
1 parent a570145 commit 3d5716c
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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}"));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace HotChocolate.ApolloFederation;
public class ComposeDirectiveTests
{
[Fact]
public async Task TestServiceTypeEmptyQueryTypePureCodeFirst()
public async Task ExportDirectiveUsingTypeCodeFirst()
{
// arrange
var schema = await new ServiceCollection()
Expand All @@ -33,6 +33,29 @@ public async Task TestServiceTypeEmptyQueryTypePureCodeFirst()
.MatchSnapshot();
}

[Fact]
public async Task ExportDirectiveUsingNameCodeFirst()
{
// arrange
var schema = await new ServiceCollection()
.AddGraphQL()
.AddApolloFederation()
.AddQueryType()
.AddType<Address>()
.ExportDirective("@custom")
.BuildSchemaAsync();

var entityType = schema.GetType<ObjectType>(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
{
Expand Down
Original file line number Diff line number Diff line change
@@ -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
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 3d5716c

Please sign in to comment.