Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple Query Type in Graphql Hotchocolate

I am using hot chocolate graphql. I have a scenario where I have two separate query type classes.

  1. PostQuery -> contains post related queries
  2. UserQuery -> contains user related queries

My Folder Structure

enter image description here

Here it is how I am configuring it

 .AddAuthorization()
    //for inmemory subscription
    .AddInMemorySubscriptions()
    .AddQueryType<PostQuery>()
    .AddQueryType<UserQuery>()
    .AddMutationType<Mutation>()
    .AddSubscriptionType<Subscription>()
    .AddGlobalObjectIdentification()
    // Registers the filter convention of MongoDB
    .AddMongoDbFiltering()
    // Registers the sorting convention of MongoDB
    .AddMongoDbSorting()
    // Registers the projection convention of MongoDB
    .AddMongoDbProjections()
    // Registers the paging providers of MongoDB
    .AddMongoDbPagingProviders();

However, i am getting the following error

System.ArgumentException: The root type `Query` has already been registered

Is there anyway it can be configured or else I have to places everything in a single class?

like image 408
shujaat siddiqui Avatar asked Mar 20 '26 14:03

shujaat siddiqui


1 Answers

You need to register the querytype "Query" and add resolvers to handle multiple schemas of type "Query"

builder.Services
.AddQueryType(q => q.Name("Query"))
.AddType<PostQuery>()
.AddType<UserQuery>()

And in your query classes:

[ExtendObjectType("Query")]
public class PostQuery 
{
    public List<Post> GetAllPosts()
    {
        return List<Post>{...};
    }
}

[ExtendObjectType("Query")]
public class UserQuery
{
    public List<User> GetAllUsers()
    {
        return List<User>{...};
    }
}
like image 60
sjokkogutten Avatar answered Mar 23 '26 03:03

sjokkogutten



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!