Skip to content

Commit 862c810

Browse files
authored
Fixed DescriptorAttributes for Scalars (#7161)
1 parent f812e1f commit 862c810

File tree

7 files changed

+32
-19
lines changed

7 files changed

+32
-19
lines changed

src/HotChocolate/Core/src/Types/Types/Descriptors/Definitions/TypeDefinitionBase~1.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ protected TypeDefinitionBase(Type runtimeType)
2727
public bool NeedsNameCompletion { get; set; }
2828

2929
/// <summary>
30-
/// Gets or sets the .net type representation of this type.
30+
/// Gets or sets the .NET type representation of this type.
3131
/// </summary>
3232
public virtual Type RuntimeType
3333
{

src/HotChocolate/Core/src/Types/Types/Descriptors/DescriptorBase~1.cs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,15 @@
66

77
namespace HotChocolate.Types.Descriptors;
88

9-
public abstract class DescriptorBase<T>
9+
public abstract class DescriptorBase<T>(IDescriptorContext context)
1010
: IDescriptor<T>
1111
, IDescriptorExtension<T>
1212
, IDescriptorExtension
1313
, IDefinitionFactory<T>
1414
where T : DefinitionBase
1515
{
16-
protected DescriptorBase(IDescriptorContext context)
17-
{
18-
Context = context ?? throw new ArgumentNullException(nameof(context));
19-
}
20-
21-
protected internal IDescriptorContext Context { get; }
16+
protected internal IDescriptorContext Context { get; } =
17+
context ?? throw new ArgumentNullException(nameof(context));
2218

2319
IDescriptorContext IHasDescriptorContext.Context => Context;
2420

src/HotChocolate/Core/src/Types/Types/Descriptors/InputFieldDescriptor.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ protected internal InputFieldDescriptor(
6363
protected override void OnCreateDefinition(InputFieldDefinition definition)
6464
{
6565
Context.Descriptors.Push(this);
66-
66+
6767
if (Definition is { AttributesAreApplied: false, Property: not null, })
6868
{
6969
Context.TypeInspector.ApplyAttributes(
@@ -77,7 +77,7 @@ protected override void OnCreateDefinition(InputFieldDefinition definition)
7777

7878
Context.Descriptors.Pop();
7979
}
80-
80+
8181
/// <inheritdoc />
8282
public IInputFieldDescriptor Name(string value)
8383
{

src/HotChocolate/Core/src/Types/Types/Descriptors/ScalarTypeDescriptor.cs

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System;
12
using HotChocolate.Language;
23
using HotChocolate.Types.Descriptors.Definitions;
34
using HotChocolate.Types.Helpers;
@@ -17,6 +18,25 @@ protected ScalarTypeDescriptor(IDescriptorContext context)
1718

1819
protected internal override ScalarTypeDefinition Definition { get; protected set; } = new();
1920

21+
protected override void OnCreateDefinition(ScalarTypeDefinition definition)
22+
{
23+
Context.Descriptors.Push(this);
24+
25+
if (!Definition.AttributesAreApplied &&
26+
Definition.RuntimeType != typeof(object))
27+
{
28+
Context.TypeInspector.ApplyAttributes(
29+
Context,
30+
this,
31+
Definition.RuntimeType);
32+
Definition.AttributesAreApplied = true;
33+
}
34+
35+
base.OnCreateDefinition(definition);
36+
37+
Context.Descriptors.Pop();
38+
}
39+
2040
public IScalarTypeDescriptor Directive<T>(T directiveInstance)
2141
where T : class
2242
{
@@ -40,6 +60,7 @@ public IScalarTypeDescriptor Directive(string name, params ArgumentNode[] argume
4060
public static ScalarTypeDescriptor New(
4161
IDescriptorContext context,
4262
string name,
43-
string? description)
44-
=> new(context) { Definition = { Name = name, Description = description } };
63+
string? description,
64+
Type scalarType)
65+
=> new(context) { Definition = { Name = name, Description = description, RuntimeType = scalarType } };
4566
}

src/HotChocolate/Core/src/Types/Types/Scalars/ScalarType.Initialization.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ protected ScalarType(string name, BindingBehavior bind = BindingBehavior.Explici
3737

3838
protected override ScalarTypeDefinition CreateDefinition(ITypeDiscoveryContext context)
3939
{
40-
var descriptor = ScalarTypeDescriptor.New(context.DescriptorContext, Name, Description);
40+
var descriptor = ScalarTypeDescriptor.New(context.DescriptorContext, Name, Description, GetType());
4141
Configure(descriptor);
4242
return descriptor.CreateDefinition();
4343
}

src/HotChocolate/Core/src/Types/Types/Scalars/ScalarType.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ public virtual bool IsInstanceOfType(object? runtimeValue)
9292
{
9393
return true;
9494
}
95+
9596
return RuntimeType.IsInstanceOfType(runtimeValue);
9697
}
9798

src/HotChocolate/Core/test/Types.Tests/Types/Scalars/CustomScalarTests.cs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public async Task CustomDirective_With_Directives_Annotation()
3030
var schema = await new ServiceCollection()
3131
.AddGraphQL()
3232
.AddQueryType<Query>()
33-
.AddType<FluentCustomScalarType>()
33+
.AddType<AnnotationCustomScalarType>()
3434
.BuildSchemaAsync();
3535

3636
// assert
@@ -92,11 +92,6 @@ public AnnotationCustomScalarType()
9292
: base("Custom")
9393
{
9494
}
95-
96-
protected override void Configure(IScalarTypeDescriptor descriptor)
97-
{
98-
descriptor.Directive<CustomDirective>();
99-
}
10095
}
10196

10297
[DirectiveType(DirectiveLocation.Scalar)]

0 commit comments

Comments
 (0)