0

I've set up a project using HotChocolate and Entity Framework. The query looks like this. post -> comment -> comment...

fragment comment on CommentType {
    id
    creatorId
    text
    timeStamp
}

query {
    posts {
        edges {
            node {
                id
                communityName
                title
                text
                timestamp
                creatorId
                comments {
                    postId
                    ...comment
                    replies {
                        ...comment
                        replies {
                            ...comment
                            replies {
                                ...comment
                            }
                        }
                    }
                }
            }
        }
    }
}

The Commenttype looks like this. I use dataLoaders for the nested relationships.

public class CommentType : ISearchResult {
        [GraphQLNonNullType]
        public Guid Id { get; set; }
        public string Text { get; set; }
        public DateTime TimeStamp { get; set; }

        [IsProjected(true)]
        public string CreatorId { get; set; }

        public async Task<UserType> User([DataLoader] UserDataLoader dataLoader) {
            return await dataLoader.LoadAsync(CreatorId, CancellationToken.None);
        }

        [IsProjected(true)]
        public Guid PostId { get; set; }
        public async Task<PostType> Post([DataLoader] PostDataLoader dataLoader) {
            return await dataLoader.LoadAsync(PostId, CancellationToken.None);
        }

        [IsProjected(true)]
        public Guid? ReplyToId { get; set; }
        public async Task<CommentType?> ReplyTo([DataLoader] CommentDataLoader dataLoader) {
            if (ReplyToId == null)
                return null;
            else
                return (CommentType?)await dataLoader.LoadAsync(ReplyToId, CancellationToken.None);
        }

        [IsProjected(true)]
        public IEnumerable<Guid> ReplyIds { get; set; }

        [UseProjection]
        public async Task<IEnumerable<CommentType>> Replies([DataLoader] CommentDataLoader dataLoader) {
            return await dataLoader.LoadAsync(ReplyIds.ToArray(), CancellationToken.None);
        }
    }

`

[ExtendObjectType(typeof(Query))]
    public class PostQuery {
        private readonly IMapper _mapper;

        public PostQuery(IMapper mapper) {
            _mapper = mapper;
        }

        [UseDbContext(typeof(ApplicationDbContext))]
        [UsePaging(IncludeTotalCount = true, DefaultPageSize = 20)]
        [UseProjection]
        [UseFiltering]
        [UseSorting]
        public IQueryable<PostType> GetPosts([ScopedService] ApplicationDbContext context) {
            return context.Posts.ProjectTo<PostType>(_mapper.ConfigurationProvider);
        }
    }

This works but it never goes deeper than the first nested object in any of my queries. I've tried adding projection to the nested list, but that doesn't seem to help either.

{
    "data": {
        "posts": {
            "edges": [
                {
                    "node": {
                        "id": "e9f2cedc-6c1d-4977-81b7-65b819e8131d",
                        "communityName": "Test Community",
                        "title": "Test Post",
                        "text": "testing post",
                        "timestamp": "2022-12-23T16:40:14.649Z",
                        "creatorId": "ilIlLJ1g7CTRhcCfZAWG5V9j4rx1",
                        "comments": [
                            {
                                "postId": "e9f2cedc-6c1d-4977-81b7-65b819e8131d",
                                "id": "7804eada-64ea-4a03-b9aa-7127affb9f32",
                                "creatorId": "ilIlLJ1g7CTRhcCfZAWG5V9j4rx1",
                                "text": "comment on post",
                                "timeStamp": "2022-12-23T17:51:33.808Z",
                                "replies": []
                            }
                        ]
                    }
                }
            ]
        }
    }
}

Is there a specific way to do this with HotChocolate?

Edit: Ok so I forgot to add the Include(c => c.Replies) when fetching the ids from the repository, which makes sense since the dataloader doesn't make use of IQueryable. I suppose won't overfetch since it only reaches this point if it's included in the query anyway. This seems to have solved it.

bokobaba
  • 21
  • 1
  • 3

0 Answers0