Search code examples
asp.netgraphqlhotchocolate

What is the Best Practice and Efficient way to organize Query, Mutation and Subscription classes in Hotchocolate GraphQL


which one is best and efficient way to organize query, mutation and subscription class?

  1. Partial class
  2. [ExtendObjectType(OperationTypeNames.Query)]
  3. others?

And what is the difference, what is happening behind the scenes?

In this official docs If we just want to organize the fields of one of our types in different files, we can use partial classes in the Annotation-based approach.

But in workshop ExtendObjectType is used

I have also asked the same question here


Solution

  • With partial classes nothing special is happening behind the scenes as it's just a regular C# feature. Its downsides are that they can not be used across assemblies and you can't replace or remove fields that where defined in another partial class.

    Type extensions on the other hand are being merged with the original type definition by Hot Chocolate. They work across assemblies and allow you to hide or replace members of the original type definition.

    As you've quoted from the documentation, partial classes are easier to use if you just want to split your type definitions into different files within the same project. For a more Hot Chocolate integrated way of combining these multiple files (classes) Type Extensions should be used.