无法弄清楚是否可以在 Hot Chocolate 中为 ASP.NET Core 使用多个架构

问题描述

我正在尝试开始在 ASP.NET Core 上使用 Hot Chocolate 库开发 GraphQL API,但我不知道如何为不同的端点使用不同的架构。我知道模式拼接,但这不是我要找的。 我想实现的是能够从不同的端点查询不同的类型,例如,我想从 localhost:5000/graphapi 查询用户数据和从 localhost:5000/admin/graphapi 查询不同的管理数据 是的,可以为此创建单独的服务器,但我想要整体 API。

解决方法

这很简单,

首先为您的架构设置 GraphQL 配置:

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

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

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

接下来我们需要配置模式到具体路由的映射:

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

完成。

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...