Skip to content

Type System Alignment #8247

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 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ [new Uri(FederationVersionUrls.Federation27)] = FederationVersion.Federation27,

public static FederationVersion GetFederationVersion<T>(
this IDescriptor<T> descriptor)
where T : DefinitionBase
where T : TypeSystemConfiguration
{
var contextData = descriptor.Extend().Context.ContextData;
if (contextData.TryGetValue(FederationContextData.FederationVersion, out var value) &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ internal sealed class FederationTypeInterceptor : TypeInterceptor
private TypeRegistry _typeRegistry = default!;
private ObjectType _queryType = default!;
private ExtendedTypeDirectiveReference _keyDirectiveReference = default!;
private SchemaTypeDefinition _schemaTypeDefinition = default!;
private SchemaTypeConfiguration _schemaTypeCfg = default!;
private RegisteredType _schemaType = default!;
private bool _registeredTypes;

Expand All @@ -63,22 +63,22 @@ internal override void InitializeContext(

public override void OnAfterInitialize(
ITypeDiscoveryContext discoveryContext,
DefinitionBase definition)
TypeSystemConfiguration configuration)
{
if (discoveryContext.Type is ObjectType objectType &&
definition is ObjectTypeDefinition objectTypeDefinition)
configuration is ObjectTypeConfiguration objectTypeCfg)
{
ApplyMethodLevelReferenceResolvers(
objectType,
objectTypeDefinition);
objectTypeCfg);

AddToUnionIfHasTypeLevelKeyDirective(
objectType,
objectTypeDefinition);
objectTypeCfg);

AggregatePropertyLevelKeyDirectives(
objectType,
objectTypeDefinition,
objectTypeCfg,
discoveryContext);
}
}
Expand Down Expand Up @@ -114,40 +114,40 @@ public override IEnumerable<TypeReference> RegisterMoreTypes(

public override void OnBeforeCompleteName(
ITypeCompletionContext completionContext,
DefinitionBase definition)
TypeSystemConfiguration configuration)
{
if (definition is SchemaTypeDefinition schemaDef)
if (configuration is SchemaTypeConfiguration schemaCfg)
{
_schemaType = (RegisteredType)completionContext;
_schemaTypeDefinition = schemaDef;
_schemaTypeCfg = schemaCfg;
}
}

public override void OnAfterCompleteName(
ITypeCompletionContext completionContext,
DefinitionBase definition)
TypeSystemConfiguration configuration)
{
if (_context.GetFederationVersion() == FederationVersion.Federation10
|| definition is not ITypeDefinition and not DirectiveTypeDefinition)
|| configuration is not ITypeConfiguration and not DirectiveTypeConfiguration)
{
return;
}

// if we find a PagingInfo we will make all fields sharable.
if (definition is ObjectTypeDefinition typeDef
&& typeDef.Name.Equals(PageInfoType.Names.PageInfo))
if (configuration is ObjectTypeConfiguration typeCfg
&& typeCfg.Name.Equals(PageInfoType.Names.PageInfo))
{
foreach (var fieldDef in typeDef.Fields)
foreach (var fieldCfg in typeCfg.Fields)
{
if (fieldDef.Directives.All(t => t.Value is not ShareableDirective))
if (fieldCfg.Directives.All(t => t.Value is not ShareableDirective))
{
var typeRef = TypeReference.CreateDirective(_typeInspector.GetType(typeof(ShareableDirective)));
fieldDef.Directives.Add(new DirectiveDefinition(ShareableDirective.Default, typeRef));
fieldCfg.Directives.Add(new DirectiveConfiguration(ShareableDirective.Default, typeRef));
}
}
}

var hasRuntimeType = (IHasRuntimeType)definition;
var hasRuntimeType = (IHasRuntimeType)configuration;
var type = hasRuntimeType.RuntimeType;

if (type != typeof(object) &&
Expand Down Expand Up @@ -259,7 +259,7 @@ private void RegisterImports()
TypeDependencyFulfilled.Completed);
_schemaType.Dependencies.Add(dependency);

_schemaTypeDefinition
_schemaTypeCfg
.GetLegacyDefinition()
.AddDirective(
new LinkDirective(version.ToUrl(), federationTypes),
Expand All @@ -272,7 +272,7 @@ private void RegisterImports()
continue;
}

_schemaTypeDefinition
_schemaTypeCfg
.GetLegacyDefinition()
.AddDirective(
new LinkDirective(import.Key, import.Value),
Expand Down Expand Up @@ -307,7 +307,7 @@ private void RegisterExportedDirectives()

foreach (var directive in composeDirectives)
{
_schemaTypeDefinition
_schemaTypeCfg
.GetLegacyDefinition()
.AddDirective(directive, _typeInspector);
}
Expand All @@ -316,7 +316,7 @@ private void RegisterExportedDirectives()

public override void OnAfterResolveRootType(
ITypeCompletionContext completionContext,
ObjectTypeDefinition definition,
ObjectTypeConfiguration configuration,
OperationType operationType)
{
if (operationType is OperationType.Query)
Expand All @@ -327,37 +327,37 @@ public override void OnAfterResolveRootType(

public override void OnBeforeCompleteType(
ITypeCompletionContext completionContext,
DefinitionBase definition)
TypeSystemConfiguration configuration)
{
AddMemberTypesToTheEntityUnionType(
completionContext,
definition);
configuration);

AddServiceTypeToQueryType(
completionContext,
definition);
configuration);
}

public override void OnAfterMakeExecutable(
ITypeCompletionContext completionContext,
DefinitionBase definition)
TypeSystemConfiguration configuration)
{
if (completionContext.Type is ObjectType type &&
definition is ObjectTypeDefinition typeDef)
configuration is ObjectTypeConfiguration typeCfg)
{
CompleteExternalFieldSetters(type, typeDef);
CompleteReferenceResolver(typeDef);
CompleteExternalFieldSetters(type, typeCfg);
CompleteReferenceResolver(typeCfg);
}
}

private void CompleteExternalFieldSetters(ObjectType type, ObjectTypeDefinition typeDef)
=> ExternalSetterExpressionHelper.TryAddExternalSetter(type, typeDef);
private void CompleteExternalFieldSetters(ObjectType type, ObjectTypeConfiguration typeCfg)
=> ExternalSetterExpressionHelper.TryAddExternalSetter(type, typeCfg);

private void CompleteReferenceResolver(ObjectTypeDefinition typeDef)
private void CompleteReferenceResolver(ObjectTypeConfiguration typeCfg)
{
IReadOnlyList<ReferenceResolverDefinition> resolvers;
{
var contextData = typeDef.GetContextData();
var contextData = typeCfg.GetContextData();

if (!contextData.TryGetValue(EntityResolver, out var resolversObject))
{
Expand All @@ -374,7 +374,7 @@ private void CompleteReferenceResolver(ObjectTypeDefinition typeDef)

if (resolvers.Count == 1)
{
typeDef.ContextData[EntityResolver] = resolvers[0].Resolver;
typeCfg.ContextData[EntityResolver] = resolvers[0].Resolver;
}
else
{
Expand Down Expand Up @@ -404,38 +404,38 @@ private void CompleteReferenceResolver(ObjectTypeDefinition typeDef)

current = Expression.Block(new[] { variable, }, current, variable);

typeDef.ContextData[EntityResolver] =
typeCfg.ContextData[EntityResolver] =
Expression.Lambda<FieldResolverDelegate>(current, context).Compile();
}
}

private void AddServiceTypeToQueryType(
ITypeCompletionContext completionContext,
DefinitionBase? definition)
TypeSystemConfiguration? definition)
{
if (!ReferenceEquals(completionContext.Type, _queryType))
{
return;
}

var objectTypeDefinition = (ObjectTypeDefinition)definition!;
objectTypeDefinition.Fields.Add(ServerFields.CreateServiceField(_context));
var objectTypeCfg = (ObjectTypeConfiguration)definition!;
objectTypeCfg.Fields.Add(ServerFields.CreateServiceField(_context));
if (_entityTypes.Count > 0)
{
objectTypeDefinition.Fields.Add(ServerFields.CreateEntitiesField(_context));
objectTypeCfg.Fields.Add(ServerFields.CreateEntitiesField(_context));
}
}

private void ApplyMethodLevelReferenceResolvers(
ObjectType objectType,
ObjectTypeDefinition objectTypeDefinition)
ObjectTypeConfiguration objectTypeCfg)
{
if (objectType.RuntimeType == typeof(object))
{
return;
}

var descriptor = ObjectTypeDescriptor.From(_context, objectTypeDefinition);
var descriptor = ObjectTypeDescriptor.From(_context, objectTypeCfg);

// Static methods won't end up in the schema as fields.
// The default initialization system only considers instance methods,
Expand All @@ -460,21 +460,21 @@ private void ApplyMethodLevelReferenceResolvers(
}

// This seems to re-detect the entity resolver and save it into the context data.
descriptor.CreateDefinition();
descriptor.CreateConfiguration();
}

private void AddToUnionIfHasTypeLevelKeyDirective(
ObjectType objectType,
ObjectTypeDefinition objectTypeDefinition)
ObjectTypeConfiguration objectTypeCfg)
{
if (objectTypeDefinition.Directives.FirstOrDefault(d => d.Value is KeyDirective) is { } keyDirective &&
if (objectTypeCfg.Directives.FirstOrDefault(d => d.Value is KeyDirective) is { } keyDirective &&
((KeyDirective)keyDirective.Value).Resolvable)
{
_entityTypes.Add(objectType);
return;
}

if (objectTypeDefinition.Fields.Any(f => f.ContextData.TryGetValue(KeyMarker, out var resolvable) &&
if (objectTypeCfg.Fields.Any(f => f.ContextData.TryGetValue(KeyMarker, out var resolvable) &&
resolvable is true))
{
_entityTypes.Add(objectType);
Expand All @@ -483,21 +483,21 @@ private void AddToUnionIfHasTypeLevelKeyDirective(

private void AggregatePropertyLevelKeyDirectives(
ObjectType objectType,
ObjectTypeDefinition objectTypeDefinition,
ObjectTypeConfiguration objectTypeCfg,
ITypeDiscoveryContext discoveryContext)
{
// if we find key markers on our fields, we need to construct the key directive
// from the annotated fields.
{
var foundMarkers = objectTypeDefinition.Fields.Any(f => f.ContextData.ContainsKey(KeyMarker));
var foundMarkers = objectTypeCfg.Fields.Any(f => f.ContextData.ContainsKey(KeyMarker));

if (!foundMarkers)
{
return;
}
}

IReadOnlyList<ObjectFieldDefinition> fields = objectTypeDefinition.Fields;
IReadOnlyList<ObjectFieldConfiguration> fields = objectTypeCfg.Fields;
var fieldSet = new StringBuilder();
bool? resolvable = null;

Expand Down Expand Up @@ -525,11 +525,11 @@ private void AggregatePropertyLevelKeyDirectives(
}

// add the key directive with the dynamically generated field set.
AddKeyDirective(objectTypeDefinition, fieldSet.ToString(), resolvable ?? true);
AddKeyDirective(objectTypeCfg, fieldSet.ToString(), resolvable ?? true);

// register dependency to the key directive so that it is completed before
// we complete this type.
foreach (var directiveDefinition in objectTypeDefinition.Directives)
foreach (var directiveDefinition in objectTypeCfg.Directives)
{
discoveryContext.Dependencies.Add(
new TypeDependency(
Expand All @@ -549,25 +549,25 @@ private void AggregatePropertyLevelKeyDirectives(

private void AddMemberTypesToTheEntityUnionType(
ITypeCompletionContext completionContext,
DefinitionBase? definition)
TypeSystemConfiguration? definition)
{
if (completionContext.Type is _EntityType &&
definition is UnionTypeDefinition unionTypeDefinition)
definition is UnionTypeConfiguration unionTypeCfg)
{
foreach (var objectType in _entityTypes)
{
unionTypeDefinition.Types.Add(TypeReference.Create(objectType));
unionTypeCfg.Types.Add(TypeReference.Create(objectType));
}
}
}

private void AddKeyDirective(
ObjectTypeDefinition objectTypeDefinition,
ObjectTypeConfiguration objectTypeCfg,
string fieldSet,
bool resolvable)
{
objectTypeDefinition.Directives.Add(
new DirectiveDefinition(
objectTypeCfg.Directives.Add(
new DirectiveConfiguration(
new KeyDirective(fieldSet, resolvable),
_keyDirectiveReference));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ private static bool TryGetValue<T>(
int i,
out T? value)
{
type = type is NonNullType nonNullType ? nonNullType.Type : type;
type = type is NonNullType nonNullType ? nonNullType.NullableType : type;
switch (valueNode.Kind)
{
case SyntaxKind.ObjectValue:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ internal static class ExternalSetterExpressionHelper
private static readonly ParameterExpression _data = Parameter(typeof(IValueNode), "data");
private static readonly ParameterExpression _entity = Parameter(typeof(object), "entity");

public static void TryAddExternalSetter(ObjectType type, ObjectTypeDefinition typeDef)
public static void TryAddExternalSetter(ObjectType type, ObjectTypeConfiguration typeDef)
{
List<Expression>? block = null;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace HotChocolate.ApolloFederation.Types;
/// <summary>
/// The entity definition allows to specify a reference resolver.
/// </summary>
public sealed class EntityResolverDefinition : DefinitionBase
public sealed class EntityResolverDefinition : TypeSystemConfiguration
{
/// <summary>
/// The runtime type of the entity.
Expand Down
Loading
Loading