website/src/docs/strawberryshake/v16/get-started/index.md
In this tutorial we will walk you through the basics of adding a Strawberry Shake GraphQL client to a Blazor for WebAssembly project. For this example we will create a Blazor for WebAssembly application and fetch some simple data from our demo backend.
<Video videoId="-oq7YEciouM" />Strawberry Shake is not limited to Blazor 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 Blazor project so that we have a little playground.
Demo.sln.dotnet new sln --name Demo
dotnet new blazorwasm --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. All of this functionality comes packaged with the StrawberryShake.Blazor meta package.
StrawberryShake.Blazor package to your project.dotnet add Demo package StrawberryShake.Blazor
To add a client to your project, you need to run dotnet graphql init {{ServerUrl}} --clientName {{ClientName}}.
In this tutorial we will use our ChilliCream demo project to create a list of cryptocurrencies that we will add to our Blazor application.
If you want to have a look at our demo GraphQL server head over here.
dotnet graphql init https://demo.chillicream.com/graphql/ --clientName CryptoClient --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": "CryptoClient",
"namespace": "Demo.GraphQL",
"url": "https://demo.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 GetAssets {
assets {
nodes {
name
price {
lastPrice
}
}
}
}
If you are working with Visual Studio on Windows edit the properties of GetSessions.graphql and set the build action for the GraphQL file to GraphQL compiler.
dotnet build
With the project compiled the strawberry shake generator produced a client but also components that we can use in Blazor.
Program.cs and add the new CryptoClient 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.AspNetCore.Components.Web;
using Microsoft.AspNetCore.Components.WebAssembly.Hosting;
using Demo;
var builder = WebAssemblyHostBuilder.CreateDefault(args);
builder.RootComponents.Add<App>("#app");
builder.RootComponents.Add<HeadOutlet>("head::after");
builder.Services.AddScoped(sp => new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) });
builder.Services
.AddCryptoClient()
.ConfigureHttpClient(client => client.BaseAddress = new Uri("https://demo.chillicream.com/graphql"));
await builder.Build().RunAsync();
_Imports.razor and add Demo.GraphQL to the common imports@using System.Net.Http
@using System.Net.Http.Json
@using Microsoft.AspNetCore.Components.Forms
@using Microsoft.AspNetCore.Components.Routing
@using Microsoft.AspNetCore.Components.Web
@using Microsoft.AspNetCore.Components.Web.Virtualization
@using Microsoft.AspNetCore.Components.WebAssembly.Http
@using Microsoft.JSInterop
@using Demo
@using Demo.Shared // (from .NET 8, `Demo.Layout`)
@using Demo.GraphQL
@using Demo.GraphQL.Components
@using StrawberryShake
In this section we will integrate the Razor component and print a simple list on our index page to display the crypto currencies.
Head over to Pages/Index.razor (from .NET 8, Home.razor).
Remove everything from your page but the @page "/"
@page "/"
UseGetAssets component to your page.@page "/"
<UseGetAssets Context="result">
<ChildContent>
</ChildContent>
<ErrorContent>
Something went wrong ...
@result.First().Message
</ErrorContent>
<LoadingContent>
Loading ...
</LoadingContent>
</UseGetAssets>
The query component allows you to handle the loading and the error state when fetching data. Both states can be handled but do not have to be.
@page "/"
<UseGetAssets Context="result">
<ChildContent>
<ul>
@foreach (var item in result.Assets!.Nodes!)
{
<li>@item.Name (@item.Price.LastPrice)</li>
}
</ul>
</ChildContent>
<ErrorContent>
Something went wrong ...
@result.First().Message
</ErrorContent>
<LoadingContent>
Loading ...
</LoadingContent>
</UseGetAssets>
dotnet watch --project ./Demo and see if your code works.Awesome! You have created your first application with Blazor and GraphQL.