首页 > 解决方案 > 没有可用的架构 graphiql aspnet 核心

问题描述

我在https://github.com/nmarun/customergraph有我的仓库。当我运行应用程序时,我在文档资源管理器中看不到任何架构。当我查看“网络”选项卡时,我看到了 404。

网络标签中的 404

我想我需要将 GraphiQl 配置为调用 /graphql 端点来检索架构详细信息,并且由于某种原因,我的 HTTP POST 操作方法没有在 /graph 端点处受到打击。

当我到达端点时,所有通过邮递员的调用都可以正常工作:http://localhost:54068/graphql

请协助。

using CustomerGraph.Models.Schema;
using CustomerGraph.Models.Services;
using GraphiQl;
using GraphQL;
using GraphQL.Server;
using GraphQL.Types;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;

namespace CustomerGraph
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
            services.AddScoped<IDocumentExecuter, DocumentExecuter>();
            services.AddSingleton<ICustomerService, CustomerService>();
            services.AddSingleton<CustomerType>();
            services.AddSingleton<AddressType>();
            services.AddSingleton<ContactType>();
            services.AddSingleton<ContactMethodType>();
            services.AddSingleton<CustomersQuery>();
            services.AddSingleton<CustomerSchema>();
            services.AddSingleton<IDependencyResolver>(
                c => new FuncDependencyResolver(type => c.GetRequiredService(type)));

            services.AddGraphQL(_ =>
            {
                _.EnableMetrics = true;
                _.ExposeExceptions = true;
            });

            var sp = services.BuildServiceProvider();
            services.AddSingleton<ISchema>(new CustomerSchema(new FuncDependencyResolver(type => sp.GetService(type))));

        }

        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseGraphiQl("/graphiql");
            app.UseGraphQL<ISchema>("/graphql");
            app.UseGraphQLWebSockets<CustomerSchema>("/graphql");
            app.UseWebSockets();
            app.UseHttpsRedirection();
            app.UseMvc();
        }
    }
}

谢谢,阿伦

标签: asp.net-coregraphqlgraphiql

解决方案


这是由 UseGraphIQL 方法引起的,它假定 graphql 端点与 GraphQL api 位于同一位置。

通过替换以下内容来修复它:

   app.UseGraphiQl("/graphiql");
            //app.UseGraphQL<CustomerSchema>("/graph");
            //app.UseGraphQLWebSockets<CustomerSchema>("/graphql");
            //app.UseWebSockets();
            app.UseHttpsRedirection();
            app.UseMvc();

经过:

   app.UseGraphiQl("/graphiql", "/graphql");
            app.UseGraphQL<CustomerSchema>("/graphql");
            app.UseGraphQLWebSockets<CustomerSchema>("/graphql");
            app.UseWebSockets();
            app.UseHttpsRedirection();
            app.UseMvc();

推荐阅读