Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using Microsoft.Extensions.DependencyInjection;
using HotChocolate.Execution;
using HotChocolate.Types;

namespace HotChocolate.ApolloFederation.Directives;

public class TagDirectiveTests
{
[Fact]
public async Task Tag_InApolloFederationMode_ExcludesDirectiveDefinitionLocation()
{
// arrange & act
var schema = await new ServiceCollection()
.AddGraphQL()
.AddApolloFederation()
.AddQueryType<Query>()
.BuildSchemaAsync(cancellationToken: TestContext.Current.CancellationToken);

// assert
var tag = schema.DirectiveTypes["tag"];
Assert.False(tag.Locations.HasFlag(DirectiveLocation.DirectiveDefinition));
}

public class Query
{
[Tag("public")]
public string Foo() => "bar";
}
}
1 change: 1 addition & 0 deletions src/HotChocolate/Caching/test/Caching.Tests/SchemaTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ directive @tag("The name of the tag." name: String!) repeatable on
| ENUM_VALUE
| INPUT_OBJECT
| INPUT_FIELD_DEFINITION
| DIRECTIVE_DEFINITION
"""");
}

Expand Down
3 changes: 2 additions & 1 deletion src/HotChocolate/Core/src/Types/Types/Directives/Tag.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ namespace HotChocolate.Types;
| DirectiveLocation.InputFieldDefinition
| DirectiveLocation.ArgumentDefinition
| DirectiveLocation.EnumValue
| DirectiveLocation.Schema,
| DirectiveLocation.Schema
| DirectiveLocation.DirectiveDefinition,
IsRepeatable = true)]
[TagDirectiveConfig]
[GraphQLDescription(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,10 @@ protected internal override void TryConfigure(
desc.Tag(Name);
break;

case IDirectiveTypeDescriptor desc:
desc.Tag(Name);
break;

default:
throw new SchemaException(
SchemaErrorBuilder.New()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,36 @@ public static ISchemaTypeDescriptor Tag(
return descriptor;
}

/// <summary>
/// Adds a @tag(name: "your-value") to a <see cref="DirectiveType"/>.
/// <code>
/// directive @custom @tag(name: "your-value") on OBJECT
/// </code>
/// </summary>
/// <param name="descriptor">
/// The <paramref name="descriptor"/> on which this directive shall be annotated.
/// </param>
/// <param name="name">
/// The value represents the <paramref name="name"/> of the tag.
/// </param>
/// <returns>
/// Returns the <paramref name="descriptor"/> on which this directive
/// was applied for configuration chaining.
/// </returns>
/// <exception cref="ArgumentNullException">
/// <paramref name="descriptor"/> is <c>null</c>.
/// </exception>
/// <exception cref="ArgumentException">
/// <paramref name="name"/> is <c>null</c> or <see cref="string.Empty"/>.
/// </exception>
public static IDirectiveTypeDescriptor Tag(
this IDirectiveTypeDescriptor descriptor,
string name)
{
ApplyTag(descriptor, name);
return descriptor;
}

private static void ApplyTag(
this IDescriptor descriptor,
string name)
Expand Down Expand Up @@ -475,6 +505,10 @@ private static void ApplyTag(
desc.Directive(new Tag(name));
break;

case IDirectiveTypeDescriptor desc:
desc.Directive(new Tag(name));
break;

default:
throw new NotSupportedException(
TypeResources.TagDirective_Descriptor_NotSupported);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,45 +1,163 @@
using HotChocolate.Execution;
using HotChocolate.Language;
using Microsoft.Extensions.DependencyInjection;

namespace HotChocolate.Types.Directives;

public class TagDirectiveTests
{
[Fact]
public async Task EnsureAllLocationsAreApplied()
public async Task SchemaFirst_Tag()
{
// arrange & act
var schema =
await new ServiceCollection()
.AddGraphQL()
.AddQueryType<Query>()
.AddObjectType<Foo>()
.AddType<FooDirective>()
.SetSchema(d => d.Tag("OnSchema"))
.AddDocumentFromString(
"""
schema @tag(name: "OnSchema") {
query: Query
}

type Query @tag(name: "OnObjectType") {
foo(a: String! @tag(name: "OnObjectFieldArg")): IFoo!
@tag(name: "OnObjectField")
fooEnum(input: FooInput!): FooEnum!
fooUnion: FooUnion!
}

type Foo implements IFoo {
bar(baz: String!): String!
}

interface IFoo @tag(name: "OnInterface") {
bar(baz: String! @tag(name: "OnInterfaceFieldArg")): String!
@tag(name: "OnInterfaceField")
}

union FooUnion @tag(name: "OnUnion") = FooUnionA | FooUnionB

type FooUnionA {
id: String!
}

type FooUnionB {
id: String!
}

input FooInput @tag(name: "OnInputObjectType") {
bar: String! @tag(name: "OnInputObjectField")
}

enum FooEnum @tag(name: "OnEnum") {
FOO @tag(name: "OnEnumValue")
BAR
}

directive @foo(arg: String! @tag(name: "OnDirectiveArgument"))
@tag(name: "OnDirectiveDefinition") on QUERY

directive @tag("The name of the tag." name: String!)
repeatable on SCHEMA | SCALAR | OBJECT | FIELD_DEFINITION |
ARGUMENT_DEFINITION | INTERFACE | UNION | ENUM | ENUM_VALUE |
INPUT_OBJECT | INPUT_FIELD_DEFINITION | DIRECTIVE_DEFINITION
""")
.UseField(_ => _ => default)
.BuildSchemaAsync(cancellationToken: TestContext.Current.CancellationToken);

// assert
schema.MatchSnapshot();
}

[Fact]
public async Task SchemaFirst_Tag()
public async Task CodeFirst_Tag()
{
// arrange & act
var schema =
await new ServiceCollection()
.AddGraphQL()
.AddDocumentFromString(
"""
type Query {
field: String @tag(name: "abc")
}

directive @tag("The name of the tag." name: String!)
repeatable on SCHEMA | SCALAR | OBJECT | FIELD_DEFINITION |
ARGUMENT_DEFINITION | INTERFACE | UNION | ENUM | ENUM_VALUE |
INPUT_OBJECT | INPUT_FIELD_DEFINITION
""")
.SetSchema(d => d.Tag("OnSchema"))
.AddQueryType(d =>
{
d.Tag("OnObjectType");
d.Field("foo")
.Type("IFoo!")
.Argument("a", a => a.Type("String!").Tag("OnObjectFieldArg"))
.Tag("OnObjectField");
d.Field("fooEnum")
.Type("FooEnum!")
.Argument("input", a => a.Type("FooInput!"));
d.Field("fooUnion").Type("FooUnion!");
})
.AddInterfaceType(d =>
{
d.Name("IFoo");
d.Tag("OnInterface");
d.Field("bar")
.Type("String!")
.Argument("baz", a => a.Type("String!").Tag("OnInterfaceFieldArg"))
.Tag("OnInterfaceField");
})
.AddObjectType(d => d
.Name("Foo")
.Implements("IFoo")
.Field("bar")
.Type("String!")
.Argument("baz", a => a.Type("String!")))
.AddUnionType(d => d
.Name("FooUnion")
.Tag("OnUnion")
.Type(new NamedTypeNode("FooUnionA"))
.Type(new NamedTypeNode("FooUnionB")))
.AddObjectType(d => d.Name("FooUnionA").Field("id").Type("String!"))
.AddObjectType(d => d.Name("FooUnionB").Field("id").Type("String!"))
.AddInputObjectType(d =>
{
d.Name("FooInput");
d.Tag("OnInputObjectType");
d.Field("bar").Type("String!").Tag("OnInputObjectField");
})
.AddEnumType(d =>
{
d.Name("FooEnum");
d.Tag("OnEnum");
d.Value("FOO").Tag("OnEnumValue");
d.Value("BAR");
})
.AddDirectiveType(d =>
{
d.Name("foo");
d.Location(DirectiveLocation.Query);
d.Tag("OnDirectiveDefinition");
d.Argument("arg", a => a.Type("String!").Tag("OnDirectiveArgument"));
})
.UseField(_ => _ => default)
.BuildSchemaAsync(cancellationToken: TestContext.Current.CancellationToken);

// assert
schema.MatchSnapshot();
}

[Fact]
public async Task ImplementationFirst_Tag()
{
// The SCHEMA position is intentionally omitted here: HotChocolate does not
// apply class-level descriptor attributes to Schema subclasses, so a [Tag]
// on a Schema class has no effect. The schema-first and code-first modes
// cover the SCHEMA position instead.

// arrange & act
var schema =
await new ServiceCollection()
.AddGraphQL()
.AddQueryType<Query>()
.AddObjectType<Foo>()
.AddType<FooUnionA>()
.AddType<FooUnionB>()
.AddType<FooDirective>()
.BuildSchemaAsync(cancellationToken: TestContext.Current.CancellationToken);

// assert
schema.MatchSnapshot();
}

Expand Down Expand Up @@ -109,6 +227,8 @@ public class Query
public IFoo GetFoo([Tag("OnObjectFieldArg")] string a) => new Foo();

public FooEnum GetFooEnum(FooInput input) => FooEnum.Foo;

public IFooUnion GetFooUnion() => new FooUnionA();
}

[Tag("OnInterface")]
Expand All @@ -123,6 +243,20 @@ public class Foo : IFoo
public string Bar(string baz) => "Bar" + baz;
}

[UnionType("FooUnion")]
[Tag("OnUnion")]
public interface IFooUnion;

public sealed class FooUnionA : IFooUnion
{
public string Id => "a";
}

public sealed class FooUnionB : IFooUnion
{
public string Id => "b";
}

[Tag("OnEnum")]
public enum FooEnum
{
Expand All @@ -138,6 +272,7 @@ public class FooInput
public required string Bar { get; set; }
}

[Tag("OnDirectiveDefinition")]
[DirectiveType(DirectiveLocation.Query)]
public class FooDirective
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,28 @@ type Query @tag(name: "OnObjectType") {
foo(a: String! @tag(name: "OnObjectFieldArg")): IFoo!
@tag(name: "OnObjectField")
fooEnum(input: FooInput!): FooEnum!
fooUnion: FooUnion!
}

type Foo implements IFoo {
bar(baz: String!): String!
}

type FooUnionA {
id: String!
}

type FooUnionB {
id: String!
}

interface IFoo @tag(name: "OnInterface") {
bar(baz: String! @tag(name: "OnInterfaceFieldArg")): String!
@tag(name: "OnInterfaceField")
}

union FooUnion @tag(name: "OnUnion") = FooUnionA | FooUnionB

input FooInput @tag(name: "OnInputObjectType") {
bar: String! @tag(name: "OnInputObjectField")
}
Expand All @@ -26,7 +37,8 @@ enum FooEnum @tag(name: "OnEnum") {
BAR
}

directive @foo(arg: String! @tag(name: "OnDirectiveArgument")) on QUERY
directive @foo(arg: String! @tag(name: "OnDirectiveArgument"))
@tag(name: "OnDirectiveDefinition") on QUERY

"""
The @tag directive is used to apply arbitrary string
Expand All @@ -52,3 +64,4 @@ directive @tag("The name of the tag." name: String!) repeatable on
| ENUM_VALUE
| INPUT_OBJECT
| INPUT_FIELD_DEFINITION
| DIRECTIVE_DEFINITION
Loading
Loading