GraphQL 文档资源管理器未在浏览器 .net core 3.1 中显示 [英] GraphQL Documentation Explorer not showing in browser .net core 3.1

查看:14
本文介绍了GraphQL 文档资源管理器未在浏览器 .net core 3.1 中显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 GraphQL 的新手,我已经使用 GraphQL 构建了一个运行良好的示例项目,但是'文档探索'(我的自定义架构)未加载到浏览器 .net core 3.1 中还附加 <强>StartUp.cs.注意:这在 .net core 2.0 中有效.

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.

这里是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();
        }

    }
}

这里供大家参考

推荐答案

你的查询结果是否以data"开头?如果不是,那么这可能是问题所在.文档浏览器(它是一个 React 应用程序)期望其元数据查询的结果以数据"开头.

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".

我在 GraphQLController 中添加了以下代码,然后出现了文档.

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

这篇关于GraphQL 文档资源管理器未在浏览器 .net core 3.1 中显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆