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

Upgrade to run .net8 and opinionated changes #27

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
9 changes: 2 additions & 7 deletions Server.Tests/Server.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,26 +1,21 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<IsPackable>false</IsPackable>
</PropertyGroup>

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

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="xunit" Version="2.6.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.4" />
<PackageReference Include="Snapshooter.Xunit" Version="0.13.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="../Server/Server.csproj"/>
<ProjectReference Include="../Server/Server.csproj" />
</ItemGroup>

</Project>
6 changes: 1 addition & 5 deletions Server.Tests/SubgraphTest.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using ApolloGraphQL.HotChocolate.Federation.Two;
using HotChocolate;
using HotChocolate.Execution;
using Snapshooter.Xunit;
Expand All @@ -11,10 +10,7 @@ private static async Task<IRequestExecutor> CreateSchemaAsync()
=> await new ServiceCollection()
.AddSingleton<Data>()
.AddGraphQL()
.AddApolloFederationV2(new CustomSchema())
.AddType<ContactDirectiveType>()
.AddQueryType<Query>()
.RegisterService<Data>()
.AddGraphQLServices()
.BuildRequestExecutorAsync();

[Fact]
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
schema @contact(name: "Thing Server Team", url: "https:\/\/myteam.slack.com\/archives\/teams-chat-room-url", description: "send urgent issues to [#oncall](https:\/\/yourteam.slack.com\/archives\/oncall)") @link(url: "https:\/\/specs.apollo.dev\/federation\/v2.5", import: [ "@extends", "@external", "@key", "@inaccessible", "@override", "@provides", "@requires", "@shareable", "@tag", "FieldSet", "@composeDirective", "@interfaceObject" ]) {
schema @link(url: "https:\/\/specs.apollo.dev\/federation\/v2.5", import: [ "@extends", "@external", "@key", "@inaccessible", "@override", "@provides", "@requires", "@shareable", "@tag", "FieldSet", "@composeDirective", "@interfaceObject" ]) @contact(name: "Thing Server Team", url: "https:\/\/myteam.slack.com\/archives\/teams-chat-room-url", description: "send urgent issues to [#oncall](https:\/\/yourteam.slack.com\/archives\/oncall)") {
query: Query
mutation: Mutation
}

type Mutation {
createThing(thing: ThingInput!): Thing
}

type Query {
Expand All @@ -21,6 +26,11 @@ type _Service {
"Union of all types that key directive applied. This information is needed by the Apollo federation gateway."
union _Entity = Thing

input ThingInput {
id: String!
name: String
}

"Indicates to composition that the target element is accessible only to the authenticated supergraph users."
directive @authenticated on SCALAR | OBJECT | FIELD_DEFINITION | INTERFACE | ENUM

Expand Down
72 changes: 36 additions & 36 deletions Server.sln
Original file line number Diff line number Diff line change
@@ -1,36 +1,36 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.0.31903.59
MinimumVisualStudioVersion = 10.0.40219.1
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Server", "Server", "{2FE5A0A3-9A07-42E0-B6D3-486E17167C2D}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Server", "Server\Server.csproj", "{6DC201A7-45E1-4223-8E6B-779E6520859E}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Server.Tests", "Server.Tests", "{BE4BAB9F-3D26-4D64-AB1C-F37EDEBF5545}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Server.Tests", "Server.Tests\Server.Tests.csproj", "{AFD5A3E8-9B16-452E-A11D-A688CAA9D8E8}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{6DC201A7-45E1-4223-8E6B-779E6520859E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{6DC201A7-45E1-4223-8E6B-779E6520859E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6DC201A7-45E1-4223-8E6B-779E6520859E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6DC201A7-45E1-4223-8E6B-779E6520859E}.Release|Any CPU.Build.0 = Release|Any CPU
{AFD5A3E8-9B16-452E-A11D-A688CAA9D8E8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{AFD5A3E8-9B16-452E-A11D-A688CAA9D8E8}.Debug|Any CPU.Build.0 = Debug|Any CPU
{AFD5A3E8-9B16-452E-A11D-A688CAA9D8E8}.Release|Any CPU.ActiveCfg = Release|Any CPU
{AFD5A3E8-9B16-452E-A11D-A688CAA9D8E8}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{6DC201A7-45E1-4223-8E6B-779E6520859E} = {2FE5A0A3-9A07-42E0-B6D3-486E17167C2D}
{AFD5A3E8-9B16-452E-A11D-A688CAA9D8E8} = {BE4BAB9F-3D26-4D64-AB1C-F37EDEBF5545}
EndGlobalSection
EndGlobal

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.0.31903.59
MinimumVisualStudioVersion = 10.0.40219.1
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Server", "Server", "{2FE5A0A3-9A07-42E0-B6D3-486E17167C2D}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Server", "Server\Server.csproj", "{6DC201A7-45E1-4223-8E6B-779E6520859E}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Server.Tests", "Server.Tests", "{BE4BAB9F-3D26-4D64-AB1C-F37EDEBF5545}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Server.Tests", "Server.Tests\Server.Tests.csproj", "{AFD5A3E8-9B16-452E-A11D-A688CAA9D8E8}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{6DC201A7-45E1-4223-8E6B-779E6520859E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{6DC201A7-45E1-4223-8E6B-779E6520859E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6DC201A7-45E1-4223-8E6B-779E6520859E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6DC201A7-45E1-4223-8E6B-779E6520859E}.Release|Any CPU.Build.0 = Release|Any CPU
{AFD5A3E8-9B16-452E-A11D-A688CAA9D8E8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{AFD5A3E8-9B16-452E-A11D-A688CAA9D8E8}.Debug|Any CPU.Build.0 = Debug|Any CPU
{AFD5A3E8-9B16-452E-A11D-A688CAA9D8E8}.Release|Any CPU.ActiveCfg = Release|Any CPU
{AFD5A3E8-9B16-452E-A11D-A688CAA9D8E8}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{6DC201A7-45E1-4223-8E6B-779E6520859E} = {2FE5A0A3-9A07-42E0-B6D3-486E17167C2D}
{AFD5A3E8-9B16-452E-A11D-A688CAA9D8E8} = {BE4BAB9F-3D26-4D64-AB1C-F37EDEBF5545}
EndGlobalSection
EndGlobal
23 changes: 23 additions & 0 deletions Server/Extensions/GraphServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using ApolloGraphQL.HotChocolate.Federation.Two;
using HotChocolate.Execution.Configuration;

namespace Microsoft.Extensions.DependencyInjection;

public static class GraphServiceCollectionExtensions
{
public static IRequestExecutorBuilder AddGraphQLServices(this IRequestExecutorBuilder builder)
{
return builder
.AddApolloFederationV2(schemaConfiguration: schema =>
{
schema.Contact(
name: "Thing Server Team",
url: "https://myteam.slack.com/archives/teams-chat-room-url",
description: "send urgent issues to [#oncall](https://yourteam.slack.com/archives/oncall)");
})
.AddType<ContactDirectiveType>()
.AddQueryType<Query>()
.AddMutationType<Mutation>()
.RegisterService<Data>();
}
}
19 changes: 3 additions & 16 deletions Server/Program.cs
Original file line number Diff line number Diff line change
@@ -1,34 +1,21 @@
using ApolloGraphQL.HotChocolate.Federation.Two;
using HotChocolate.AspNetCore;
using HotChocolate.Execution;

var builder = WebApplication.CreateBuilder(args);
var builder = WebApplication.CreateSlimBuilder(args);

builder.Services
.AddSingleton<Data>();

builder.Services
.AddGraphQLServer()
.AddApolloFederationV2(new CustomSchema())
.AddType<ContactDirectiveType>()
.AddQueryType<Query>()
.AddMutationType<Mutation>()
.RegisterService<Data>()
.AddGraphQLServices()
.AddHttpRequestInterceptor<RouterAuthInterceptor>();

var port = Environment.GetEnvironmentVariable("PORT") ?? "4001";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to make sure that the template will listen on the PORT environment variable for deployments like Railway.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe simplify this to:

if (Environment.GetEnvironmentVariable("PORT") != null)
    builder.WebHost.UseUrls($"http://*:{Environment.GetEnvironmentVariable("PORT")}");

builder.WebHost.UseUrls($"http://*:{port}");

var app = builder.Build();

app.MapGraphQL();
var bananaCakePop = app.MapBananaCakePop("/");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this is a template for a new project, I think we should have the / route redirect to the GraphQL explorer experience. For a new user getting started, they might not know to visit the /graphql endpoint to explore the API and it would be good to add that line but make it simpler:

app.MapBananaCakePop("/")
    .WithOptions(new GraphQLToolOptions() { GraphQLEndpoint = "/graphql" });

bananaCakePop.WithOptions(new GraphQLToolOptions { GraphQLEndpoint = "/graphql" });

if (args.Length > 1 && args[0] == "schema" && args[1] == "export")
_ = app.RunWithGraphQLCommandsAsync(args);
else
app.Run();
app.RunWithGraphQLCommands(args);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is way nicer!


public sealed class RouterAuthInterceptor : DefaultHttpRequestInterceptor
{
Expand Down
41 changes: 41 additions & 0 deletions Server/Properties/launchSettings.json
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I love the opinionated launchSettings. I wanted this but wasn't quite sure of the current standards, lots of dotnet releases since I was with Xamarin.

Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{
"$schema": "http://json.schemastore.org/launchsettings.json",
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:49905",
"sslPort": 44344
}
},
"profiles": {
"http": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"launchUrl": "graphql",
"applicationUrl": "http://localhost:5239",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"https": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"launchUrl": "graphql",
"applicationUrl": "https://localhost:7037;http://localhost:5239",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchUrl": "graphql",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
7 changes: 2 additions & 5 deletions Server/Server.csproj
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<InvariantGlobalization>true</InvariantGlobalization>
</PropertyGroup>

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

<ItemGroup>
<PackageReference Include="HotChocolate.AspNetCore" Version="13.6.1" />
<PackageReference Include="HotChocolate.AspNetCore.CommandLine" Version="13.6.1" />
<PackageReference Include="ApolloGraphQL.HotChocolate.Federation" Version="1.3.0" />
</ItemGroup>

</Project>
10 changes: 0 additions & 10 deletions Server/Types/CustomSchema.cs

This file was deleted.