ChilliCream / hotchocolate-examples

MIT License
187 stars 137 forks source link

Configure is never called for ObjectTypeExtension instances provided in ITypeModule.CreateTypesAsync #38

Closed DaveRMaltby closed 2 years ago

DaveRMaltby commented 2 years ago

If ObjectTypeExtension.Configure() is overridden or if an Action<IObjectTypeDescriptor> is provided to the ObjectTypeExtension ctor, neither is ever called. For example, in my dynamic schema, I am adding an ObjectFieldDefinition to my query. But in order to provide this to the ITypeModule.CreateTypesAsync, it must be added to the ObjectTypeDefinition.Fields query extension and converted into a ObjectTypeExtension like so:

ObjectTypeExtension.CreateUnsafe(typeDefinition)

This was all working fine until I wanted to add paging, sorting and filtering to my dynamically added schema field queries. After much pouring through the Hot Chocolate code, I have ultimately determined that I should be able to inject these facilities, by deriving a class off of ObjectTypeExtension like so: (The important call below is the fieldDescriptor.UseSorting()):

private class ObjectTypeExtensionEx : ObjectTypeExtension
    {
        public ObjectTypeExtensionEx()
            : base()
        {
        }

        protected override void Configure(IObjectTypeDescriptor descriptor)
        {
            var fieldDescriptor = descriptor as ObjectFieldDescriptor;
            fieldDescriptor.UseSorting(null);
            base.Configure(descriptor);
        }

        public static ObjectTypeExtensionEx CreateUnsafe(ObjectTypeDefinition definition)
            => new() { Definition = definition };
    }

and then call the CreateUnsafe static method on this derived class as the type extension to return for ITypeModule.CreateTypesAsync().
Unfortunately, in my testing the overridden ObjectTypeExtensionEx.Configure() never gets called.
Maybe this approach wasn't intended to be supported though. If not, is there another way that dynamic schemas can utilize Hot Chocolate's built in facilities for paging, sorting and filtering? Thanks! Note: I'm happy to contribute a PR, but would need some guidance as this is a complicated area of the Hot Chocolate code base.

DaveRMaltby commented 2 years ago

I've mistakenly created this issue in this repo instead of the main repository. I'll copy it over there and close this one.

DaveRMaltby commented 2 years ago

Created issue in the main repository. https://github.com/ChilliCream/hotchocolate/issues/4867