Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Apollo Federation Code-First Example #4687

Merged
merged 11 commits into from
Jan 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .build/Build.Sonar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -121,5 +121,7 @@ DotNetBuildSettings SonarBuildAll(DotNetBuildSettings settings) =>
.SetFramework(Net60);

bool IsRelevantForSonar(string fileName)
=> !ExcludedCover.Contains(GetFileNameWithoutExtension(fileName));
=> !ExcludedCover.Contains(GetFileNameWithoutExtension(fileName)) &&
!fileName.Contains("example") &&
!fileName.Contains("sample");
}
86 changes: 60 additions & 26 deletions src/HotChocolate/ApolloFederation/HotChocolate.ApolloFederation.sln
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,33 @@ VisualStudioVersion = 16.0.30114.105
MinimumVisualStudioVersion = 10.0.40219.1
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{74D95657-7BC6-409F-8484-F963A128764A}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ApolloFederation", "src\ApolloFederation\ApolloFederation.csproj", "{46D3566C-FBBC-46BB-A9AB-454F348A003E}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ApolloFederation", "src\ApolloFederation\HotChocolate.ApolloFederation.csproj", "{46D3566C-FBBC-46BB-A9AB-454F348A003E}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test", "test", "{EA937CAB-A9B1-474E-BF2A-A45399561B2E}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ApolloFederation.Tests", "test\ApolloFederation.Tests\ApolloFederation.Tests.csproj", "{3B96CAE2-A6C0-4BA6-A21B-1CBFE6EF7A16}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ApolloFederation.Tests", "test\ApolloFederation.Tests\HotChocolate.ApolloFederation.Tests.csproj", "{3B96CAE2-A6C0-4BA6-A21B-1CBFE6EF7A16}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "examples", "examples", "{82050CA3-2111-49D6-B682-EA22191ADB99}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Accounts", "examples\Accounts\Accounts.csproj", "{4A81BD31-4B6B-4891-9CBA-7EB2513C9B5D}"
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "AnnotationBased", "AnnotationBased", "{E296D200-558C-4217-95C3-C239C88955D8}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Inventory", "examples\Inventory\Inventory.csproj", "{04BCB1AF-1DC4-482F-9C3C-78FE537B7CCD}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Accounts", "examples\AnnotationBased\Accounts\Accounts.csproj", "{27A5C157-B595-4158-8D58-8E5559A14E39}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Products", "examples\Products\Products.csproj", "{B15DE642-C58C-43AE-932E-A39B0896305A}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Products", "examples\AnnotationBased\Products\Products.csproj", "{7CD8650E-6358-40AB-9376-E0DB36B91C33}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Reviews", "examples\Reviews\Reviews.csproj", "{6B16B4FF-E179-4BA7-8E86-17CECEB6DEE0}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Inventory", "examples\AnnotationBased\Inventory\Inventory.csproj", "{919C2EDB-72D7-476E-919F-521BCA6A2C90}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Reviews", "examples\AnnotationBased\Reviews\Reviews.csproj", "{FFE872A7-6686-4820-86F4-C081714CEFD8}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "CodeFirst", "CodeFirst", "{4FC21B51-8DAB-487D-BF66-32E674417EAB}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Accounts", "examples\CodeFirst\Accounts\Accounts.csproj", "{02272634-995C-44CE-B0E6-E9CFCDE21432}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Products", "examples\CodeFirst\Products\Products.csproj", "{4DA1ECA1-5AAF-465D-A2C7-DECA20DC1065}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Inventory", "examples\CodeFirst\Inventory\Inventory.csproj", "{7C107B4E-9BDE-4944-AAF3-EA8110D04F33}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Reviews", "examples\CodeFirst\Reviews\Reviews.csproj", "{D662DB3B-CB78-49A1-B45B-E518746C4DAF}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand All @@ -38,29 +50,51 @@ Global
{3B96CAE2-A6C0-4BA6-A21B-1CBFE6EF7A16}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3B96CAE2-A6C0-4BA6-A21B-1CBFE6EF7A16}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3B96CAE2-A6C0-4BA6-A21B-1CBFE6EF7A16}.Release|Any CPU.Build.0 = Release|Any CPU
{4A81BD31-4B6B-4891-9CBA-7EB2513C9B5D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{4A81BD31-4B6B-4891-9CBA-7EB2513C9B5D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4A81BD31-4B6B-4891-9CBA-7EB2513C9B5D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{4A81BD31-4B6B-4891-9CBA-7EB2513C9B5D}.Release|Any CPU.Build.0 = Release|Any CPU
{04BCB1AF-1DC4-482F-9C3C-78FE537B7CCD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{04BCB1AF-1DC4-482F-9C3C-78FE537B7CCD}.Debug|Any CPU.Build.0 = Debug|Any CPU
{04BCB1AF-1DC4-482F-9C3C-78FE537B7CCD}.Release|Any CPU.ActiveCfg = Release|Any CPU
{04BCB1AF-1DC4-482F-9C3C-78FE537B7CCD}.Release|Any CPU.Build.0 = Release|Any CPU
{B15DE642-C58C-43AE-932E-A39B0896305A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B15DE642-C58C-43AE-932E-A39B0896305A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B15DE642-C58C-43AE-932E-A39B0896305A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B15DE642-C58C-43AE-932E-A39B0896305A}.Release|Any CPU.Build.0 = Release|Any CPU
{6B16B4FF-E179-4BA7-8E86-17CECEB6DEE0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{6B16B4FF-E179-4BA7-8E86-17CECEB6DEE0}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6B16B4FF-E179-4BA7-8E86-17CECEB6DEE0}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6B16B4FF-E179-4BA7-8E86-17CECEB6DEE0}.Release|Any CPU.Build.0 = Release|Any CPU
{27A5C157-B595-4158-8D58-8E5559A14E39}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{27A5C157-B595-4158-8D58-8E5559A14E39}.Debug|Any CPU.Build.0 = Debug|Any CPU
{27A5C157-B595-4158-8D58-8E5559A14E39}.Release|Any CPU.ActiveCfg = Release|Any CPU
{27A5C157-B595-4158-8D58-8E5559A14E39}.Release|Any CPU.Build.0 = Release|Any CPU
{7CD8650E-6358-40AB-9376-E0DB36B91C33}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{7CD8650E-6358-40AB-9376-E0DB36B91C33}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7CD8650E-6358-40AB-9376-E0DB36B91C33}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7CD8650E-6358-40AB-9376-E0DB36B91C33}.Release|Any CPU.Build.0 = Release|Any CPU
{919C2EDB-72D7-476E-919F-521BCA6A2C90}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{919C2EDB-72D7-476E-919F-521BCA6A2C90}.Debug|Any CPU.Build.0 = Debug|Any CPU
{919C2EDB-72D7-476E-919F-521BCA6A2C90}.Release|Any CPU.ActiveCfg = Release|Any CPU
{919C2EDB-72D7-476E-919F-521BCA6A2C90}.Release|Any CPU.Build.0 = Release|Any CPU
{FFE872A7-6686-4820-86F4-C081714CEFD8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{FFE872A7-6686-4820-86F4-C081714CEFD8}.Debug|Any CPU.Build.0 = Debug|Any CPU
{FFE872A7-6686-4820-86F4-C081714CEFD8}.Release|Any CPU.ActiveCfg = Release|Any CPU
{FFE872A7-6686-4820-86F4-C081714CEFD8}.Release|Any CPU.Build.0 = Release|Any CPU
{02272634-995C-44CE-B0E6-E9CFCDE21432}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{02272634-995C-44CE-B0E6-E9CFCDE21432}.Debug|Any CPU.Build.0 = Debug|Any CPU
{02272634-995C-44CE-B0E6-E9CFCDE21432}.Release|Any CPU.ActiveCfg = Release|Any CPU
{02272634-995C-44CE-B0E6-E9CFCDE21432}.Release|Any CPU.Build.0 = Release|Any CPU
{4DA1ECA1-5AAF-465D-A2C7-DECA20DC1065}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{4DA1ECA1-5AAF-465D-A2C7-DECA20DC1065}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4DA1ECA1-5AAF-465D-A2C7-DECA20DC1065}.Release|Any CPU.ActiveCfg = Release|Any CPU
{4DA1ECA1-5AAF-465D-A2C7-DECA20DC1065}.Release|Any CPU.Build.0 = Release|Any CPU
{7C107B4E-9BDE-4944-AAF3-EA8110D04F33}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{7C107B4E-9BDE-4944-AAF3-EA8110D04F33}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7C107B4E-9BDE-4944-AAF3-EA8110D04F33}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7C107B4E-9BDE-4944-AAF3-EA8110D04F33}.Release|Any CPU.Build.0 = Release|Any CPU
{D662DB3B-CB78-49A1-B45B-E518746C4DAF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D662DB3B-CB78-49A1-B45B-E518746C4DAF}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D662DB3B-CB78-49A1-B45B-E518746C4DAF}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D662DB3B-CB78-49A1-B45B-E518746C4DAF}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{46D3566C-FBBC-46BB-A9AB-454F348A003E} = {74D95657-7BC6-409F-8484-F963A128764A}
{3B96CAE2-A6C0-4BA6-A21B-1CBFE6EF7A16} = {EA937CAB-A9B1-474E-BF2A-A45399561B2E}
{4A81BD31-4B6B-4891-9CBA-7EB2513C9B5D} = {82050CA3-2111-49D6-B682-EA22191ADB99}
{04BCB1AF-1DC4-482F-9C3C-78FE537B7CCD} = {82050CA3-2111-49D6-B682-EA22191ADB99}
{B15DE642-C58C-43AE-932E-A39B0896305A} = {82050CA3-2111-49D6-B682-EA22191ADB99}
{6B16B4FF-E179-4BA7-8E86-17CECEB6DEE0} = {82050CA3-2111-49D6-B682-EA22191ADB99}
{E296D200-558C-4217-95C3-C239C88955D8} = {82050CA3-2111-49D6-B682-EA22191ADB99}
{27A5C157-B595-4158-8D58-8E5559A14E39} = {E296D200-558C-4217-95C3-C239C88955D8}
{7CD8650E-6358-40AB-9376-E0DB36B91C33} = {E296D200-558C-4217-95C3-C239C88955D8}
{919C2EDB-72D7-476E-919F-521BCA6A2C90} = {E296D200-558C-4217-95C3-C239C88955D8}
{FFE872A7-6686-4820-86F4-C081714CEFD8} = {E296D200-558C-4217-95C3-C239C88955D8}
{4FC21B51-8DAB-487D-BF66-32E674417EAB} = {82050CA3-2111-49D6-B682-EA22191ADB99}
{02272634-995C-44CE-B0E6-E9CFCDE21432} = {4FC21B51-8DAB-487D-BF66-32E674417EAB}
{4DA1ECA1-5AAF-465D-A2C7-DECA20DC1065} = {4FC21B51-8DAB-487D-BF66-32E674417EAB}
{7C107B4E-9BDE-4944-AAF3-EA8110D04F33} = {4FC21B51-8DAB-487D-BF66-32E674417EAB}
{D662DB3B-CB78-49A1-B45B-E518746C4DAF} = {4FC21B51-8DAB-487D-BF66-32E674417EAB}
EndGlobalSection
EndGlobal
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\..\AspNetCore\src\AspNetCore\HotChocolate.AspNetCore.csproj" />
<ProjectReference Include="..\..\..\Core\src\Core\HotChocolate.Core.csproj" />
<ProjectReference Include="..\..\src\ApolloFederation\ApolloFederation.csproj" />
<ProjectReference Include="..\..\..\..\AspNetCore\src\AspNetCore\HotChocolate.AspNetCore.csproj" />
<ProjectReference Include="..\..\..\..\Core\src\Core\HotChocolate.Core.csproj" />
<ProjectReference Include="..\..\..\src\ApolloFederation\HotChocolate.ApolloFederation.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
var builder = WebApplication.CreateBuilder(args);

builder.Services
.AddSingleton<UserRepository>();

builder.Services
.AddGraphQLServer()
.AddApolloFederation()
.AddQueryType<Query>()
.RegisterService<UserRepository>();

var app = builder.Build();

app.MapGraphQL();

app.Run();
var builder = WebApplication.CreateBuilder(args);

builder.Services
.AddSingleton<UserRepository>();

builder.Services
.AddGraphQLServer()
.AddApolloFederation()
.AddQueryType<Query>()
.RegisterService<UserRepository>();

var app = builder.Build();
app.MapGraphQL();
app.Run();
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\..\AspNetCore\src\AspNetCore\HotChocolate.AspNetCore.csproj" />
<ProjectReference Include="..\..\..\Core\src\Core\HotChocolate.Core.csproj" />
<ProjectReference Include="..\..\src\ApolloFederation\ApolloFederation.csproj" />
<ProjectReference Include="..\..\..\..\AspNetCore\src\AspNetCore\HotChocolate.AspNetCore.csproj" />
<ProjectReference Include="..\..\..\..\Core\src\Core\HotChocolate.Core.csproj" />
<ProjectReference Include="..\..\..\src\ApolloFederation\HotChocolate.ApolloFederation.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
var builder = WebApplication.CreateBuilder(args);

builder.Services
.AddGraphQLServer()
.AddApolloFederation()
.AddQueryType()
.AddType<Product>();

var app = builder.Build();

app.MapGraphQL();

app.Run();
var builder = WebApplication.CreateBuilder(args);

builder.Services
.AddGraphQLServer()
.AddApolloFederation()
.AddQueryType()
.AddType<Product>();

var app = builder.Build();
app.MapGraphQL();
app.Run();
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using HotChocolate.ApolloFederation;

namespace Products;

public class Product
{
public Product(string upc, string name, int price, int weight)
{
Upc = upc;
Name = name;
Price = price;
Weight = weight;
}

[Key]
public string Upc { get; }

public string Name { get; }

public int Price { get; }

public int Weight { get; }

[ReferenceResolver]
public static Product GetByIdAsync(
string upc,
ProductRepository productRepository)
=> productRepository.GetById(upc);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
namespace Products;

public class ProductRepository
{
private readonly Dictionary<string, Product> _products;

public ProductRepository()
{
_products = CreateProducts().ToDictionary(product => product.Upc);
}

public IEnumerable<Product> GetTop(int amount)
=> _products.Values.Take(amount);

public Product GetById(string upc)
=> _products[upc];

private static IEnumerable<Product> CreateProducts()
{
yield return new Product("1", "Table", 899, 100);
yield return new Product("2", "Couch", 1299, 1000);
yield return new Product("3", "Chair", 54, 50);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\..\AspNetCore\src\AspNetCore\HotChocolate.AspNetCore.csproj" />
<ProjectReference Include="..\..\..\Core\src\Core\HotChocolate.Core.csproj" />
<ProjectReference Include="..\..\src\ApolloFederation\ApolloFederation.csproj" />
<ProjectReference Include="..\..\..\..\AspNetCore\src\AspNetCore\HotChocolate.AspNetCore.csproj" />
<ProjectReference Include="..\..\..\..\Core\src\Core\HotChocolate.Core.csproj" />
<ProjectReference Include="..\..\..\src\ApolloFederation\HotChocolate.ApolloFederation.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
var builder = WebApplication.CreateBuilder(args);

builder.Services
.AddSingleton<ProductRepository>();

builder.Services
.AddGraphQLServer()
.AddApolloFederation()
.AddQueryType<Query>()
.RegisterService<ProductRepository>();

var app = builder.Build();

app.MapGraphQL();

app.Run();
var builder = WebApplication.CreateBuilder(args);

builder.Services
.AddSingleton<ProductRepository>();

builder.Services
.AddGraphQLServer()
.AddApolloFederation()
.AddQueryType<Query>()
.RegisterService<ProductRepository>();

var app = builder.Build();
app.MapGraphQL();
app.Run();
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
var builder = WebApplication.CreateBuilder(args);

builder.Services.AddSingleton<ReviewRepository>();
builder.Services.AddSingleton<UserRepository>();

builder.Services
.AddGraphQLServer()
.AddApolloFederation()
.AddQueryType()
.AddType<Review>()
.RegisterService<ReviewRepository>()
.RegisterService<UserRepository>();

var app = builder.Build();
app.MapGraphQL();
app.Run();
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\..\AspNetCore\src\AspNetCore\HotChocolate.AspNetCore.csproj" />
<ProjectReference Include="..\..\..\Core\src\Core\HotChocolate.Core.csproj" />
<ProjectReference Include="..\..\src\ApolloFederation\ApolloFederation.csproj" />
<ProjectReference Include="..\..\..\..\AspNetCore\src\AspNetCore\HotChocolate.AspNetCore.csproj" />
<ProjectReference Include="..\..\..\..\Core\src\Core\HotChocolate.Core.csproj" />
<ProjectReference Include="..\..\..\src\ApolloFederation\HotChocolate.ApolloFederation.csproj" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "build",
"command": "dotnet",
"type": "shell",
"args": [
"build",
// Ask dotnet build to generate full paths for file names.
"/property:GenerateFullPaths=true",
// Do not generate summary otherwise it leads to duplicate errors in Problems panel
"/consoleloggerparameters:NoSummary"
],
"group": "build",
"presentation": {
"reveal": "silent"
},
"problemMatcher": "$msCompile"
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

<ItemGroup Condition="'$(ImplicitUsings)' == 'enable'">
<Using Include="Accounts" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\..\..\AspNetCore\src\AspNetCore\HotChocolate.AspNetCore.csproj" />
<ProjectReference Include="..\..\..\..\Core\src\Core\HotChocolate.Core.csproj" />
<ProjectReference Include="..\..\..\src\ApolloFederation\HotChocolate.ApolloFederation.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
namespace Accounts;

public class User
{
public User(string id, string name, string username)
{
Id = id;
Name = name;
Username = username;
}

public string Id { get; }

public string Name { get; }

public string Username { get; }
}
Loading