I am trying to access the directives of each field’s type of intercepted types using HotChocolate v14.
Why am I getting TypeInitializationException when trying to access ObjectType’s Directives in my TypeInterceptor.OnAfterCompleteName?
I also tried OnAfterCompleteType but the same happens.
internal sealed class ZajilTypeInterceptor : TypeInterceptor
{
public override void OnAfterCompleteName(ITypeCompletionContext cc, DefinitionBase td)
{
if (!cc.IsIntrospectionType && td is ObjectTypeDefinition otd)
{
foreach (var f in otd.Fields)
{
if (!f.IsIntrospectionField)
{
var ft = cc.GetType<IType>(f.Type!);
var ds = ft switch
{
IObjectType fot => fot.Directives, // TypeInitializationException is thrown here.
NonNullType fnnt
=> fnnt.InnerType() is IObjectType fot ? fot.Directives : null, // TypeInitializationException is thrown here.
_ => null
};
var ad = ds?.Where(d => d.Type.Name == "aggregate").SingleOrDefault();
if (ad is not null)
{
f.PureResolver = _ => new Dictionary<string, object?> { { "id", "test" } };
}
else
{
f.PureResolver = rc => rc.Parent<IDictionary<string, object?>>()[f.Name];
}
}
}
}
}
}
Here is the test source with test schema also:
using HotChocolate;
using HotChocolate.Execution;
using Xunit;
using Zajil;
namespace LibraryTests;
public sealed class ExtensionsTests
{
[Fact]
public void AddZajil_AddsFieldResolvers()
{
var sd = """
directive @aggregate on OBJECT
type Query {
test: Test
}
type Test @aggregate {
id: String!
}
""";
var s = SchemaBuilder.New().AddDocumentFromString(sd).AddZajil().Create();
}
}