Skip to content

Commit

Permalink
Deprecate SchemaBuilder Hooks (#6652)
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelstaib authored Nov 1, 2023
1 parent 6f17b6f commit bf74176
Show file tree
Hide file tree
Showing 14 changed files with 46 additions and 4,656 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ jobs:
PR_NUMBER=${{ github.event.pull_request.number }}
# Comment on the PR using the PR number
gh pr comment $PR_NUMBER --body-file comment.txt
# gh pr comment $PR_NUMBER --body-file comment.txt
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ internal override void InitializeContext(
_typeLookup = typeLookup;
}

public override void OnBeforeCreateSchema(
internal override void OnBeforeCreateSchemaInternal(
IDescriptorContext context,
ISchemaBuilder schemaBuilder)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public void SetInterceptors(IReadOnlyCollection<TypeInterceptor> typeInterceptor
}
}

public override void OnBeforeCreateSchema(
internal override void OnBeforeCreateSchemaInternal(
IDescriptorContext context,
ISchemaBuilder schemaBuilder)
{
Expand All @@ -45,7 +45,7 @@ public override void OnBeforeCreateSchema(
// we first initialize all schema context ...
while (Unsafe.IsAddressLessThan(ref current, ref end))
{
current.OnBeforeCreateSchema(context, schemaBuilder);
current.OnBeforeCreateSchemaInternal(context, schemaBuilder);
current = ref Unsafe.Add(ref current, 1)!;
}

Expand Down Expand Up @@ -433,14 +433,14 @@ public override void OnTypesCompleted()
}
}

public override void OnAfterCreateSchema(IDescriptorContext context, ISchema schema)
internal override void OnAfterCreateSchemaInternal(IDescriptorContext context, ISchema schema)
{
ref var first = ref GetReference();
var length = _typeInterceptors.Length;

for (var i = 0; i < length; i++)
{
Unsafe.Add(ref first, i).OnAfterCreateSchema(context, schema);
Unsafe.Add(ref first, i).OnAfterCreateSchemaInternal(context, schema);
}
}

Expand Down
16 changes: 12 additions & 4 deletions src/HotChocolate/Core/src/Types/Configuration/TypeInterceptor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,12 @@ public abstract class TypeInterceptor
internal virtual bool IsMutationAggregator(IDescriptorContext context) => false;

internal virtual void SetSiblings(TypeInterceptor[] all) { }


[Obsolete("The schema builder is obsolete and will be removed in the next version.", error: true)]
public virtual void OnBeforeCreateSchema_(
IDescriptorContext context,
ISchemaBuilder schemaBuilder) { }

/// <summary>
/// This hook is invoked before anything else any allows for additional modification
/// with the schema builder.
Expand All @@ -41,7 +46,7 @@ internal virtual void SetSiblings(TypeInterceptor[] all) { }
/// <param name="schemaBuilder">
/// The schema builder.
/// </param>
public virtual void OnBeforeCreateSchema(
internal virtual void OnBeforeCreateSchemaInternal(
IDescriptorContext context,
ISchemaBuilder schemaBuilder) { }

Expand Down Expand Up @@ -259,7 +264,10 @@ public virtual void OnValidateType(
DefinitionBase definition) { }

public virtual void OnTypesCompleted() { }


[Obsolete("The schema builder is obsolete and will be removed in the next version.", error: true)]
public virtual void OnAfterCreateSchema_(IDescriptorContext context, ISchema schema) { }

/// <summary>
/// This hook is invoked after schema is fully created and gives access
/// to the created schema object.
Expand All @@ -270,7 +278,7 @@ public virtual void OnTypesCompleted() { }
/// <param name="schema">
/// The created schema.
/// </param>
public virtual void OnAfterCreateSchema(IDescriptorContext context, ISchema schema) { }
internal virtual void OnAfterCreateSchemaInternal(IDescriptorContext context, ISchema schema) { }

/// <summary>
/// This hook is invoked if an error occured during schema creation.
Expand Down
1 change: 1 addition & 0 deletions src/HotChocolate/Core/src/Types/HotChocolate.Types.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

<ItemGroup>
<InternalsVisibleTo Include="HotChocolate.Execution" />
<InternalsVisibleTo Include="HotChocolate.Execution.Tests" />
<InternalsVisibleTo Include="HotChocolate.Types.Mutations" />
<InternalsVisibleTo Include="HotChocolate.Authorization" />
<InternalsVisibleTo Include="HotChocolate.AspNetCore" />
Expand Down
4 changes: 2 additions & 2 deletions src/HotChocolate/Core/src/Types/SchemaBuilder.Setup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public static Schema Create(
((AggregateTypeInterceptor)context.TypeInterceptor)
.SetInterceptors(typeInterceptors);

context.TypeInterceptor.OnBeforeCreateSchema(context, builder);
context.TypeInterceptor.OnBeforeCreateSchemaInternal(context, builder);

var typeReferences = CreateTypeReferences(builder, context);
var typeRegistry = InitializeTypes(builder, context, typeReferences);
Expand Down Expand Up @@ -365,7 +365,7 @@ private static Schema CompleteSchema(
var schema = typeRegistry.Types.Select(t => t.Type).OfType<Schema>().First();
schema.CompleteSchema(definition);
lazySchema.Schema = schema;
context.TypeInterceptor.OnAfterCreateSchema(context, schema);
context.TypeInterceptor.OnAfterCreateSchemaInternal(context, schema);
return schema;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,30 @@
using System.Collections.Generic;
using HotChocolate.Language;

#nullable enable
#nullable enable

namespace HotChocolate.Types.Descriptors.Definitions;

public class SchemaTypeDefinition
: DefinitionBase<SchemaDefinitionNode>
, IHasDirectiveDefinition
{
private List<DirectiveDefinition>? _directives;

/// <summary>
/// Gets the list of directives that are annotated to this schema.
/// </summary>
public IList<DirectiveDefinition> Directives =>
internal IList<DirectiveDefinition> Directives =>
_directives ??= new List<DirectiveDefinition>();

/// <summary>
/// Specifies if this schema has directives.
/// </summary>
public bool HasDirectives => _directives is { Count: > 0 };
internal bool HasDirectives => _directives is { Count: > 0 };

/// <summary>
/// Gets the list of directives that are annotated to this schema.
/// </summary>
public IReadOnlyList<DirectiveDefinition> GetDirectives()
internal IReadOnlyList<DirectiveDefinition> GetDirectives()
{
if (_directives is null)
{
Expand All @@ -35,4 +34,17 @@ public IReadOnlyList<DirectiveDefinition> GetDirectives()

return _directives;
}

internal IHasDirectiveDefinition GetLegacyDefinition()
=> new CompatibilityLayer(this);

private class CompatibilityLayer(SchemaTypeDefinition definition) : IHasDirectiveDefinition
{
public bool HasDirectives => definition.HasDirectives;

public IList<DirectiveDefinition> Directives => definition.Directives;

public IReadOnlyList<DirectiveDefinition> GetDirectives()
=> definition.GetDirectives();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,22 +44,22 @@ public ISchemaTypeDescriptor Description(string value)
public ISchemaTypeDescriptor Directive<T>(T directiveInstance)
where T : class
{
Definition.AddDirective(directiveInstance, Context.TypeInspector);
Definition.GetLegacyDefinition().AddDirective(directiveInstance, Context.TypeInspector);
return this;
}

public ISchemaTypeDescriptor Directive<T>()
where T : class, new()
{
Definition.AddDirective(new T(), Context.TypeInspector);
Definition.GetLegacyDefinition().AddDirective(new T(), Context.TypeInspector);
return this;
}

public ISchemaTypeDescriptor Directive(
string name,
params ArgumentNode[] arguments)
{
Definition.AddDirective(name, arguments);
Definition.GetLegacyDefinition().AddDirective(name, arguments);
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ private sealed class OptionsInterceptor : TypeInterceptor
{
public IReadOnlySchemaOptions Options { get; private set; } = default!;

public override void OnBeforeCreateSchema(
internal override void OnBeforeCreateSchemaInternal(
IDescriptorContext context,
ISchemaBuilder schemaBuilder)
{
Expand Down
Loading

0 comments on commit bf74176

Please sign in to comment.