gpt4 book ai didi

asp.net-core - 为什么 "IApplicationBuilder"找不到 "UseGraphQL"的定义?

转载 作者:行者123 更新时间:2023-12-02 18:24:18 25 4
gpt4 key购买 nike

我是 GraphQL 新手。每当我运行我的项目时,它都会显示

server is not reachable

我交叉检查了项目文件,我猜问题出在 Startup.cs 文件上。我尝试在配置函数中使用 app.UseGraphQL 但 IDE 无法建议我使用正确的库。

这是我的代码:

using GraphQLMutationBasicCRUD.IService;
using GraphQLMutationBasicCRUD.Service;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using HotChocolate;
using GraphQLMutationBasicCRUD.GraphQL;
using HotChocolate.AspNetCore;
using HotChocolate.AspNetCore.Playground;
using Microsoft.AspNetCore.Http;
namespace GraphQLMutationBasicCRUD
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}

public IConfiguration Configuration { get; }

// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddSingleton<IGroupService, GroupService>();
services.AddSingleton<IStudentService, StudentService>();

services.AddGraphQL(x=> SchemaBuilder.New()
.AddServices(x)
.AddType<GroupType>()
.AddType<StudentType>()
.AddQueryType<Query>()
.AddMutationType<Mutation>()
.Create()
);
}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UsePlayground(new PlaygroundOptions
{
QueryPath = "/api",
Path = "/Playground"
});
}

app.UseGraphQL("/ api");
app.UseRouting();

app.UseEndpoints(endpoints =>
{
endpoints.MapGet("/", async context =>
{
await context.Response.WriteAsync("Hello World!");
});
});
}
}
}

完整代码: GitHub

最佳答案

你能像这样修改你的代码吗?

app.UseEndpoints(endpoints =>
{
endpoints.MapGet("/", async context =>
{
await context.Response.WriteAsync("Hello World!");
});
endpoints.MapGraphQL();
});

我认为您不需要 app.UseGraphQL("/api");

您可以浏览 GraphQL 客户端 - https://localhost:5001/graphql/

关于asp.net-core - 为什么 "IApplicationBuilder"找不到 "UseGraphQL"的定义?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70430822/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com