-
-
Notifications
You must be signed in to change notification settings - Fork 744
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added support for directives on scalar types (#7150)
- Loading branch information
1 parent
9c68057
commit d786b2f
Showing
28 changed files
with
528 additions
and
150 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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
using System; | ||
#if NET8_0_OR_GREATER | ||
using System.Collections.Frozen; | ||
#endif | ||
using System.Collections.Generic; | ||
using HotChocolate.Types; | ||
using HotChocolate.Types.Descriptors; | ||
|
||
namespace HotChocolate; | ||
|
||
internal static class SchemaTools | ||
{ | ||
public static void AddSchemaConfiguration( | ||
this ISchemaBuilder builder, | ||
Action<ISchemaTypeDescriptor> configure) | ||
{ | ||
if (builder is null) | ||
{ | ||
throw new ArgumentNullException(nameof(builder)); | ||
} | ||
|
||
if (configure is null) | ||
{ | ||
throw new ArgumentNullException(nameof(configure)); | ||
} | ||
|
||
List<Action<ISchemaTypeDescriptor>> options; | ||
|
||
if (!builder.ContextData.TryGetValue(WellKnownContextData.InternalSchemaOptions, out var value)) | ||
{ | ||
options = []; | ||
builder.ContextData.Add(WellKnownContextData.InternalSchemaOptions, options); | ||
value = options; | ||
} | ||
|
||
options = (List<Action<ISchemaTypeDescriptor>>)value!; | ||
options.Add(configure); | ||
} | ||
|
||
public static void AddSchemaConfiguration( | ||
this IDescriptorContext context, | ||
Action<ISchemaTypeDescriptor> configure) | ||
{ | ||
if (context is null) | ||
{ | ||
throw new ArgumentNullException(nameof(context)); | ||
} | ||
|
||
if (configure is null) | ||
{ | ||
throw new ArgumentNullException(nameof(configure)); | ||
} | ||
|
||
List<Action<ISchemaTypeDescriptor>> options; | ||
|
||
if (!context.ContextData.TryGetValue(WellKnownContextData.InternalSchemaOptions, out var value)) | ||
{ | ||
options = []; | ||
context.ContextData.Add(WellKnownContextData.InternalSchemaOptions, options); | ||
value = options; | ||
} | ||
|
||
options = (List<Action<ISchemaTypeDescriptor>>)value!; | ||
options.Add(configure); | ||
} | ||
|
||
public static void ApplySchemaConfigurations( | ||
this IDescriptorContext context, | ||
ISchemaTypeDescriptor descriptor) | ||
{ | ||
if (context is null) | ||
{ | ||
throw new ArgumentNullException(nameof(context)); | ||
} | ||
|
||
if (descriptor is null) | ||
{ | ||
throw new ArgumentNullException(nameof(descriptor)); | ||
} | ||
|
||
if (context.ContextData.TryGetValue(WellKnownContextData.InternalSchemaOptions, out var value) && | ||
value is List<Action<ISchemaTypeDescriptor>> options) | ||
{ | ||
foreach (var option in options) | ||
{ | ||
option(descriptor); | ||
} | ||
} | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
src/HotChocolate/Core/src/Types/Types/Attributes/ScalarTypeDescriptorAttribute.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,31 @@ | ||
using System; | ||
using System.Reflection; | ||
using HotChocolate.Types.Descriptors; | ||
|
||
#nullable enable | ||
|
||
namespace HotChocolate.Types; | ||
|
||
[AttributeUsage( | ||
AttributeTargets.Class, | ||
Inherited = true, | ||
AllowMultiple = true)] | ||
public abstract class ScalarTypeDescriptorAttribute : DescriptorAttribute | ||
{ | ||
protected internal sealed override void TryConfigure( | ||
IDescriptorContext context, | ||
IDescriptor descriptor, | ||
ICustomAttributeProvider element) | ||
{ | ||
if (descriptor is IScalarTypeDescriptor d | ||
&& element is Type t) | ||
{ | ||
OnConfigure(context, d, t); | ||
} | ||
} | ||
|
||
protected abstract void OnConfigure( | ||
IDescriptorContext context, | ||
IScalarTypeDescriptor descriptor, | ||
Type type); | ||
} |
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.