I am new to GraphQL. Whenever I am running my project it shows
server is not reachable
I have crosschecked project files and I guess the issue is with the Startup.cs
file. I am trying to use app.UseGraphQL
in Configure function but the IDE could not suggest me a correct library for it.
This is my code:
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!");
});
});
}
}
}
For full code: GitHub
CodePudding user response:
Can you modify your code like this.
app.UseEndpoints(endpoints =>
{
endpoints.MapGet("/", async context =>
{
await context.Response.WriteAsync("Hello World!");
});
endpoints.MapGraphQL();
});
I don't think you need app.UseGraphQL("/api");
You can browse your GraphQL client - https://localhost:5001/graphql/