8

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.

1 Answers1

16

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.

Pascal Senn
  • 1,712
  • 11
  • 20
  • 3
    Have you found that Banana Cake Pop doesn't switch between the two schemas correctly unless you clear the browser cache? Or is there a setting or something for this? – Rei Miyasaka Aug 15 '21 at 00:07