website/src/docs/strawberryshake/v16/get-started/console.md
In this tutorial we will walk you through the basics of adding a Strawberry Shake GraphQL client to a console project. For this example we will create a simple console application and fetch some simple data from our demo backend.
Strawberry Shake is not limited to console application and can be used with any .NET standard compliant library.
In this tutorial, we will teach you:
The Strawberry Shake tool will help you to set up your project to create a GraphQL client.
Open your preferred terminal and select a directory where you want to add the code of this tutorial.
dotnet new tool-manifest
dotnet tool install StrawberryShake.Tools --local
Next, we will create our console project so that we have a little playground.
Demo.sln.dotnet new sln --name Demo
dotnet new console --name Demo
Demo.sln.dotnet sln add ./Demo
Strawberry Shake supports multiple GraphQL transport protocols. In this example we will use the standard GraphQL over HTTP protocol to interact with our GraphQL server.
StrawberryShake.Server package to your project in order to add our code generation.dotnet add Demo package StrawberryShake.Server
To add a client to your project, you need to run dotnet graphql init {{ServerUrl}} --clientName {{ClientName}}.
In this tutorial we will use our GraphQL workshop to create a list of sessions that we will add to our console application.
If you want to have a look at our GraphQL workshop head over here.
dotnet graphql init https://workshop.chillicream.com/graphql/ --clientName ConferenceClient --Path ./Demo
Demo.GraphQL. For this head over to the .graphqlrc.json and insert a namespace property in the StrawberryShake section.{
"schema": "schema.graphql",
"documents": "**/*.graphql",
"extensions": {
"strawberryShake": {
"name": "ConferenceClient",
"namespace": "Demo.GraphQL",
"url": "https://workshop.chillicream.com/graphql/",
"records": {
"inputs": false,
"entities": false
},
"transportProfiles": [
{
"default": "Http",
"subscription": "WebSocket"
}
]
}
}
}
Now that everything is in place let us write our first query to ask for a list of session titles of the conference API.
code ./Demo
GetSessions.graphql with the following content:query GetSessions {
sessions(order: { title: ASC }) {
nodes {
title
}
}
}
dotnet build
With the project compiled, you should now see in the directory ./obj/<configuration>/<target-framework>/berry the generated code that your applications can leverage. For example, if you've run a Debug build for .NET 10, the path would be ./obj/Debug/net10.0/berry.
Program.cs and add the new ConferenceClient to the dependency injection.In some IDEs it is still necessary to reload the project after the code was generated to update the IntelliSense. So, if you have any issues in the next step with IntelliSense just reload the project and everything should be fine.
using Microsoft.Extensions.DependencyInjection;
using Demo.GraphQL;
var serviceCollection = new ServiceCollection();
serviceCollection
.AddConferenceClient()
.ConfigureHttpClient(
client =>
client.BaseAddress =
new Uri("https://workshop.chillicream.com/graphql"));
var services = serviceCollection.BuildServiceProvider();
var client = services.GetRequiredService<IConferenceClient>();
In this section we will perform a simple fetch with our ConferenceClient and output the result to the console.
Head over to Program.cs.
Add the following code to execute the GetSessions query.
var result = await client.GetSessions.ExecuteAsync();
result.EnsureNoErrors();
if (result.Data?.Sessions?.Nodes is null)
{
Console.WriteLine("No sessions found.");
return;
}
foreach (var session in result.Data.Sessions.Nodes)
{
Console.WriteLine(session.Title);
}
dotnet run --project ./Demo and see if your code works.