Upgrade GraphQL from .NET core 2.2 to 3.0
Asked Answered
M

2

6

I am new to GraphQL, when I try to upgrade .net core version from 2.2 to 3.0

I got problem about UI display on /graphql page when using UseGraphiQl

enter image description here

API is working normally but the UI is display incorrect. I googled for find out solutions, but nothing really helpful.

Here is my config for graphql:

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

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

Any help is greatly appreciated, thanks.

Manikin answered 25/11, 2019 at 3:37 Comment(0)
M
5

Finally, I find out the solution:

services.AddRazorPages().AddNewtonsoftJson();

As part of the work to improve the ASP.NET Core shared framework, Json.NET has been removed from the ASP.NET Core shared framework.

To use Json.NET in an ASP.NET Core 3.0 project:

  • Add a package reference to Microsoft.AspNetCore.Mvc.NewtonsoftJson.

  • Update Startup.ConfigureServices to call AddNewtonsoftJson.

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

Manikin answered 26/11, 2019 at 2:10 Comment(0)
D
2

I'm not sure if they are changing anything in .net core version 3.0 but you can view my blog here

I'm using GraphQL.Server.Ui.Playground

Below is minial config you can see

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?}");
    });
}

The result is the same with GraphiQl

enter image description here

Edit: This is because Newtonsoft.Json is change in .Net Core 3. You can view my answer here

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

Desjardins answered 25/11, 2019 at 4:56 Comment(2)
Thanks for your response but my application is totally ok in v2.2.Manikin
I am in latest version already 1.2 for graphiql and 3.4.0 for GraphQL.Server.Ui.GraphiQLManikin

© 2022 - 2024 — McMap. All rights reserved.