I am learning GraphQL
with .Net Core
, Entity Framework Core
and C#
. So far, what I have learned is, if I use ISchema
then it is very easy to integrate such IDEs. I have successfully integrated these IDEs, GraphiQL & GraphQL.Server.Ui.Playground.
Now when I started integrated Hot Chocolate, which comes with its own IDE, and they recommend using that instead of HotChocolate.AspNetCore.Playground, I noticed that, it makes the implementation so much easier. Following is the comparison for IShcema
Vs Hot Chocolate
implementation.
In ISchema
, I have to define Type
, Query
and Service
and register them Program.cs
In Hot Chocolate
, I just need Query
and Service
and register them Program.cs
where query implementation is very easy and just few lines of code to register them.
Questions:
- Is it possible to integrate such IDEs with
Hot Chocolate
? Because I am getting404
for both IDEs when I integrate them on top ofHot Chocolate
. (Tried them separately). - What are the benefits of using that vs default
Hot Chocolate
?
ISchema:
Program.cs
using GraphiQl;
using GraphQL.Server;
using GraphQL.Types;
using GraphQlProject.Interfaces;
using GraphQlProject.Mutations;
using GraphQlProject.Query;
using GraphQlProject.Schema;
using GraphQlProject.Services;
using GraphQlProject.Type;
using Microsoft.Azure.Cosmos;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllers();
builder.Services.AddTransient<IUser, UserService>();
// Register GraphQL type, query and schema
builder.Services.AddSingleton<UserType>();
builder.Services.AddSingleton<ContactType>();
builder.Services.AddSingleton<UserQuery>();
builder.Services.AddSingleton<ISchema, UserSchema>();
builder.Services.AddGraphQL(x => x.EnableMetrics = false).AddSystemTextJson();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
// app.UseGraphiQl("/graphql");
app.UseGraphQLPlayground("/graphql");
app.UseGraphQL<ISchema>();
app.Run();
UserType.cs
using GraphQL.Types;
using GraphQlProject.Models;
namespace GraphQlProject.Type;
public class UserType : ObjectGraphType<User>
{
public UserType()
{
Field(x => x.id);
Field(x => x.userId);
Field(x => x.isActive);
Field(
name: "contact",
type: typeof(ContactType),
resolve: context => context.Source.contact
);
}
}
UserQuery.cs
using GraphQL;
using GraphQL.Types;
using GraphQlProject.Interfaces;
using GraphQlProject.Type;
namespace GraphQlProject.Query;
public class UserQuery: ObjectGraphType
{
public UserQuery(IUser userService)
{
Field<UserType>("user",
arguments: new QueryArguments(
new QueryArgument<StringGraphType> { Name = "id"},
new QueryArgument<StringGraphType> { Name = "userId"}
),
resolve: _ => userService.GetUserById(
_.GetArgument<string>("id"),
_.GetArgument<string>("userId")
));
}
}
UserService.cs
using System.Text.Json;
using System.Text.Json.Serialization;
using GraphQlProject.Interfaces;
using Microsoft.Azure.Cosmos;
using User = GraphQlProject.Models.User;
namespace GraphQlProject.Services;
public class UserService : IUser
{
private readonly CosmosClient _client;
private readonly IConfiguration _config;
public UserService(CosmosClient client, IConfiguration config)
{
_client = client;
_config = config;
}
public async Task<User> GetUserById(string id, string userId)
{
try
{
// Your logic here
}
catch (Exception e)
{
Console.WriteLine(e);
throw;
}
}
}
Hot Chocolate:
Program.cs
using GraphQlHotChocolate.Resolver;
using GraphQlHotChocolate.Services;
using Microsoft.Azure.Cosmos;
var builder = WebApplication.CreateBuilder(args);
// Add Cosmos.
builder.Services.AddSingleton(_ => new CosmosClient(builder.Configuration["Cosmos:ConnectionString"]));
builder.Services.AddControllers();
builder.Services
.AddGraphQLServer()
.AddQueryType<Query>()
.AddMutationType<Mutation>()
.AddSubscriptionType<Subscriptions>()
.AddInMemorySubscriptions();
builder.Services.AddTransient<IUser, UserService>();
var app = builder.Build();
if (app.Environment.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseRouting();
app.UseWebSockets();
app.UseEndpoints(endpoints =>
{
endpoints.MapGraphQL();
});
app.Run();
UserQuery.cs
using GraphQlHotChocolate.Data;
using GraphQlHotChocolate.Services;
namespace GraphQlHotChocolate.Resolver;
public class Query
{
private readonly IUser _userService;
public Query(IUser userService)
{
_userService = userService;
}
public Task<User> GetUserById(string id, string userId) => _userService.GetUserById(id, userId);
}
UserService.cs
using System.Text.Json;
using System.Text.Json.Serialization;
using GraphQlProject.Interfaces;
using Microsoft.Azure.Cosmos;
using User = GraphQlProject.Models.User;
namespace GraphQlProject.Services;
public class UserService : IUser
{
private readonly CosmosClient _client;
private readonly IConfiguration _config;
public UserService(CosmosClient client, IConfiguration config)
{
_client = client;
_config = config;
}
public async Task<User> GetUserById(string id, string userId)
{
try
{
// Your logic here
}
catch (Exception e)
{
Console.WriteLine(e);
throw;
}
}
}