将 GraphQL 从 .NET core 2.2 升级到 3.0

Upgrade GraphQL from .NET core 2.2 to 3.0

当我尝试将 .net 核心版本从 2.2 升级到 3.0 时,我是 GraphQL 的新手

我在使用 UseGraphiQl 时遇到关于 UI 在 /graphql 页面上显示的问题

API 正常,但 UI 显示不正确。 我在谷歌上搜索了解决方案,但没有什么真正有用的。

这是我的 graphql 配置:

services.AddRazorPages().SetCompatibilityVersion(CompatibilityVersion.Version_3_0);

app.UseGraphiQLServer(new GraphiQLOptions());
app.UseGraphiQl("/graphiql", "/graphql");
app.UseEndpoints(x =>
{
    x.MapControllers();
});

非常感谢任何帮助,谢谢。

我不确定他们是否在 .net core 3.0 版中进行了任何更改,但您可以查看我的博客 here

我正在使用 GraphQL.Server.Ui.Playground

下面是你可以看到的最小配置

public void ConfigureServices(IServiceCollection services)
{
    services
        .AddMvc()
        .AddJsonOptions(
            options => options.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore
        )
        .SetCompatibilityVersion(CompatibilityVersion.Version_2_2);

    services.AddGraphQL(x =>
    {
        x.ExposeExceptions = true; //set true only in development mode. make it switchable.
    })
    .AddGraphTypes(ServiceLifetime.Scoped);
}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env, Seeder seeder)
{
    app.UseGraphQL<DataSchema>();
    app.UseGraphQLPlayground(new GraphQLPlaygroundOptions());

    app.UseMvc(routes =>
    {
        routes.MapRoute(
            name: "default",
            template: "{controller}/{action=Index}/{id?}");
    });
}

结果与GraphiQl相同

编辑:这是因为 Newtonsoft.Json 是 .Net Core 3 中的更改。您可以在此处查看我的回答

终于找到解决办法了:

services.AddRazorPages().AddNewtonsoftJson();

作为改进 ASP.NET 核心共享框架工作的一部分,Json.NET 已从 ASP.NET 核心共享框架中删除。

要在 ASP.NET Core 3.0 项目中使用 Json.NET:

  • 添加对 Microsoft.AspNetCore.Mvc.NewtonsoftJson 的包引用。

  • 更新Startup.ConfigureServices调用AddNewtonsoftJson。

参考:https://docs.microsoft.com/en-us/aspnet/core/migration/22-to-30?view=aspnetcore-3.0&tabs=visual-studio#jsonnet-support