Skip to content

Added opt-in features #7652

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
25 changes: 25 additions & 0 deletions src/HotChocolate/Core/src/Abstractions/WellKnownDirectives.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,29 @@ public static class WellKnownDirectives
/// The name of the @semanticNonNull argument levels.
/// </summary>
public const string Levels = "levels";

/// <summary>
/// The name of the @requiresOptIn directive.
/// </summary>
public const string RequiresOptIn = "requiresOptIn";

/// <summary>
/// The name of the @requiresOptIn feature argument.
/// </summary>
public const string RequiresOptInFeatureArgument = "feature";

/// <summary>
/// The name of the @optInFeatureStability directive.
/// </summary>
public const string OptInFeatureStability = "optInFeatureStability";

/// <summary>
/// The name of the @optInFeatureStability feature argument.
/// </summary>
public const string OptInFeatureStabilityFeatureArgument = "feature";

/// <summary>
/// The name of the @optInFeatureStability stability argument.
/// </summary>
public const string OptInFeatureStabilityStabilityArgument = "stability";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using HotChocolate;
using HotChocolate.Execution.Configuration;
using HotChocolate.Types;

namespace Microsoft.Extensions.DependencyInjection;

public static partial class RequestExecutorBuilderExtensions
{
public static IRequestExecutorBuilder OptInFeatureStability(
this IRequestExecutorBuilder builder,
string feature,
string stability)
{
if (builder is null)
{
throw new ArgumentNullException(nameof(builder));
}

if (feature is null)
{
throw new ArgumentNullException(nameof(feature));
}

if (stability is null)
{
throw new ArgumentNullException(nameof(stability));
}

return Configure(
builder,
options => options.OnConfigureSchemaServicesHooks.Add(
(ctx, _) => ctx.SchemaBuilder.AddSchemaConfiguration(
d => d.Directive(new OptInFeatureStabilityDirective(feature, stability)))));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
using HotChocolate.Types;
using HotChocolate.Types.Descriptors;
using static HotChocolate.Utilities.ErrorHelper;

namespace HotChocolate.Configuration.Validation;

internal sealed class RequiresOptInValidationRule : ISchemaValidationRule
{
public void Validate(
IDescriptorContext context,
ISchema schema,
ICollection<ISchemaError> errors)
{
if (!context.Options.EnableOptInFeatures)
{
return;
}

foreach (var type in schema.Types)
{
switch (type)
{
case IInputObjectType inputObjectType:
foreach (var field in inputObjectType.Fields)
{
if (field.Type.IsNonNullType() && field.DefaultValue is null)
{
var requiresOptInDirectives = field.Directives
.Where(d => d.Type is RequiresOptInDirectiveType);

foreach (var _ in requiresOptInDirectives)
{
errors.Add(RequiresOptInOnRequiredInputField(
inputObjectType,
field));
}
}
}

break;

case IObjectType objectType:
foreach (var field in objectType.Fields)
{
foreach (var argument in field.Arguments)
{
if (argument.Type.IsNonNullType() && argument.DefaultValue is null)
{
var requiresOptInDirectives = argument.Directives
.Where(d => d.Type is RequiresOptInDirectiveType);

foreach (var _ in requiresOptInDirectives)
{
errors.Add(RequiresOptInOnRequiredArgument(
objectType,
field,
argument));
}
}
}
}

break;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ internal static class SchemaValidator
new DirectiveValidationRule(),
new InterfaceHasAtLeastOneImplementationRule(),
new IsSelectedPatternValidation(),
new EnsureFieldResultsDeclareErrorsRule()
new EnsureFieldResultsDeclareErrorsRule(),
new RequiresOptInValidationRule()
];

public static IReadOnlyList<ISchemaError> Validate(
Expand Down
5 changes: 5 additions & 0 deletions src/HotChocolate/Core/src/Types/IReadOnlySchemaOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,11 @@ public interface IReadOnlySchemaOptions
/// </summary>
bool EnableTag { get; }

/// <summary>
/// Specifies that the opt-in features functionality will be enabled.
/// </summary>
bool EnableOptInFeatures { get; }

/// <summary>
/// Specifies the default dependency injection scope for query fields.
/// </summary>
Expand Down
Loading
Loading