-
-
Notifications
You must be signed in to change notification settings - Fork 754
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
26943d1
commit ea594fb
Showing
10 changed files
with
4,903 additions
and
15 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
122 changes: 122 additions & 0 deletions
122
...hocolate/ApolloFederation/test/ApolloFederation.Tests/Directives/ComposeDirectiveTests.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,122 @@ | ||
using System; | ||
using System.Reflection; | ||
using System.Threading.Tasks; | ||
using CookieCrumble; | ||
using HotChocolate.ApolloFederation.Constants; | ||
using HotChocolate.ApolloFederation.Types; | ||
using HotChocolate.Execution; | ||
using HotChocolate.Language; | ||
using HotChocolate.Types; | ||
using HotChocolate.Types.Descriptors; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using DirectiveLocation = HotChocolate.Types.DirectiveLocation; | ||
|
||
namespace HotChocolate.ApolloFederation; | ||
|
||
public class ComposeDirectiveTests | ||
{ | ||
[Fact] | ||
public async Task TestServiceTypeEmptyQueryTypePureCodeFirst() | ||
{ | ||
// 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 | ||
{ | ||
[CustomDirective] | ||
public string Field => "abc"; | ||
} | ||
|
||
[Package("https://specs.custom.dev/custom/v1.0")] | ||
[DirectiveType(DirectiveLocation.FieldDefinition)] | ||
public sealed class Custom; | ||
|
||
public sealed class CustomDirectiveAttribute : DirectiveAttribute<Custom> | ||
{ | ||
public CustomDirectiveAttribute() | ||
: base(new Custom()) | ||
{ | ||
} | ||
} | ||
|
||
public abstract class DirectiveAttribute<T>(T directive) : DescriptorAttribute where T : class | ||
{ | ||
protected override void TryConfigure( | ||
IDescriptorContext context, | ||
IDescriptor descriptor, | ||
ICustomAttributeProvider element) | ||
{ | ||
switch (descriptor) | ||
{ | ||
case ArgumentDescriptor desc: | ||
desc.Directive(directive); | ||
break; | ||
|
||
case DirectiveArgumentDescriptor desc: | ||
desc.Directive(directive); | ||
break; | ||
|
||
case EnumTypeDescriptor desc: | ||
desc.Directive(directive); | ||
break; | ||
|
||
case EnumValueDescriptor desc: | ||
desc.Directive(directive); | ||
break; | ||
|
||
case InputFieldDescriptor desc: | ||
desc.Directive(directive); | ||
break; | ||
|
||
case InputObjectTypeDescriptor desc: | ||
desc.Directive(directive); | ||
break; | ||
|
||
case InterfaceFieldDescriptor desc: | ||
desc.Directive(directive); | ||
break; | ||
|
||
case InterfaceTypeDescriptor desc: | ||
desc.Directive(directive); | ||
break; | ||
|
||
case ObjectFieldDescriptor desc: | ||
desc.Directive(directive); | ||
break; | ||
|
||
case ObjectTypeDescriptor desc: | ||
desc.Directive(directive); | ||
break; | ||
|
||
case SchemaTypeDescriptor desc: | ||
desc.Directive(directive); | ||
break; | ||
|
||
case UnionTypeDescriptor desc: | ||
desc.Directive(directive); | ||
break; | ||
|
||
default: | ||
throw new ArgumentOutOfRangeException(nameof(descriptor)); | ||
} | ||
} | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
...es/__snapshots__/ComposeDirectiveTests.TestServiceTypeEmptyQueryTypePureCodeFirst.graphql
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,37 @@ | ||
schema @composeDirective(name: "custom") @link(url: "https:\/\/specs.apollo.dev\/federation\/v2.5", import: [ "@composeDirective", "@key", "FieldSet" ]) @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 | ||
|
||
"Object representation of @link directive." | ||
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 |
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
Oops, something went wrong.