0

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 getting 404 for both IDEs when I integrate them on top of Hot 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;
        }
    }
}
GThree
  • 2,708
  • 7
  • 34
  • 67
  • Any reason why you deleted and reposted this question rather than [editing your existing post](https://stackoverflow.com/questions/75630230)? – gunr2171 Mar 03 '23 at 18:28
  • Why would you go with playground... BCP is much feature richer and much more stable? – Michael Ingmar Staib Mar 03 '23 at 18:44
  • @MichaelIngmarStaib I honestly have no preference. I am working with multiple POCs and found out about it. As mentioned in questions, I see two routes where such IDEs can be used with `Ischema` whereas `Hot Chocolate` comes with built-in. Is there a way to override `Hot Chocolate` IDE with some other IDE? – GThree Mar 03 '23 at 18:49
  • @gunr2171 I figured out how to use such IDEs when I am using `ISchema`. So I want to show here is, how did that work in `ISchema`, what I did with `Hot Chocolate` and Is there a need to add external IDEs with HotChocolate? If yes, how to achieve that? I felt like this question explains that. – GThree Mar 03 '23 at 18:52

0 Answers0