GraphQL Documentation Explorer not showing in browser .net core 3.1

女生的网名这么多〃 提交于 2021-01-24 09:36:32

问题


i am new to GraphQL, i have build a sample project using GraphQL which is working fine, but 'Documentation Explore' (my custom schema) not loaded in Browser .net core 3.1 also attached StartUp.cs. note : Which was works in .net core 2.0.

here is 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();
        }

    }
}

Here is for your reference


回答1:


Do your query results start with "data"? If not then this may be the problem. The Documentation Explorer (it's a React app) expects the results of its metadata query to start with "data".

I added it with the following code in the GraphQLController and then the documentation appeared.

[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);
}


来源:https://stackoverflow.com/questions/61224442/graphql-documentation-explorer-not-showing-in-browser-net-core-3-1

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!