Use Hot Chocolate and NET 6 to build GraphQL application -- Realizing Query mapping function

Series navigation

Use Hot Chocolate and NET 6 to build GraphQL application article index


In the last article Use Hot Chocolate and NET 6 to build GraphQL application (3) -- realizing the basic function of Query In, we left two problems. One is that the Posts results returned through the GraphQL interface did not get the associated Comment and Tag content, and the other is that the returned results were not sorted. In this article, let's first solve the first problem.


To obtain associated entities, you will naturally think of adding an Include statement when using EF Core. Although this method can be implemented, it is not in line with the style of GraphQL interface. GraphQL itself treats data objects as vertices and their adjacent nodes in the graph. Therefore, we will not modify the logic of the interface itself, and complete the requirements through the mapping attribute provided by Hot Chocolate.


To use the Projection attribute, you need to specify when adding service dependency injection:

  • ProgramExtension.cs

Then add UseProjection above the interface:

  • Query.cs
public IQueryable<Post> GetPosts([Service] IRepository<Post> repository) => repository.GetAsQueryable();

In this way, the acquisition of associated entities is very simple. Let's verify it.


Start the Api project and call the interface:

You can see that the Comment and Tag information have appeared in the return body. Let's take another look at the EF Core log output from the console:

[09:48:40 INF] Executing endpoint 'Hot Chocolate GraphQL Pipeline'
[09:48:40 WRN] Compiling a query which loads related collections for more than one collection navigation, either via 'Include' or through projection, but no 'QuerySplittingBehavior' has been configured. By default, Entity Framework will use 'QuerySplittingBehavior.SingleQuery', which can potentially result in slow query performance. See for more information. To identify the query that's triggering this warning call 'ConfigureWarnings(w => w.Throw(RelationalEventId.MultipleCollectionIncludeWarning))'.
[09:48:40 INF] Executed DbCommand (2ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
SELECT "p"."Id", "p"."Title", "p"."Author", "c"."Content", "c"."Id", "t0"."Name", "t0"."PostsId", "t0"."TagsId", "t0"."Id"
FROM "Posts" AS "p"
LEFT JOIN "Comments" AS "c" ON "p"."Id" = "c"."PostId"
    SELECT "t"."Name", "p0"."PostsId", "p0"."TagsId", "t"."Id"
    FROM "PostTag" AS "p0"
    INNER JOIN "Tags" AS "t" ON "p0"."TagsId" = "t"."Id"
) AS "t0" ON "p"."Id" = "t0"."PostsId"
ORDER BY "p"."Id", "c"."Id", "t0"."PostsId", "t0"."TagsId"
[09:48:40 INF] Executed endpoint 'Hot Chocolate GraphQL Pipeline'

From the log, we can see that Hot Chocolate tells us that when loading multiple associated entity collections, either Include or Projection is used, but QuerySplittingBehavior will be used by default when no QuerySplittingBehavior is applied Singlequery may lead to slow query. The specific description also gives links. Interested partners can have a look.

Then there is the SQL statement actually executed. We can see that this SQL statement obviously performs the Join operation between associated objects, so we can get the desired result.


In this paper, we realize the acquisition of association objects. The next article will introduce how to filter query data.

Keywords: graphql

Added by gwydionwaters on Sun, 30 Jan 2022 07:04:36 +0200