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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With