Обозреватель документации GraphQL не отображается в браузере .net core 3.1

Я новичок в GraphQL, у меня есть образец проекта с использованием GraphQL, который работает нормально, но 'Documentation Explore' (моя настраиваемая схема) не загружен в Browser .net core 3.1, также прилагается StartUp .cs. примечание: что было работает в .net core 2.0.

вот startup.cs

using GraphiQl;
using GraphQL;
using GraphQL.Server;
using GraphQL.Types;
{
    public class Startup
    {

        public void ConfigureServices(IServiceCollection services)
        {
            services.Configure<IISServerOptions>(options =>
            {
                options.AllowSynchronousIO = true;
            });

            services.AddSingleton<IDependencyResolver>(c => new FuncDependencyResolver(type => c.GetRequiredService(type)));

            services.AddDbContext<RealEstateContext>(options => options.UseSqlServer(Configuration["ConnectionStrings:RealEstateDb"]));
            services.AddScoped<IDocumentExecuter, DocumentExecuter>();
            services.AddScoped<PropertyQuery>();
            services.AddScoped<PropertyMutation>();
            services.AddScoped<PropertyType>();
            services.AddScoped<ConstituencyType>();
            services.AddScoped<PropertyInputType>();
            services.AddScoped<PaymentType>();
                    services.AddGraphQL(options =>
            {
                options.EnableMetrics = true;
                options.ExposeExceptions = true;
            }).AddWebSockets();
        }

        public void Configure(IApplicationBuilder app, IWebHostEnvironment env,RealEstateContext db)
        {
            app.UseWebSockets();
            app.UseGraphQLWebSockets<RealEstateSchema>("/graphql");
            app.UseGraphQL<RealEstateSchema>("/graphql");
            db.EnsureSeedData();
        }

    }
}

Вот для справки


person Karunakaran    schedule 15.04.2020    source источник


Ответы (1)


Результаты вашего запроса начинаются с данных? Если нет, то это может быть проблемой. Обозреватель документации (это приложение React) ожидает, что результаты его запроса метаданных будут начинаться с данных.

Я добавил его с помощью следующего кода в GraphQLController, а затем появилась документация.

[Route("/graphql")]
[HttpPost]
public async Task<IActionResult> Post([FromBody] GraphQLQueryDTO query)
{
    var result = await _executer.ExecuteAsync(_ =>
    {
        _.Schema = _schema;
        _.Query = query.Query;
        _.Inputs = query.Variables?.ToInputs();

    });

    if (result.Errors?.Count > 0) {
        return BadRequest(result.Errors[0].ToString());
    }

    // The response json is not starting with "data" - maybe to look prettier
    // This issue with this is that when GraphiQL requests the schema, GraphiQL
    // is expecting a response that starts "data". So I am adding "data" to the
    // front of the response if this is a schema request.  
    if (typeof(Dictionary<string, object>).IsInstanceOfType(result.Data)) {
        var dictData = (Dictionary<string, object>)result.Data;
        if (dictData.ContainsKey("__schema")) {
            var result2 = new Dictionary<string, object>();
            result2.Add("data", dictData);
            return Ok(result2);
        } else { 
            return Ok(result.Data);
        }
    }
    return Ok(result);
}
person Nigel Maddocks    schedule 18.12.2020