Movatterモバイル変換


[0]ホーム

URL:


Strawberry Shakev13

Get started with Strawberry Shake in a Console application

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:

  • How to add the Strawberry Shake CLI tools.
  • How to generate source code from .graphql files, that contain operations.
  • How to use the generated client in a classical or reactive way.
  • How to disable state management for ASP.NET core use-cases.

Step 1: Add the Strawberry Shake CLI tools

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.

  1. Create a dotnet tool-manifest.
Bash
dotnet new tool-manifest
  1. Install the Strawberry Shake tools.
Bash
dotnet tool install StrawberryShake.Tools --local

Step 2: Create a console project

Next, we will create our console project so that we have a little playground.

  1. First, a new solution calledDemo.sln.
Bash
dotnet new sln -n Demo
  1. Create a new console application.
Bash
dotnet new console -n Demo
  1. Add the project to the solutionDemo.sln.
Bash
dotnet sln add ./Demo

Step 3: Install the required packages

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.

  1. Add theStrawberryShake.Server package to your project in order to add our code generation.
Bash
dotnet add Demo package StrawberryShake.Server

Step 4: Add a GraphQL client to your project using the CLI tools

To add a client to your project, you need to rundotnet graphql init {{ServerUrl}} -n {{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 overhere.

  1. Add the conference client to your console application.
Bash
dotnet graphql init https://workshop.chillicream.com/graphql/ -n ConferenceClient -p ./Demo
  1. Customize the namespace of the generated client to beDemo.GraphQL. For this head over to the.graphqlrc.json and insert a namespace property to theStrawberryShake section.
JSON
{
"schema": "schema.graphql",
"documents": "**/*.graphql",
"extensions": {
"strawberryShake": {
"name": "ConferenceClient",
"namespace": "Demo.GraphQL",
"url": "https://workshop.chillicream.com/graphql/",
"dependencyInjection": true
}
}
}

Now that everything is in place let us write our first query to ask for a list of session titles of the conference API.

  1. Choose your favorite IDE and the solution. If your are using VSCode do the following:
Bash
code ./Demo
  1. Create new query documentGetSessions.graphql with the following content:
GraphQL
query GetSessions {
sessions(order: { title: ASC }) {
nodes {
title
}
}
}
  1. Compile your project.
Bash
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 8, the path would be./obj/Debug/net8.0/berry.

Visual Studio code showing the generated directory.

  1. Head over to theProgram.cs and add the newConferenceClient 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.

C#
using System;
using Microsoft.Extensions.DependencyInjection;
using Demo.GraphQL;
namespace Demo
{
class Program
{
static void Main(string[] args)
{
var serviceCollection = new ServiceCollection();
serviceCollection
.AddConferenceClient()
.ConfigureHttpClient(client => client.BaseAddress = new Uri("https://workshop.chillicream.com/graphql"));
IServiceProvider services = serviceCollection.BuildServiceProvider();
IConferenceClient client = services.GetRequiredService<IConferenceClient>();
}
}
}

Step 5: Use the ConferenceClient to perform a simple fetch

In this section we will perform a simple fetch with ourConferenceClient and output the result to the console.

  1. Head over toProgram.cs.

  2. Add the following code to your main method to execute theGetSessions query.

C#
static async Task Main(string[] args)
{
var serviceCollection = new ServiceCollection();
serviceCollection
.AddConferenceClient()
.ConfigureHttpClient(client => client.BaseAddress = new Uri("https://workshop.chillicream.com/graphql"));
IServiceProvider services = serviceCollection.BuildServiceProvider();
IConferenceClient client = services.GetRequiredService<IConferenceClient>();
var result = await client.GetSessions.ExecuteAsync();
result.EnsureNoErrors();
foreach (var session in result.Data.Sessions.Nodes)
{
Console.WriteLine(session.Title);
}
}
  1. Start the console application withdotnet run --project ./Demo and see if your code works.

Started console application that shows a list of sessions

Last updated on2025-06-16 byGlen
About this article
Help us improving our content
  1. Edit on GitHub
  2. Discuss on Slack

[8]ページ先頭

©2009-2025 Movatter.jp