Build an ASP.NET Core API using StrawberryShake (recommended)
Learn how to build an ASP.NET Core Web API that retrieves content from Optimizely Graph using StrawberryShake for strongly typed queries and automatic client generation.
This guide shows how to build an ASP.NET Core Web API that connects to Optimizely Graph using StrawberryShake, a strongly typed GraphQL client with automatic code generation.
Optimizely recommends this approach for most production applications because it
- Validates queries at build time.
- Generates strongly typed models automatically.
- Provides IntelliSense support.
- Reduces manual serialization and mapping code.
If you want a lightweight manual client instead, see Build an ASP.NET Core API (manual GraphQL client).
Create an ASP.NET Core project
dotnet new webapi -n MyOptimizelyApp
cd MyOptimizelyAppInstall StrawberryShake tools
dotnet new tool-manifest
dotnet tool install StrawberryShake.ToolsInstall StrawberryShake packages
dotnet add package StrawberryShake.AspNetCore
dotnet add package StrawberryShake.CodeGeneration.CSharp.AnalyzersConfigure the project
Update MyOptimizelyApp.csproj.
<ItemGroup>
<GraphQL Include="**/*.graphql" />
</ItemGroup>Download the schema
dotnet graphql download https://cg.optimizely.com/content/v2?auth=YOUR_KEY -f schema.graphqlCreate GraphQL queries
Create Queries/ArticleQueries.graphql.
query GetArticles($limit: Int = 10, $skip: Int = 0) {
ArticlePage(orderBy: { _modified: DESC }, limit: $limit, skip: $skip) {
items {
ContentLink { Id }
Name
RelativePath
TeaserText
PageImage { Url }
MainBody
}
total
}
}
query GetArticleById($id: String!) {
ArticlePage(where: { ContentLink: { Id: { eq: $id } } }) {
items {
ContentLink { Id }
Name
RelativePath
TeaserText
PageImage { Url }
MainBody
_modified
}
}
}Configure StrawberryShake
Create .graphqlrc.json.
{
"schema": "schema.graphql",
"documents": "Queries/**/*.graphql",
"extensions": {
"strawberryShake": {
"name": "OptimizelyGraphClient",
"namespace": "MyOptimizelyApp.GraphQL",
"url": "https://cg.optimizely.com/content/v2?auth=YOUR_KEY"
}
}
}Register the generated client
Update Program.cs.
using MyOptimizelyApp.GraphQL;
var builder = WebApplication.CreateBuilder(args);
builder.Services
.AddOptimizelyGraphClient()
.ConfigureHttpClient(client =>
{
client.BaseAddress =
new Uri("https://cg.optimizely.com/content/v2?auth=YOUR_KEY");
});
builder.Services.AddControllers();
var app = builder.Build();
app.UseRouting();
app.MapControllers();
app.Run();
Generate the client
Run dotnet build to generate strongly typed classes in MyOptimizelyApp.GraphQL
Create a controller
using Microsoft.AspNetCore.Mvc;
using MyOptimizelyApp.GraphQL;
[ApiController]
[Route("api/[controller]")]
public class ArticlesController : ControllerBase
{
private readonly IOptimizelyGraphClient _graphQLClient;
public ArticlesController(IOptimizelyGraphClient graphQLClient)
{
_graphQLClient = graphQLClient;
}
[HttpGet]
public async Task<ActionResult> GetArticles(int limit = 10, int skip = 0)
{
var result = await _graphQLClient.GetArticles.ExecuteAsync(limit, skip);
if (result.IsErrorResult())
{
return BadRequest(result.Errors);
}
return Ok(result.Data?.ArticlePage);
}
[HttpGet("{id}")]
public async Task<ActionResult> GetArticleById(string id)
{
var result = await _graphQLClient.GetArticleById.ExecuteAsync(id);
if (result.IsErrorResult())
{
return BadRequest(result.Errors);
}
var article = result.Data?.ArticlePage?.Items?.FirstOrDefault();
if (article == null)
{
return NotFound();
}
return Ok(article);
}
}
Run and test
dotnet build
dotnet runVisit the following endpoints in your browser or API tool:
https://localhost:7000/api/articles– Retrieve all articles from Optimizely Graph.https://localhost:7000/api/articles/\{ID}– Retrieve a specific article by its content ID. Replace{ID}with an actual article ID.
Updated about 13 hours ago
