ChilliCream / graphql-workshop

Getting started with GraphQL on ASP.NET Core and Hot Chocolate - Workshop
465 stars 199 forks source link

Error in SpeakerType.cs on page 3-understanding-dataLoader.md ? #95

Closed filipgoris closed 2 years ago

filipgoris commented 2 years ago

Following the docs until the explanation of [Fluent Type Configurations on page 3-understanding-dataLoader.md] (https://github.com/ChilliCream/graphql-workshop/blob/master/docs/3-understanding-dataLoader.md#fluent-type-configurations).
Executing query

query GetSpeakerWithSessions { speakers { name sessions { title } } }

Should produce list of speakers with empty sessions list
Produces instead "There was no argument with the name speaker found on the field sessions." for each speaker on line 4 column 5 of the query.

zachbugay commented 2 years ago

The documentation needs to be corrected. The documentation is using Hot Chocolate 11 guidelines, but failed to add the resolver requires a parent attribute now.

Open up your SpeakerType file and update the resolver signature as such:

        private class SpeakerResolvers
        {
            public async Task<IEnumerable<Session>> GetSessionsAsync(
                [Parent] Speaker speaker, // Requires the [Parent] attribute.
                [ScopedService] ApplicationDbContext dbContext,
                SessionByIdDataLoader sessionById,
                CancellationToken cancellationToken)
            {
                int[] sessionIds = await dbContext.Speakers
                    .Where(s => s.Id == speaker.Id)
                    .Include(s => s.SessionSpeakers)
                    .SelectMany(s => s.SessionSpeakers.Select(t => t.SessionId))
                    .ToArrayAsync();

                return await sessionById.LoadAsync(sessionIds, cancellationToken);
            }
        }
    More info can be found [https://chillicream.com/docs/hotchocolate/api-reference/migrate-from-11-to-12#resolvers?ref=https://githubhelp.com](here)
filipgoris commented 2 years ago

Perfect. That was it!