将GraphQL从.NET Core 2.2升级到3.0

时间:2019-11-25 03:37:14

标签: c# asp.net-core graphql asp.net-core-3.0 graphiql

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

使用UseGraphiQl时,有关/ graphql页面上的UI显示的问题

enter image description here

API正常运行,但UI显示不正确。 我用Google搜索了解决方案,但没有什么真正有用的。

这是我对graphql的配置:

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

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

非常感谢您的帮助。

2 个答案:

答案 0 :(得分:4)

最后,我找到了解决方案:

services.AddRazorPages().AddNewtonsoftJson();

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

要在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

答案 1 :(得分:2)

我不确定他们是否在.net核心版本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相同

enter image description here

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

ASP.NET Core 3.0 [FromBody] string content returns "The JSON value could not be converted to System.String."

相关问题