Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't figure out is it possible to use multiple schemas in Hot Chocolate for ASP.NET Core

I'm trying to start developing GraphQL API with Hot Chocolate library on ASP.NET Core but I can't figure out how to use different schemas for different endpoints. I know about schema stitching but it's not what I'm looking for. What I would like to implement, it is to be able to query different types from different endpoints, for example, I want to query user data from localhost:5000/graphapi and to query different admin data from localhost:5000/admin/graphapi Yes, it is possible to create to separate servers for this but I would like to have monolith API.

like image 352
Kolia Ukrainec Avatar asked Mar 28 '21 13:03

Kolia Ukrainec


1 Answers

This is quite simple,

first setup your GraphQL configurations for your schemas:

public void ConfigureServices(IServiceCollection services)
{
    services
        .AddRouting()

    services
        .AddGraphQLServer()
        .AddQueryType<Query>()
        .AddMutationType<Mutation>();

    services
        .AddGraphQLServer("adminSchema")
        .AddQueryType<QueryAdmin>()
        .AddMutationType<MutationAdmin>();
}

Next we need to configure the mapping of the schemas to concrete routes:

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    app
        .UseRouting()
        .UseEndpoints(endpoints =>
        {
            endpoints.MapGraphQL();
            endpoints.MapGraphQL("/admin/graphql", schemaName: "adminSchema");
        });
}

done.

like image 93
Pascal Senn Avatar answered Nov 08 '22 11:11

Pascal Senn