Get started
By the end of this page you’ll have your own solution, a GraphQL API running on your machine, and a page of movies on your screen. It takes about five minutes, or ten if NuGet decides to download the internet first.
Before you start
Section titled “Before you start”You need the .NET 10 SDK. Docker is optional, but it gets you the Aspire dashboard for the logs, traces and metrics, and a container you can ship, so it’s worth having around.
Create and run your solution
Section titled “Create and run your solution”-
Install the template from NuGet.
Terminal window dotnet new install CleanGraphQLApi.Template -
Create a solution. The name becomes your namespace, project names and folder, so pick something you’re happy to see a lot of.
Terminal window dotnet new cleangraphqlapi --name WorksOnMyMachine.Movies --authorName "Your Name"cd WorksOnMyMachine.MoviesRather click? The template shows up in the new project dialog in Visual Studio and Rider with the same options. Fill them in properly. You never know who’s reading and handing out .NET Rocks mugs… Hi Richard!
If the dialog asks for a solution format, pick
.slnx, which is what the template makes. Some versions of Visual Studio and Rider still write a second solution file next to it. If you end up with two, open the.slnxand delete the other one. -
Have a look around. Everything you need is already here.
- WorksOnMyMachine.Movies.slnx
- Dockerfile
- docker-compose.yml
Directorysrc/
DirectoryApplication/ the rules, queries and validation
- …
DirectoryInfrastructure/ the database and repositories
- …
DirectoryPresentation/ the GraphQL schema
- …
Directorytests/
DirectoryApplication.Tests.Unit/
- …
DirectoryInfrastructure.Tests.Integration/
- …
DirectoryPresentation.Tests.Integration/
- …
DirectoryPresentation.Tests.Unit/
- …
-
Run it. The
localenvironment seeds sample data and logs to the console, so there’s nothing to set up.Terminal window dotnet run --project ./src/PresentationIt answers on
https://localhost:7331. If your browser orcurldoesn’t trust the .NET development certificate yet, rundotnet dev-certs https --trustonce. -
Open Nitro, the GraphQL IDE, at https://localhost:7331/graphql. The schema is on the left, with every type and field described, and the editor autocompletes as you type. Paste this in and press play:
{movies(query: "space", first: 2) {totalCountpageInfo { hasNextPage endCursor }nodes { id title reviewCount averageStars }}} -
Or ask from the terminal.
Terminal window curl -X POST "https://localhost:7331/graphql" -H "Content-Type: application/json" \-d '{"query":"{ movies(query: \"space\", first: 2) { totalCount nodes { id title reviewCount averageStars } } }"}'Either way you get a page of movies back:
{"data": {"movies": {"totalCount": 4,"pageInfo": { "hasNextPage": true, "endCursor": "b2Zmc2V0OjI=" },"nodes": [{ "id": "cb188ef6-14e7-f85f-2f78-924c2ab26b28", "title": "Deep Space Gardeners", "reviewCount": 5, "averageStars": 3.4 },{ "id": "dfefa450-5454-6a4d-a4aa-95a9b047c17b", "title": "Postcards from Outer Space", "reviewCount": 1, "averageStars": 3 }]}}}The sample movies are all made up. Deep Space Gardeners averages 3.4 stars from five reviews, which seems fair for a film about weeding in zero gravity.
-
Run the tests while you’re here.
Terminal window dotnet test
Hit a snag? Check Troubleshooting.