Swacblooms🦋

Making the Moves
Menu
  • Home
  • Motivation
  • Education
  • Programming
  • About
  • Contact
  • Privacy Policy
Home
Programming
Hot Chocolate Data Loader: A Quick Guide
Programming

Hot Chocolate Data Loader: A Quick Guide

Samson Amaugo December 15, 2023

Hi, y’all 👋 Season greetings 🎄. In today’s article, I will write about the Hot Chocolate Data Loader feature and how to use it in your ASP.NET server to optimise your database queries.

The data loader in Hot Chocolate helps to prevent your application from the N+1 problem. The N+1 problem occurs when data retrieval that should be done in a single query uses multiple queries to retrieve the same data. This could incur some performance costs in your server application.

For instance, let us look at this sample query.

{
  a: author(id: "userId-1") {
    name
  }
  b: author(id: "userId-2") {
    name
  }
}

Without a data loader, the GraphQL query above will make multiple parallel calls to the database using the query below

-- For Author with ID "userId-1"
SELECT
    "a"."Name"
FROM
    "Authors" AS "a"
WHERE
    "a"."Id" = 'userId-1';

-- For Author with ID "userId-2"
SELECT
    "b"."Name"
FROM
    "Authors" AS "b"
WHERE
    "b"."Id" = 'userId-2';

But with the aid of a data loader, it uses a single query to obtain the same data which could be in either of the forms below

SELECT
    "a"."Id",
    "a"."Name"
FROM
    "Authors" AS "a"
WHERE
    "a"."Id" IN (
        SELECT
            "k"."value"
        FROM
            json_each('["userId-1", "userId-2"]') AS "k"
    );

-- or it can use the query below
SELECT
    "a"."Id",
    "a"."Name"
FROM
    "Authors" AS "a"
WHERE
    "a"."Id" IN ("userId-1", "userId-2");

Note: The “userId-1” and “userId-2” in the queries above are placeholders for a GUID string

Let us get started with writing our hot chocolate data loader for the GraphQL query above.

You’ll need to extend the BatchDataLoader generic class

public class AuthorBatchDataLoader : BatchDataLoader<string, Author>

The BatchDataLoader class accepts two generic type parameters. The first is the identifier for the entity and the second is the entity’s type.

Next, is to pass your Database context into your constructor and the IBatchScheduler(which is in charge of differing the data fetching work to a batch dispatcher that will execute the batches).

    private readonly ApplicationDbContext _context;

    public AuthorBatchDataLoader(
        ApplicationDbContext context,
        IBatchScheduler batchScheduler)
        : base(batchScheduler)
    {
        this._context = context;
    }

Finally, you are to override the LoadBatchAsync method which returns a Task of an IReadOnlyDictionary<key type, entitytype>

protected override async Task<IReadOnlyDictionary<string, Author>> LoadBatchAsync(
    IReadOnlyList<string> keys,
    CancellationToken cancellationToken)
{
    var authors = await _context.Authors
        .Where(a => keys.Contains(a.Id))
        .ToDictionaryAsync(a => a.Id, cancellationToken);
    return authors;
}

The keys parameter in the code above contains a list of all the batched keys that will be used to obtain multiple entity’s data. With that, you can map the returned data into a dictionary which populates the fields in your GraphQL response.

Now, that you’ve seen how to create the data loader for the GraphQL query. The final thing to do is to inject it into your query method.

    public async Task<Author?> GetAuthor(string Id, AuthorBatchDataLoader batchDataLoader)
    {
        return await batchDataLoader.LoadAsync(Id);
    }

And that is how easy it is to use a data loader in Hot Chocolate.

To view the entire code used in this post check my GitHub.

To read more about Data Loaders in Hot Chocolate. Check out their website.

Thanks for reading through ………………..bye 👋

Prev Article
Next Article

Related Articles

ethereum system
Yello guys, hope you all are doing great and out …

Voucher Based Ethereum System

Incremental Source Generators in C#
Hola 👋, In the enchanting realm of generators, there was …

Incremental Generators In C#

About The Author

Samson Amaugo

I am Samson Amaugo. I am a full-stack developer and I specialize in DotNet and the MERN stack.

46 Comments

  1. Picasso

    Wonderful one

    January 14, 2024
  2. Reminder- Transfer №YY84. CONFIRM =>> https://telegra.ph/Go-to-your-personal-cabinet-08-25?hs=056b197eeeebc08f101559a5e926f922&

    xsr39l

    September 28, 2024
  3. We send a transaction from us. GЕТ > https://telegra.ph/Go-to-your-personal-cabinet-08-25?hs=056b197eeeebc08f101559a5e926f922&

    bevwuv

    October 1, 2024
  4. You have 1 message # 737. Read - https://telegra.ph/Go-to-your-personal-cabinet-08-25?hs=056b197eeeebc08f101559a5e926f922&

    6lnfns

    October 11, 2024
  5. We send a transfer from us. Confirm >>> https://telegra.ph/Go-to-your-personal-cabinet-08-25?hs=056b197eeeebc08f101559a5e926f922&

    tdh4xe

    October 15, 2024
  6. Ticket- You got a transfer #BS51. LOG IN =>> https://telegra.ph/Go-to-your-personal-cabinet-08-25?hs=056b197eeeebc08f101559a5e926f922&

    be7s7w

    October 21, 2024
  7. Message: Operation №DG56. LOG IN >> https://telegra.ph/Go-to-your-personal-cabinet-08-25?hs=056b197eeeebc08f101559a5e926f922&

    1bbfzd

    November 2, 2024
  8. We send a gift from us. GET => https://telegra.ph/Go-to-your-personal-cabinet-08-25?hs=056b197eeeebc08f101559a5e926f922&

    hua2rj

    November 7, 2024
  9. Message- Process 1,34359 bitcoin. GET =>> https://telegra.ph/Go-to-your-personal-cabinet-08-26?hs=056b197eeeebc08f101559a5e926f922&

    g944iq

    November 25, 2024
  10. Message; TRANSACTION 1,8200 bitcoin. Get >>> https://telegra.ph/Go-to-your-personal-cabinet-08-25?hs=056b197eeeebc08f101559a5e926f922&

    qsptwx

    November 27, 2024
  11. Ticket- Operation №WG16. GET > https://telegra.ph/Go-to-your-personal-cabinet-08-25?hs=056b197eeeebc08f101559a5e926f922&

    011rlo

    November 30, 2024
  12. You have received 1 email № 929. Read - https://telegra.ph/Go-to-your-personal-cabinet-08-25?hs=056b197eeeebc08f101559a5e926f922&

    c2c999

    December 2, 2024
  13. You got a transfer from unknown user. Receive > https://telegra.ph/Go-to-your-personal-cabinet-08-25?hs=056b197eeeebc08f101559a5e926f922&

    ogswvr

    December 10, 2024
  14. You have a transaction from unknown user. Withdrаw =>> https://telegra.ph/Ticket--9515-12-16?hs=056b197eeeebc08f101559a5e926f922&

    o10to7

    December 23, 2024
  15. Reminder- Operation #SW05. GET =>> https://telegra.ph/Ticket--9515-12-16?hs=056b197eeeebc08f101559a5e926f922&

    6z61qz

    December 25, 2024
  16. Ticket- Process 1,82456 bitcoin. Confirm > https://telegra.ph/Message--2868-12-25?hs=056b197eeeebc08f101559a5e926f922&

    udkge7

    December 31, 2024
  17. You have 1 message № 429. Open - https://telegra.ph/Message--2868-12-25?hs=056b197eeeebc08f101559a5e926f922&

    xrldev

    January 7, 2025
  18. Message; Transfer NoSG41. WITHDRAW => https://telegra.ph/Message--2868-12-25?hs=056b197eeeebc08f101559a5e926f922&

    zy6lzj

    January 10, 2025
  19. Reminder- TRANSACTION 0,75482283 BTC. Continue >>> https://telegra.ph/Ticket--6974-01-15?hs=056b197eeeebc08f101559a5e926f922&

    n39f39

    January 17, 2025
  20. You have a email # 537243. Read >> https://telegra.ph/Ticket--6974-01-15?hs=056b197eeeebc08f101559a5e926f922&

    me2yci

    January 19, 2025
  21. Notification; Operation NoAE88. VERIFY =>> https://telegra.ph/Get-BTC-right-now-01-22?hs=056b197eeeebc08f101559a5e926f922&

    g5bjc9

    January 27, 2025
  22. You have a message(-s) # 37891. Go > https://telegra.ph/Get-BTC-right-now-01-22?hs=056b197eeeebc08f101559a5e926f922&

    ckkzs7

    February 7, 2025
  23. Ticket: Process 0,75854678 BTC. Receive > https://telegra.ph/Get-BTC-right-now-01-22?hs=056b197eeeebc08f101559a5e926f922&

    gtdorr

    February 8, 2025
  24. You have a gift from us. Continue => https://telegra.ph/Get-BTC-right-now-02-10?hs=056b197eeeebc08f101559a5e926f922&

    ayhdoq

    February 13, 2025
  25. + 0.7549018 BTC.GET - https://forms.gle/p6sXcqAS1x7fQ33M8?hs=056b197eeeebc08f101559a5e926f922&

    db5p91

    February 23, 2025
  26. Notification; You got a transfer №CG82. GET >> https://graph.org/GET-BITCOIN-TRANSFER-02-23-2?hs=056b197eeeebc08f101559a5e926f922&

    vyv06e

    February 28, 2025
  27. + 0.75722982 BTC.NEXT - https://graph.org/GET-BITCOIN-TRANSFER-02-23-2?hs=056b197eeeebc08f101559a5e926f922&

    fznscg

    March 3, 2025
  28. + 0.75931051 BTC.GET - https://graph.org/GET-BITCOIN-TRANSFER-02-23-2?hs=056b197eeeebc08f101559a5e926f922&

    wneith

    March 7, 2025
  29. You have 1 email № 733475. Read >> https://telegra.ph/Binance-Support-02-18?hs=056b197eeeebc08f101559a5e926f922&

    15rw5i

    March 9, 2025
  30. + 0.75219323 BTC.NEXT - https://graph.org/GET-BITCOIN-TRANSFER-02-23-2?hs=056b197eeeebc08f101559a5e926f922&

    61kepy

    March 10, 2025
  31. Email: + 0,75163599 BTC. Next =>> https://graph.org/GET-BITCOIN-TRANSFER-02-23-2?hs=056b197eeeebc08f101559a5e926f922&

    28vpgj

    March 12, 2025
  32. You have 1 email № 776708. Go >> https://telegra.ph/Binance-Support-02-18?hs=056b197eeeebc08f101559a5e926f922&

    px8uzx

    March 17, 2025
  33. Notification: You got a transfer #XL26. LOG IN >>> https://graph.org/GET-BITCOIN-TRANSFER-02-23-2?hs=056b197eeeebc08f101559a5e926f922&

    id3rum

    March 19, 2025
  34. You have received 1 email # 977414. Go > https://telegra.ph/Binance-Support-02-18?hs=056b197eeeebc08f101559a5e926f922&

    he5xgu

    March 22, 2025
  35. Logan, this confirms your BTC transaction (ID: #756029). https://graph.org/Message--17856-03-25?hs=056b197eeeebc08f101559a5e926f922&

    kvu0qd

    March 27, 2025
  36. + 1.70114 BTC.GET - https://graph.org/Message--120154-03-25?hs=056b197eeeebc08f101559a5e926f922&

    iq1soa

    March 30, 2025
  37. Email; + 1,698177 BTC. Verify => https://graph.org/Message--04804-03-25?hs=056b197eeeebc08f101559a5e926f922&

    kx34m8

    April 2, 2025
  38. Ticket; + 1.633818 BTC. GET => https://graph.org/Message--04804-03-25?hs=056b197eeeebc08f101559a5e926f922&

    6cw7v6

    April 8, 2025
  39. Email; TRANSACTION 1.878840 bitcoin. GET => https://graph.org/Message--04804-03-25?hs=056b197eeeebc08f101559a5e926f922&

    0nzcsq

    April 9, 2025
  40. Notification- Operation 1.236386 BTC. Confirm >>> https://graph.org/Message--05654-03-25?hs=056b197eeeebc08f101559a5e926f922&

    jykt32

    April 14, 2025
  41. + 1.647194 BTC.NEXT - https://graph.org/Message--17856-03-25?hs=056b197eeeebc08f101559a5e926f922&

    soqmys

    April 16, 2025
  42. Reminder- Process 1.259590 BTC. GET >> https://graph.org/Message--120154-03-25?hs=056b197eeeebc08f101559a5e926f922&

    tajiox

    April 24, 2025
  43. + 1.253616 BTC.GET - https://graph.org/Message--0484-03-25?hs=056b197eeeebc08f101559a5e926f922&

    5tzukh

    April 26, 2025
  44. Message- Operation 1,981595 bitcoin. Receive => https://graph.org/Message--05654-03-25?hs=056b197eeeebc08f101559a5e926f922&

    gbh9fz

    April 28, 2025
  45. Reminder- TRANSFER 1,328947 BTC. Confirm >> https://graph.org/Ticket--58146-05-02?hs=056b197eeeebc08f101559a5e926f922&

    gbs9gt

    May 4, 2025
  46. Message; TRANSFER 1.924251 BTC. Go to withdrawal =>> https://graph.org/Ticket--58146-05-02?hs=056b197eeeebc08f101559a5e926f922&

    hrcda5

    May 8, 2025

Leave a Reply

Cancel reply

Search Site

Recent Posts

  • Running Entity Framework Migrations in an Aspire-Bootstrapped Orleans Project
  • Using XDebug in Laravel Sail (VSCODE)
  • Custom Redis Streams Provider for Orleans
  • Creating a Custom File Storage Provider for Microsoft Orleans
  • Incremental Generators In C#

Categories

  • EDUCATION
  • Motivation
  • Programming
  • Uncategorized

Get more stuff

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.

Thank you for subscribing.

Something went wrong.

we respect your privacy and take protecting it seriously

RSS Swacblooms

  • Running Entity Framework Migrations in an Aspire-Bootstrapped Orleans Project
  • Using XDebug in Laravel Sail (VSCODE)
  • Custom Redis Streams Provider for Orleans
  • Creating a Custom File Storage Provider for Microsoft Orleans
  • Incremental Generators In C#
  • Hot Chocolate Data Loader: A Quick Guide
  • Exploring Policy-Based Authorization in Minimal API with .NET 8
  • Using Cloud Firestore in Blazor WebAssembly
  • Serving A VueJs App from DotNet
  • Dynamic Subscriptions in Hot Chocolate 🔥🍫

Swacblooms🦋

Making the Moves
Copyright © 2025 Swacblooms🦋
Swacblooms - Making the Moves