October 24, 2023

Announcing official Apollo Federation support for .NET with Hot Chocolate and Federation 2!

Phil Prasek

Phil Prasek

Apollo Federation is an open specification for exposing API services as a single GraphQL access layer, known as a supergraph. In a supergraph, you connect and curate your services through smaller, modular graphs called subgraphs. There’s a vibrant ecosystem of community-led frameworks that let you write subgraphs in every common language, bolstered by official Apollo Federation support libraries for JavaScript and Java.

Today, we’re excited to announce a new official Apollo Federation support library for Hot Chocolate — a popular .NET GraphQL framework — including full Federation 2 support for field migrations, entity interfaces, and more flexible type ownership!

A growing ecosystem of frameworks

With the help of Apollo’s official Federation support libraries, the community has added Federation support to over 30+ GraphQL frameworks that you can use to build subgraphs today.

The Apollo-maintained subgraph-js library enables Federation in JavaScript/TypeScript subgraphs:

  • Adds Federation support to Apollo Server, Express GraphQL, NestJS, Yoga, and more.
  • Builds on top of the core graphql-js library, to enable the entire JavaScript ecosystem.
  • Apollo helps actively maintain graphql-js as a key reference implementation across language ecosystems.

The Apollo-maintained federation-jvm library powers Federation in Java/Kotlin subgraphs:

  • Adds Federation support to Spring GraphQL, Netflix’s DGS Framework, and more.
  • Builds on top of the core graphql-java library to enable the whole Java ecosystem.

Expanding official support to .NET

With momentum building in the .NET community, more and more subgraphs are being created using community support for Federation in popular GraphQL packages like GraphQL .NET and Hot Chocolate. To better support the growing number of .NET subgraphs, we’re excited to announce official Apollo support for .NET with a new v1.0 Apollo Federation support library for Hot Chocolate. It’s an open source library that adds Federation support to the main Hot Chocolate GraphQL server and is based on the previous community-led implementation for backwards compatibility. We’ve fixed a few things, overhauled the docs, and added support for Federation 2!

Official compatibility testing for C# / .NET shows comprehensive Federation 2 support for Hot Chocolate:

Quick start template for Hot Chocolate subgraphs

The Rover CLI now supports .NET and a new Hot Chocolate subgraph template that includes:

  • Annotation-based Federation for clean idiomatic C# code.
  • GitHub actions configured for CI/CD using GraphOS schema checks and publishing.
  • Railway one click deploy template.

To bootstrap a new Hot Chocolate subgraph project, run:

rover template use --template subgraph-csharp-hotchocolate-annotation

Federation 2 is configured into your Hot Chocolate subgraph in Program.cs

using ApolloGraphQL.HotChocolate.Federation;

var builder = WebApplication.CreateBuilder(args);

builder.Services
    .AddGraphQLServer()
    .AddApolloFederationV2() 
    // register your types and services
    ;

var app = builder.Build();
app.MapGraphQL();
app.Run();

Root query fields for your subgraph are defined in Query.cs, which federate into your supergraph for apps to use:

public class Query
{
    public Thing? Thing([ID] string id, Data repository)
        => repository.Things.FirstOrDefault(t => t.Id.Equals(id));
}

Entity classes like Thing.cs define a Key , ID, and Reference Resolver to enable joining data across subgraphs:

[Key("id")]
public class Thing
{
    [ID]
    public string Id { get; }

    public string? Name { get; }

    [ReferenceResolver]
    public static Thing? GetThingById(
        string id,
        Data repository)
        => repository.Things.FirstOrDefault(t => t.Id.Equals(id));
}

Upgrade an existing Hot Chocolate subgraph to Federation 2

Update your .csproj file:

  <ItemGroup>
    <PackageReference Include="HotChocolate.AspNetCore" Version="13.5.1" />
    <PackageReference Include="ApolloGraphQL.HotChocolate.Federation" Version="1.0.0" />
  </ItemGroup>

Use the new official Apollo Federation support library and configure your subgraph to use Federation 2:

using ApolloGraphQL.HotChocolate.Federation;

var builder = WebApplication.CreateBuilder(args);

builder.Services
    .AddGraphQLServer()
    .AddApolloFederationV2() 
    // ...
    ;

var app = builder.Build();
app.MapGraphQL();
app.Run();

What’s next?

Starting today, Apollo will continue to maintain official Federation subgraph support for Hot Chocolate. You can expect ongoing improvements and fixes, as well as full support for the latest Apollo Federation spec changes.

We also plan to work with the community to add Federation 2 support for other popular .NET packages like GraphQL .NET.

Learn more

Written by

Phil Prasek

Phil Prasek

Read more by Phil Prasek