HotChocolate GraphQL Automatic Type Registration

Josh Dunn

Are you sick of having to manually register your type extensions in HotChocolate GraphQL Server? Well good news! As of the release of HotChocolate 13, there is now a way to setup automatic type extension registration with only a few lines.

hero background

What is Type Registration?

If you're familiar with HotChocolate GraphQL then you're probably familiar with the pattern of defining a base query and mutation type and then using type extensions to define each of your queries and mutations. A basic implementation of this would look like.

public class Query 
{
}

public class Mutation 
{
}

[ExtendTypeObject<Query>]
public class FetchSomethingQuery 
{
    // Code omitted for brevity.
}

[ExtendTypeObject<Mutation>]
public class DoSomethingMutation 
{
    // Code omitted for brevity.
}

Once your base classes and any queries/mutations are defined, they then need to be added to your Startup.cs class so that HotChocolate knows about them.

public class Startup 
{
    public void ConfigureServices(IServiceCollection services) 
    {
        services.AddGraphQLServer()
            .AddQueryType<Query>()
            .AddMutationType<Mutation>()
            .AddTypeExtension<DoSomethingMutation>()
            .AddTypeExtension<FetchSomethingQuery>();
            
        // Other setup code here...
    }
}

And that's it, pretty easy to setup and add new queries or mutations however this approach doesn't scale well as larger projects will see this .AddTypeExtension() call chain grow and become harder to manage - especially in larger teams.

So, how can we make it better?

upgrades, people. Upgrades

The ChilliCream team recognized that this isn't an ideal solution so they implemented the ability to let HotChocolate handle the type registration for you which drastically cuts down on the amount of effort required and helps reduce merge conflicts as multiple people are not modifying the Startup.cs file each time they add a new query or mutation.

To do so, there's a few steps you need to follow.

Add HotChocolate.Types.Analyzers Package

Using your preferred package manager, add the HotChocolate.Types.Analyzers package and make sure it is the same version as the version of HotChocolate you are using.

Add Module to ModuleInfo.cs File

Add a Properties/ModuleInfo.cs file with the line [assembly: HotChocolate.Module("Types")]

Update Queries & Mutations

Because we're relying on HotChocolate to handle the type registration, we no longer need to be extending a base Query or Mutation class, so the attributes for our queries and mutations need to change slightly.

Instead of using [ExtendTypeObject<Query>] or [ExtendTypeObject<Mutation>] we can now just use the [QueryType] or [MutationType] attribute as required.

This means our code will now look like:

[QueryType]
public class FetchSomethingQuery 
{
    // Code omitted for brevity.
}

[MutationType]
public class DoSomethingMutation 
{
    // Code omitted for brevity.
}

Update Startup.cs

Now that we have everything else setup, the last step is to update the Startup.cs file to remove the manual type registrations and use AddTypes() instead. We can also remove the AddQueryType<Query>() and AddMutationType<Mutation>() lines as they're no longer required.

public class Startup 
{
    public void ConfigureServices(IServiceCollection services) 
    {
        services.AddGraphQLServer()
            .AddTypes();
            
        // Other setup code here...
    }
}

Once updated, rebuild the solution and rejoice!

It's also worth noting that HotChocolate requires at least one class for the auto registration to work, otherwise it will throw an error.