Skip to content

Commit

Permalink
Added support for directives on scalar types (#7150)
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelstaib authored Jun 5, 2024
1 parent 9c68057 commit d786b2f
Show file tree
Hide file tree
Showing 28 changed files with 528 additions and 150 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ void Register(
void MarkResolved(TypeReference typeReference);

bool IsResolved(TypeReference typeReference);



TypeSystemObjectBase CreateInstance(Type namedSchemaType);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ internal sealed partial class RegisteredType : ITypeCompletionContext

/// <inheritdoc />
IReadOnlyList<FieldMiddleware> ITypeCompletionContext.GlobalComponents
=> GlobalComponents ?? (IReadOnlyList<FieldMiddleware>)Array.Empty<FieldMiddleware>();
=> GlobalComponents ?? (IReadOnlyList<FieldMiddleware>)[];

/// <inheritdoc />
public IsOfTypeFallback? IsOfType { get; private set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Linq;
using HotChocolate.Configuration.Validation;
using HotChocolate.Language;
using HotChocolate.Resolvers;
using HotChocolate.Types;
Expand All @@ -25,7 +24,6 @@ internal sealed class TypeInitializer
private readonly TypeInterceptor _interceptor;
private readonly IsOfTypeFallback? _isOfType;
private readonly Func<TypeSystemObjectBase, RootTypeKind> _getTypeKind;
private readonly IReadOnlySchemaOptions _options;
private readonly TypeRegistry _typeRegistry;
private readonly TypeLookup _typeLookup;
private readonly TypeReferenceResolver _typeReferenceResolver;
Expand All @@ -44,6 +42,11 @@ public TypeInitializer(
Func<TypeSystemObjectBase, RootTypeKind> getTypeKind,
IReadOnlySchemaOptions options)
{
if(options is null)
{
throw new ArgumentNullException(nameof(options));
}

_context = descriptorContext ??
throw new ArgumentNullException(nameof(descriptorContext));
_typeRegistry = typeRegistry ??
Expand All @@ -52,8 +55,6 @@ public TypeInitializer(
throw new ArgumentNullException(nameof(initialTypes));
_getTypeKind = getTypeKind ??
throw new ArgumentNullException(nameof(getTypeKind));
_options = options ??
throw new ArgumentNullException(nameof(options));

_isOfType = isOfType ?? options.DefaultIsOfTypeCheck;

Expand Down
10 changes: 5 additions & 5 deletions src/HotChocolate/Core/src/Types/Configuration/TypeRegistrar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public TypeRegistrar(IDescriptorContext context,
throw new ArgumentNullException(nameof(typeLookup));
_context = context ??
throw new ArgumentNullException(nameof(context));
_interceptor = typeInterceptor ??
_interceptor = typeInterceptor ??
throw new ArgumentNullException(nameof(typeInterceptor));
_schemaServices = context.Services;
_applicationServices = context.Services.GetService<IApplicationServiceProvider>();
Expand Down Expand Up @@ -61,15 +61,15 @@ public void Register(
{
return;
}

RegisterTypeAndResolveReferences(registeredType);

if (obj is not IHasRuntimeType hasRuntimeType ||
if (obj is not IHasRuntimeType hasRuntimeType ||
hasRuntimeType.RuntimeType == typeof(object))
{
return;
}

var runtimeTypeRef =
_context.TypeInspector.GetTypeRef(
hasRuntimeType.RuntimeType,
Expand All @@ -82,7 +82,7 @@ public void Register(
{
return;
}

MarkResolved(runtimeTypeRef);
_typeRegistry.TryRegister(runtimeTypeRef, registeredType.References[0]);
}
Expand Down
82 changes: 0 additions & 82 deletions src/HotChocolate/Core/src/Types/Schema.Initialization.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
#if NET8_0_OR_GREATER
using System.Collections.Frozen;
#endif
using System.Collections.Generic;
using System.Linq;
using HotChocolate.Configuration;
using HotChocolate.Types;
Expand Down Expand Up @@ -113,84 +112,3 @@ internal void CompleteSchema(SchemaTypesDefinition schemaTypesDefinition)
_sealed = true;
}
}

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);
}
}
}
}
9 changes: 8 additions & 1 deletion src/HotChocolate/Core/src/Types/SchemaBuilder.Setup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ public static Schema Create(
builder._typeInterceptors.Add(typeof(DirectiveTypeInterceptor));
}

if(builder._schemaFirstTypeInterceptor is not null)
{
typeInterceptors.Add(builder._schemaFirstTypeInterceptor);
}

InitializeInterceptors(
context.Services,
builder._typeInterceptors,
Expand Down Expand Up @@ -146,7 +151,9 @@ private static IEnumerable<TypeReference> ParseDocuments(
schemaDocument = schemaDocument.RemoveBuiltInTypes();
documents.Add(schemaDocument);

var visitorContext = new SchemaSyntaxVisitorContext(context);
var visitorContext = new SchemaSyntaxVisitorContext(
context,
builder._schemaFirstTypeInterceptor!.Directives);
var visitor = new SchemaSyntaxVisitor();

visitor.Visit(schemaDocument, visitorContext);
Expand Down
5 changes: 4 additions & 1 deletion src/HotChocolate/Core/src/Types/SchemaBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using HotChocolate.Types;
using HotChocolate.Types.Descriptors;
using HotChocolate.Types.Descriptors.Definitions;
using HotChocolate.Types.Factories;
using HotChocolate.Types.Interceptors;
using HotChocolate.Types.Introspection;
using HotChocolate.Types.Pagination;
Expand All @@ -31,6 +32,7 @@ public partial class SchemaBuilder : ISchemaBuilder
private readonly Dictionary<OperationType, CreateRef> _operations = new();
private readonly Dictionary<(Type, string?), List<CreateConvention>> _conventions = new();
private readonly Dictionary<Type, (CreateRef, CreateRef)> _clrTypes = new();
private SchemaFirstTypeInterceptor? _schemaFirstTypeInterceptor;

private readonly List<object> _typeInterceptors =
[
Expand Down Expand Up @@ -146,6 +148,7 @@ public ISchemaBuilder AddDocument(LoadSchemaDocument loadSchemaDocument)
throw new ArgumentNullException(nameof(loadSchemaDocument));
}

_schemaFirstTypeInterceptor ??= new SchemaFirstTypeInterceptor();
_documents.Add(loadSchemaDocument);
return this;
}
Expand Down Expand Up @@ -262,7 +265,7 @@ public ISchemaBuilder AddType(INamedTypeExtension typeExtension)
_types.Add(_ => TypeReference.Create(typeExtension));
return this;
}

internal void AddTypeReference(TypeReference typeReference)
{
if (typeReference is null)
Expand Down
90 changes: 90 additions & 0 deletions src/HotChocolate/Core/src/Types/SchemaTools.cs
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);
}
}
}
}
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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ public interface IObjectField : IOutputField
/// Defines if this field can be executed in parallel with other fields.
/// </summary>
bool IsParallelExecutable { get; }

/// <summary>
/// Defines in which DI scope this field is executed.
/// </summary>
DependencyInjectionScope DependencyInjectionScope { get; }
DependencyInjectionScope DependencyInjectionScope { get; }

/// <summary>
/// Defines that the resolver pipeline returns an
Expand Down Expand Up @@ -68,4 +68,4 @@ public interface IObjectField : IOutputField
/// this property will return <see cref="Member"/>.
/// </summary>
MemberInfo? ResolverMember { get; }
}
}
Loading

0 comments on commit d786b2f

Please sign in to comment.