GraphQL API with .NET 5 and Hot Chocolate
HTML-код
- Опубликовано: 2 окт 2024
- In this step-by-step tutorial I take you through how build a GraphQL API using C#9, .NET 5 and the Hot Chocolate framework.
The video covers:
Application Architecture Overview
GraphQL theory and core concepts
Building Queries
Multi-threaded queries using Pooled DBContextFactory
Sorting & Filtering
Building Mutations
Building Subscriptions (real-time event notifications with Web Sockets)
Level: Intermediate
📕 My Book: www.apress.com...
🤩 Patreon Site (Exclusive Member Benefits!): / binarythistle
📕 Webhooks Course on Udemy: www.udemy.com/...
💻 GitHub Repo: github.com/bin...
🔗 WSL Install: docs.microsoft...
🔗 Installing Docker in WSL: docs.docker.co...
🔗 Visual Studio Code: code.visualstu...
🔗 SQL Server Express: www.microsoft....
🔗 Visual Studio: visualstudio.m...
🔗 Insomnia: insomnia.rest/
🔗 SQL Server on Docker: hub.docker.com...
🔗 SQL Server Management Studio: aka.ms/ssmsful...
🔗 GraphQL Voyager: github.com/API...
🔗 GraphQL Official Site: graphql.org/
🔗 Chilli Cream / Hot Chocolate Site: chillicream.com/
⏲️ Time Codes ⏲️
PART 1 - INTRODUCTION
0:48 Welcome
2:38 Demo
11:10 Course Overview
14:03 Architecture
17:15 Ingredients
PART 2 - THEORY
19:26 What is GraphQL?
20:17 GraphQL Core Concepts
24:22 REST Vs GraphQL
31:22 GraphQL Frameworks in .NET
PART 3 - CODING PART 1
33:42 Environment Setup
38:44 Project Setup
47:58 Setup SQL Server with Docker
58:29 Configuration & Dependency Injection
1:10:41 Set up our 1st Model
1:14:20 Setup DB Context & Connection String
1:25:44 Migrate to DB
1:32:45 Create GraphQL Query
1:37:33 Configuring GraphQL Services (Startup Class)
1:38:54 Configuring GraphQL in Request Pipeline ()
1:40:03 Querying our endpoint with Banana Cake Pop & Insomnia
1:51:39 Setting up and using GraphQL Voyager
1:57:55 Concurrent quering issue
2:02:10 Introducing the Pooled DbContext Factory
PART 4 - CODING PART 2 - EXTENDING OUR QUERIES
2:06:49 Adding our 2nd Model
2:10:51 Update DBContext & Migrate
2:17:41 Querying with multi-model - using projection
2:24:40 Adding a Commands Query
2:28:44 Adding in-line documentation to our API
2:32:46 Annotation Vs Code First approaches
2:38:51 Adding a Platform Type with Resolver
2:51:08 Adding a Command Type with Resolver
3:00:11 Sorting & Filtering capabilities
PART 5 - CODING PART 3 - MUTATIONS & SUBSCRIPTIONS
3:07:05 Introducing Mutations & Adding Platforms
3:17:16 Testing our Platform Mutation
3:22:37 Adding a Commands Mutation
3:30:46 Testing our Commands Mutation
3:34:08 Adding Subscriptions (Real-time events)
3:43:01 Testing our Subscription
PART 6 - WRAP UP
3:45:23 Wrap up and Final thoughts
3:46:48 Patreon Supporter Credits
I was getting errors using the ResolveWith in the two Type classes, what fixed the issue was adding the [Parent] decorator to the first argument in the two Resolvers classes.
public Platform GetPlatform([Parent] Command command, [ScopedService] AppDbContext context) and
public IQueryable GetCommands([Parent] Platform platform, [ScopedService] AppDbContext context)
Hope this helps anyone doing this currently.
This is with HotChocolate version 12.0.1
Got the same, thanks !
and I
I spent two days, and no result. But, THANK YOU ^^
Thanks! you saved a lot of my efforts. I got stuck with these errors
Thank you so much!
Tip: If you add a 00:00 time code in your description, then youtube will automatically add chapters to the video.
Did not know this. Great tip.
Amazing tutorial, thank you! What would be awesome is to include a section (or a separate video) on how to handle authorization in GraphQL (both "endpoint-wide" and also user by user/object by object). I know it's a whole different complex can of worms and lots of work, however that's the last piece of info for me to be able to "ditch" REST :) Any chance you would be up to something like that?
Make more and more video about API and GraphQL please... It's very useful,... very useful....
Nice tutorial. I learned a lot. Please create a video about authentication and authorization stuff with GraphQL.
I really loved the content here. It was easy to understand and just straight forward. Could you please, go into how to secure a GraphQL Api. I have tried a lot of ways but just cannot get my head around it. Sincerely.
It is very hard to find tutorials that strikes the perfect balance between concepts and hands on and this tutorial does exactly that. Also i was determined to try out Postman throughout the session but seeing how Insomnia tool supports GraphQL, i do not see myself using postman for graphql executions. Thanks @LesJackson
For someone who is doing this app in dotnet 7 and has errors after creating the PlatformType, you need to put the [Parent[ attribute in front of the GetCommands method from the Resolvers class.
This is a breaking change in the framework of HotChocolate
could you use ODATA and keep HTTP API instead of using graphQL?
Hi Les! Love your voice, your beart, the accent and the way you're teaching us all that interesting stuff! Keep up your good work! For me it is "learning with a smile" and makes it easier then elsewhere! Greetings from Hamburg to Melbourne!
If you are having trouble with resolvers not working, you must add [Parrent] atribute to the object you are passing into function.
Hi Les, thanks for this great course! I didn't finish it yet, I'm on my way :). I would like to mention one thing I found when running a profiler for sql server: when using ObjectTypes (for Platform and Commands) and doing the heavy lifting manually, there are three queries executed against the database (one per platform id). But if I use the default behavior, there is only one. I think that's worth noting. Thanks!!
Thanks for taking your time to make up learning in a easy way!
Definitely I recommend this video!
Hi, is there a way to add authentication and authorization to this project?
Oof! I'm interested in GraphQL and may look at implementing it into one of my future projects. Might have to digest this video in chunks though! :D
Finally got through it. Very interesting, and very well explained.
Very nice in case GraphQL.Server.Ui.Voyager Version="5.0.2" is installed Configure section of Startup file will be as
app.UseGraphQLVoyager(new VoyagerOptions() {
GraphQLEndPoint = "/graphql" },
path: "/graphql-voyager"
);
Thank you! Needed this!
Thanks man! big help
Just a quick FYI, I was tripped up a little by the section on ObjectTypes as the resolvers have changed a little going from v11 => v12. The fix was really simple and it was to add [Parent] to the first parameter on the resolver Get methods (GetCommands and GetPlatform).
Thank you, I was stuck for a while till I came across your comment
@@temitayoadesugba2819 you are welcome 🙂
Same issue same solution! Tnx!
Thanks bro!
Thanks for the solution, I had the same issue
This is really great presentation. Would be great if you could include the pagination support as well.
The GraphQLVoyagerOptions to VoyagerOptions class has changed and there is no longer a Path property.
Thanks so much for this great video. Merry Christmas!
Les, this is indeed the best video on this library, but since HotChocolate has reached v13, and they've evolved their API.... Any chance you can make a v2 of this course . Even a paid course would be awesome as training on this is fragmented :-(
Welcome back bro! Keep up the awesome content! You're a great teacher.
I love graphql but having done work with graphql.net hot chocolate is far better and I did a demo with a legacy app and got up and running in a day I love banana cake pop as the testing and info ui. Its definitely something I will be promoting. Thank you for your videos it helps so much
2020 was Covid on peak throughout the world. I don't know how it was good for you and/or everyone.
For someone struggling with the voyager setup, this is how it worked for my version of 5.0.2:
app.UseGraphQLVoyager(new VoyagerOptions()
{
GraphQLEndPoint = "/graphql",
}, "/graphql-voyager");
Thanks for this - the "Path" property also doesn't exist, it seems to be set as default to this:
- localhost:5000/ui/voyager
THANKS! I burned an hour on this and could not move on.
Thanks
Thank you for this!
I guess the now supposed way is:
after
endpoints.MapGraphQL();
insert
endpoints.MapGraphQLVoyager("/graphql-voyager");
do you have any plan on using dapper on your next project?
Just finished reading a book yesterday about creating REST API in .Net core, and now youtube algorithm bring me to the author himself.
conclusion: this guy is a superb teacher!
What's the name of the book brother..?
@@robertmrobo8954 It's linked in the description: The Complete ASP.NET Core 3 API Tutorial
A very clear and understandable tutorial could be improved by setting chapters in the video or split into pieces of about 45 minutes. This would make it far easier to find a position for recapitulation, but all in all thumbs up
Was missing you and your awesome way of teaching.. hope will get many more this year..
There was no argument with the name `command` found on the field `platform`:
if you have this error, try to use tag [Parent] before Platform and Command entity in Resolvers Methods. Works for me.
Here is an example:
private class Resolvers
{
public Platform GetPlatform([Parent] Command command, [ScopedService] AppDbContext context)
{
return context.Platforms.FirstOrDefault(p => p.IdPlatform == command.PlatformId);
}
}
thnaks man
Hey Les, thanks for the great presentation. ODATA protocol seems to solve both under-fed and over-fed problems. In addition it allows you to add filters and/or range conditions. What's the benefit of using GraphQL, reinventing the query language once again. Thanks
Google "why odata is not popular", you will get your answers.
Hi Les, first of all would like to thank you for this great video! I have been following along almost until the end. At 02:26:40, I noticed when adding the [UseProjection] attribute, my query for Commands does not give back any platforms. It is only when I remove the attribute that I receive some platforms as a child node in my GetCommand query. Any idea why this happens? Just created the project back yesterday, so in terms of versions there shouldn't be too much of a difference. Thanks again!
EDIT: At 02:57:55 I noticed you had the same issue (I didn't do any intermediate testing, so noticed it quite late), and you solved it the way I found out :-D
Great except for two things:
1- I think there should be kind of writing comments. Beginners need that to fully understand the code.
2- I hope you could explain how to upload the project on a working website and how does that process done through VS-Code
Thanks for the tutorial. Watched and implemented to the end. Everything worked fine. Thanks once again @Les Jackson
This is Superb Tutorial, Well presented . Les hat off to your effort and passion in teaching every bit of details in such a structure and easy format. I loved this video and have become fan of your channel.
Keep up the good work .
Many thanks
Hey man on 3:06:00 I notice we still can access the licenseKey in order to query. In fact it is not available as property of the platfom object but it appears as a order
Hey, I am new to dotnet and wondering what is best way to store connection string and how to provide connection string on prod?.
Usually in Java(Spring boot) we override local connection string with environment variable on prod. So is there anything in dotnet?
Don't want to use user-secret and other third party service.
What a mad lad! Thank you so much for these great videos, learning with you is so much easier 😊 👍👍
Tip: If you run "dotnet watch run", then you program you auto restart when watch some changes
Hi @Les Jackson very awesome and helpful tutorial, i wanted to present graphql infront of students where i work and i saw the presentation that you use in this tutorial is very clear and informative, if you could send me the presentation that would mean the world to me, thanks a bunch
This tutorial is still amazing, but you're missing one very important step: the n+1 problem. This is a huge issue that every API has to deal with, and it's not as straight forward as it should be. It would be great to see an implementation for that.
agreed
From some minimal SQL profiling, I was not seeing the n+1 surface in queries generated by this framework. Querying across a FK relationship was handled by a Join as expected, not multiple DB round trips. It seems that Hot Chocolate has a DataLoader implementation to handle the n+1 problem, but I'm not sure we wired it up in this tutorial. It's possible that Entity Framework was handling that optimization for us.
Great tutorial, thanks!
I have just completed this video and can confirm this was such a great introduction to GraphQL, thank you so much! I agree with another commenter that another video on the advised or suggested ways to query this new API from a frontend perspective would be great too. Thanks again!
How challenging would it be to add an authentication layer on top of this? I'm not finding a ton of examples of using GraphQL with auth.
hi, i have and error There was no argument with the name `platform` found on the field `commands`. my query is: query{
platform
{
id
name
commands
{
id
commandLine
}
}
}
Yes, I also got this error
Hi Pedro,
Yes, I just faced the same issue even I understood that we were doing same thing in the video. The reason is because we're using the latest version of hotchocolate which can be 12.0.1 which had some changes in the library (major version has been increased so breaking changes can be occured, it's normal).
You can reproduce it if you go back to the version of 11.0.8, it should work well as what author has done.
However, I believed that the stuff to go back to the version which the author demonstrated in this video is not what we're looking for, right?
Oke, actually I have checked the library once with the latest version. And I realized that in the resolver, we have to decorate the parent object with Parent Attribute, otherwise it won't understand thing as well.
So this is the fixes, you see, I decorated command parameter with parent attribute. Then you can run the source code once and see it's working now. You can do the same thing for PlatformType, it should be working properly also.
private class Resolvers
{
public Platform GetPlatform([Parent] Command command, [ScopedService] AppDbContext context)
{
return context.Platforms.FirstOrDefault(x => x.Id == command.PlatformId);
}
}
Hope this comment can help you anyways.
Thx Lucas
Thank you from Russia, with love
This is the best GraphQL tutorial yet I have watched online. Very detailed with well-explained concepts. Thanks for this awesome work!
Les!, you know you can hide cortana and the taskbar searchfield, right?^^
Thank you so much for your videos.
What about N+1 problem?
For anyone following this tutorial, here are the powershell commands in case you don't want to type them out:
dotnet new web -n $ProjectName
dotnet add package HotChocolate.AspNetCore
dotnet add package HotChocolate.Data.Entityframework
dotnet add package Microsoft.EntityframeworkCore.Design
dotnet add package Microsoft.EntityframeworkCore.SQLServer
dotnet add package GraphQL.Server.Ui.Voyager
Tip: If you having trouble installing Core design Framework, use the below command.
Error Message: error: NU1202: Package Microsoft.EntityFrameworkCore.Design 6.0.10 is not compatible with net5.0 (.NETCoreApp,Version=v5.0). Package Microsoft.EntityFrameworkCore.Design 6.0.10 supports: net6.0 (.NETCoreApp,Version=v6.0)
dotnet add package Microsoft.EntityframeworkCore.Design --version 5.0
Problem: Unfortunately I'm getting errors using the Insomnia commands "Get Commands" and "Get Platforms and Commands", while using my own project or analog your GitHib project, I just downloaded and I just can't find a clue...could you help? Could you also update the project regarding the aspect nullable references (C# 8) and from your perspective the best approach?
{
"errors": [
{
"message": "There was no argument with the name `command` found on the field `platform`.",
"locations": [
{
"line": 6,
"column": 3
}
],
"path": [
"command",
4,
"platform"
],
"extensions": {
"fieldName": "platform",
"argumentName": "command"
}
dev from philippines, your content helps me a lot
I've noticed that the new Version of GraphQL.Server.Ui.Voyager greater Than 4.4.1 isn't working GraphQL.Server.Ui.Voyager Path is failing, I've tried 5.0.(0,1, and 2)
! important update to version 5.0.2+ of GraphQLVoyager (1:54:39) instead of :
app.UseGraphQLVoyager(new GraphQLVoyagerOptions()
{
GraphQLEndPoint = "/graphql",
Path = "/graphql-voyager"
});
try to use:
app.UseGraphQLVoyager(new VoyagerOptions()
{
GraphQLEndPoint = "/graphql"
}, "/graphql-voyager");
hmm this could end up being one of those topics like; what's better react vs angular ;-)
Always be careful when you have to use tools with weird names. Hey mister major corporation CIO, we are going to implement your multi million dollar project using "hot chocolate", with our tests using cucumber Documentation with gherkin syntax. I've been burnt one to many times using the new flashy "frameworks" especially if its cross vendor. i.e. Google and Microsoft. it you are delivering projects on the Microsoft stack; i think it will be very difficult to recommend anything other than Rest + Odata + Utility APIs for edge cases (i.e solves complex queries for over and under fetching, and you can use basic query language like LINQ)
But still very interesting, thanks for sharing ;-)
enum System.Data.CommandType
hi Les,
I tried to combine your tutorials: run graphql in docker and get data from mssql server from another container throw the http get request.
I deployed graphql project and mssql server using docker-compose.
Separately both containers become available and I can fetch data from mssql container via mssql manager.
But I fail when try to execute some GET method from graphql project from Banana Cake Pop
What am I missing?
Great tutorial. GraphQl is cool!
But I found it inconvenient to use GraphQl
I can't require all fields like "select * from" in sql and I need to write all fields (from 10-100 in different request).
How to work with dynamic entities? any advice, thanks!
Excellent video had to google a breaking change in the HotChocolate 12 update in the Platform and CommandsTypes code, but this is a minor issue. I have implemented Dataloaders which I definitely recommend. Great Job! Thanks!!
There is no doubt that this is the best tutorial out there. Thank you so much Les for also taking the time to explain the things clearly. You just gained a new subscriber.
This has to be one of the best tutorials I have ever watched. Thanks for putting it together!
i Agree!
Couldn't agree more
Can you please upload the Startup.cs file somewhere and post a link of the file in the description? .NET5 is discontinued and for some reason the "dotnet new" command in .NET6 did not generate the Startup.cs file, so I had to make one manually. But in the video so far, the file is not fully visible.
When ignoring the license field I noticed that whilst it may omit it from the fields returned in the graph query, EF is still fetching the field from the DB. This doesn't happen when using UseProjection - only the fields specified will be selected from the DB. Is there a way to do this when using query types with custom resolvers?
I have not experience as much as you have as software developer but I have suggestion for you, title of the video is GraphQL API with .NET that's why if someone come here they need to know .NET and the rest that's why you don't need to dive into detail of every single component of the journey of the project. and you can make video for them seperately and you can add them in this video's cards section.
Amazing. Thank you.
Can somebody help me?
How to add opportunity to get object by id, using:
query{
object(id: 5){
name
}
}
Hi Les Jackson,
Your tutorial is very good but I have a question. When configuring the voyager in the request pipeline, I am not able to find Path as a parameter to specify the path of the voyager. Can you please help me with that?
When I Added the type classes in HotChocolate.AspNetCore version 12.7.0, a lot of errors were returned that I can't resolve, can you do an updated version of this?
thanks Les!!!!!!!!!! im subbed to your Patreon now :) next stop: microservices course! keep em comin baby
I have a question. Does it make sense to declare relationship two times? First - "one platform has many commands with one platform". Secound - "one command has one platform with many commands".
Is there any reason why u are typing it twice?
Does it form a single join query or do N+1 queries when doing the projection? If it does not do it in one single database query, I would not use it. Years ago, when learning how to implement graphql, I was seeing in the tutorials that there was no infrastructure to turn projections into a single join query, so I avoided using it and built my own alternative to graphql, that works similarly but creates efficient queries, using a rest api and query string parameters to inform the tree of objects and fields to fetch and give extra query filtering and sorting parameters. The mutations part of grapql does not offer much extras anyway, can be handled easily in rest api using put or patch http requests.
The video was great. But how to use graphql in any architecture like clean. Graphql working with entities directly but we need tp hide entities in the infrastructure and use domain models in our layers. How you solving this problem?
Very easy to understand, great voice, great visuals and great pace! Overall, a very good tutorial that is easy to digest and very much worthwhile going through. I only had one slight issue when I couldn't get Voyager to work, other than that every part was flawlessly explained and shown!
if you are using package GraphQL.Server.Ui.Voyager Version="5.0.1" or higher the following should help:
using GraphQL.Server.Ui.Voyager;
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
...
app.UseGraphQLVoyager(new VoyagerOptions(){
GraphQLEndPoint = "/graphql"
});
}
default voyager endpoint is "ui/voyager"
cheers.
@@toddnedd2138 This does not work for me.
But this:
app.UseGraphQLVoyager(new VoyagerOptions(), "/graphql-voyager");
You can also omit the path. Then the GraphQL Voyager is accessible via its default: /ui/voyager
@@rafaelskiba3854 I was googling the hell out of it and still unable to figure it out. Thank you so so much!
The resolver doesn't seem to be hit. At minute 2:48:20 at line 29 in class PlatformType, if you comment out the line everything works just the same. Am I missing something?
Hey Les, how about an upgrade to latest version and adding subscription using redis pupsup?
I understand this is a matter of personal preference, but watching you typing so many things that Visual Studio or Rider would type for you automatically, tics my nerves for some reason hahahaha.
Hi Les, F$k me.. what a great walk through. Did not follow everything the first time through but was fantastic to see practical end-end and to get an appreciation of the hill to climb with this tech.. but at same time certainly got to appreciate what it is offering!
HI Les i've looked the video from start to finish and a few part again. I've one question wich is about the Resolvers around 2:50. If i implement the resolver patern then i see a EF call for every record. When i don't use the resolver the EF only needs one call for the complete result. Is there i do wrong or is this by design and what is the profit of the resolver patern.
These are such high quality videos, I love it!
Thanks for Tutorial. Love way of your teaching 👍
Thanks for the tutorial, I got an error using types, when I run Platform is ok, but when add the Commands property an error shows up, can someone help please
One of the best tutorials I’ve seen. Coming from zero knowledge of graphql I feel at least now I understand the concepts backed by hands on examples. Thank you. Count me in as a new subscriber.
Yes, we count you.
Can someone convert this into .net6. I couldn't follow this tutorial properly because doing it while converting the code into .net 6 is confusing for me lol
Amazing tutorial, very clear, thank you
I wished you would have showed what the proper way with a graphQL how to return a error. In the methods you should be doing data validation checks to make sure the data is valid before inserting. Would you just return null? I am not sure if it will inform the caller that the data wasn't valid.
I love his tutorial! His teaching mannerism is so assuring and easy to follow. Best instructor ever!
Is pooling dbContextes means creating multiple connections to db for single request or it means just query to db multiple time ?
Great video. thank you!
I count an issue with foreign key using command query
Command table records:
6 HowTo RedHat1 CommandLine6 6
7 HowTo RedHat2 CommandLine7 6
Query:
query{
command(where: {platformId: {eq:6}}){
id
howTo
platform{
id
name
}
}
}
Result: platform is null all the time, the reasons is that platform.Id is "0" all the time.
return context.Commands.Where(p => p.PlatformId == platform.Id);
Any ideas? thanks again!
I have the same issue
Did you fixed this bag ?
I just want to say: This is awesome!!!
I noticed you install GraphQL as the server, I thought that was the purpose of HotChocolate to act as the server?
A lot of tutorials on tech topics fly over the "obvious stuff". I really appreciate that you take the time to go into the details of each step but remain concise. Great video thanks.
you didnt showed on how to wrap graphql on existing restapi @Les Jackson
Hi, great, i migratting my system .net core api and mvc using this technology, Thx
Excellent tutorial. Very clear on every setp & easy to follow along.
Can we please expect a video on integration testing GraphQL?
With Web API, it is easy to perform in-memory API testing
Any such option works with GraphQL?
public IQueryable GetCommands([Parent]Platform platform, [ScopedService] AppDbContext context) in resolver for the error
Great, wondeful, easy to absorb and understand topic. I love your method of training others. I think it would be great if you show at the end for 10 minutes how to call all those GraphQl services (Platform, Command) from a .Net client or an asp.net mvc web page, that would be great if you can add this part, well done and thanks for making this available for the community.
Yes, that would be great. I was also thinking about the graphql response, its very dynamic - How would you deal with this in a .net application?
@@KittFanNr1 I googled but was not able to figure it out for this code, they were all specific and not similar to what Les showed us, maybe he can help us with a 15 minutes video if he has some availability :-)
is it good to use GraphQL in Chat by signalR ? Notice that i am gonna bind that chat to a microservice
Nice Presentation. One correction, I would like to add one point hare, the GraphQL.Server.Ui.Voyager.VoyagerOptions seems have a changed syntax in latest version as follows
app.UseGraphQLVoyager(new VoyagerOptions()
{
GraphQLEndPoint = "/graphql",
}, "/graphql-voyager");
"I'll put a link to videos managing secrets"
no links in sight :'(