Home > Software design >  ChilliCream Hot Chocolate - Any Way To Extend Object Type On A Method Level (Instead Of On A Class L
ChilliCream Hot Chocolate - Any Way To Extend Object Type On A Method Level (Instead Of On A Class L

Time:07-15

ChilliCream Hot Chocolate only allows the registering of one type for each graph QL operation on startup e.g. for the Query operation:

public class Startup
{
    public void ConfigureServices(IServiceCollection services)
    {
        services
            .AddGraphQLServer()
            .AddQueryType<Query>();
    }
}

You may want have many possible query operations and would not want to cram them all into one class.

For good code organisation, the ExtendObjectType attribute to can be used to extend the content in the registered classes (more info here: https://chillicream.com/docs/hotchocolate/defining-a-schema/extending-types) but it currently seems to only work on a class level i.e. the attribute is used to decorate a class and then whole content of the class is used to extend the specified type.

This is not ideal, as you may already have an existing API class that has both "get" and "set" methods. You would want to include the "get" methods for the query operations but not the "set" methods (which would be included with the mutation operation).

Is there anyway of using ExtendObjectType on a method level or achieving the same thing by some other means? I know you could just create proxy classes but I'm looing for a more elegant solution.

CodePudding user response:

I have done a youtube video on this which explains what ExtendObjectType can do. ExtendObjectType can extend multiple object types at once. and you can target with each method in it a different target type.

watch here: https://www.youtube.com/watch?v=EHTr4Fq6GlA

But what it cannot due is slice a class into. When we designed it we imagined a different use-case. Where you can extend existing APIs with additional GraphQL specific resolvers.

But, what you could use is the fluent API which allows you to pick just the methods that you want.

The fluent API is also explained in the video.

public class QueryExtension : ObjectTypeExtension
{
    protected override void Configure(IObjectTypeDescriptor descriptor)
    {
        descriptor.Name("Query");
        descriptor.Field<SomeClass>(t => t.SomeMethod());
    }
}

CodePudding user response:

It seems like what I really want cannot currently be done. The best solution is to make a proxy class that calls the methods that I want to include and the use the ExtendObjectTypes attribute on the proxy class.

  • Related