将GraphQL从.NET Core 2.2升级到3.0

6

我对GraphQL还不熟悉,当我试图将.NET Core版本从2.2升级到3.0时,

在使用UseGraphiQL时,在/graphql页面上遇到了UI显示问题。

enter image description here

API正常工作,但UI显示不正确。我尝试通过谷歌搜索解决方案,但没有找到真正有用的。

以下是我的GraphQL配置:

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

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

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

2个回答

5

2

我不确定他们是否会在 .net core 3.0 版本中有所更改,但你可以查看我的博客这里

我正在使用 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]字符串内容返回"The JSON value could not be converted to System.String."

1
谢谢您的回复,但我的应用程序在v2.2中完全正常。 - Tan Sang
1
我已经使用最新版本的GraphiQL 1.2和GraphQL.Server.Ui.GraphiQL 3.4.0。 - Tan Sang

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接