Skip to main content
Image

r/graphql


How to use Dapper and hot chocolate instead of EF Core + DbContext?
How to use Dapper and hot chocolate instead of EF Core + DbContext?

Okay for Dapper you dont need a DbContext. Just a connectionstring. I let my program know I have a connectionstring like this:

builder.Services.AddTransient<MawaddaDbContext>(_
    => new MawaddaDbContext(new NpgsqlConnectionFactory(builder.Configuration)));

builder.Services
    .AddGraphQLServer()
    .AddQueryType<Query>();

Here is "MawaddaDbContext" just a class where I just the connection to create tables in my Postgresql database when they dont exist by writing queries using Dapper.

Then I make a Query class so I can fetch data using graphql Hot chocolate library because its just better than REST. However I get an internal error when I try to fetch my schema.

I have searched all of the internet but i cannot find a tutorial how to properly do this stuff with Dapper instead of EF core.

Here is my Query class:

    public class Query
    {
        public async Task<Experience> GetExperienceByIdAsync(string id,               MawaddaDbContext context)
        {
            var conn = await context.ConnectionFactory.CreateConnection();
            Experience experience = await conn.QueryFirstAsync<Experience>(@"SELECT * FROM experiences WHERE Id == ?", id);
            return experience;
        }
    } 

Dominate the competition with the new Galaxy S26 Ultra. Our mightiest processor yet piles on the power for a gaming beast that makes every moment epic. Experience lightning-fast responsiveness and next-level graphics on a powerful device that fits comfortably in your pocket.
media poster