Skip to content
Draft
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
1 change: 1 addition & 0 deletions src/Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
<PackageVersion Include="BenchmarkDotNet.Diagnostics.dotMemory" Version="0.15.8" />
<PackageVersion Include="ChilliCream.ModelContextProtocol.AspNetCore" Version="1.4.1" />
<PackageVersion Include="ChilliCream.Nitro.App" Version="$(NitroVersion)" />
<PackageVersion Include="ChilliCream.Nitro.App.Embed" Version="$(NitroVersion)" />
<PackageVersion Include="CliWrap" Version="3.10.5" />
<PackageVersion Include="Confluent.Kafka" Version="2.14.2" />
<PackageVersion Include="Dapper" Version="2.1.79" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Diagnostics.CodeAnalysis;
using ChilliCream.Nitro.App;
using ChilliCream.Nitro.App.Embed;
using HotChocolate.AspNetCore;
using HotChocolate.AspNetCore.Extensions;
using Microsoft.AspNetCore.Http;
Expand Down Expand Up @@ -86,7 +87,7 @@ public static GraphQLEndpointConventionBuilder MapGraphQL(
var pattern = Parse(path + "/{**slug}");
var requestPipeline = endpointRouteBuilder.CreateApplicationBuilder();
var services = endpointRouteBuilder.ServiceProvider;
requestPipeline.MapGraphQL(path, schemaNameOrDefault);
MapGraphQLPipeline(requestPipeline, path, schemaNameOrDefault, mountedOnEndpoint: true);
var serverOptions = services.GetServerOptions(schemaNameOrDefault);

return new GraphQLEndpointConventionBuilder(
Expand Down Expand Up @@ -123,6 +124,15 @@ public static IApplicationBuilder MapGraphQL(
ArgumentNullException.ThrowIfNull(applicationBuilder);
ArgumentException.ThrowIfNullOrEmpty(schemaName);

return MapGraphQLPipeline(applicationBuilder, path, schemaName, mountedOnEndpoint: false);
}

private static IApplicationBuilder MapGraphQLPipeline(
IApplicationBuilder applicationBuilder,
PathString path,
string schemaName,
bool mountedOnEndpoint)
{
path = path.ToString().TrimEnd('/');

var services = applicationBuilder.ApplicationServices;
Expand All @@ -144,7 +154,20 @@ public static IApplicationBuilder MapGraphQL(
executor, serverOptions, path, MiddlewareRoutingType.Integrated))
.UseWhen(
context => context.MayReachNitroApp(path),
branch => branch.UseNitroApp(path, serverOptions.Tool))
branch =>
{
if (mountedOnEndpoint)
{
branch.UseNitroApp(serverOptions.Tool, EmbeddedNitroApp.AssetSource);
}
else
{
branch.Use(NitroAppMiddleware.Create(
serverOptions.Tool,
path,
embeddedAssetSource: EmbeddedNitroApp.AssetSource));
}
})
.Use(MiddlewareFactory.CreateHttpUnsupportedRequestMiddleware(
executor,
serverOptions,
Expand Down Expand Up @@ -529,7 +552,7 @@ public static NitroAppEndpointConventionBuilder MapNitroApp(
var nitroOptions = new NitroAppOptions { GraphQLEndpoint = relativeRequestPath };

requestPipeline
.UseNitroApp(toolPath, nitroOptions)
.UseNitroApp(nitroOptions, EmbeddedNitroApp.AssetSource)
.Use(_ => context =>
{
context.Response.StatusCode = 404;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using ChilliCream.Nitro.App;
using HotChocolate.AspNetCore.Formatters;
using HotChocolate.AspNetCore.Utilities;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Routing;

namespace HotChocolate.AspNetCore;

Expand Down Expand Up @@ -29,7 +31,7 @@ public static bool MayReachNitroApp(this HttpContext context, PathString path)
{
var request = context.Request;

if (!request.IsGetOrHeadMethod() || !request.IsEndpointPath(path))
if (!request.IsGetOrHeadMethod() || !IsGraphQLEndpointPath(context, path))
{
return true;
}
Expand All @@ -40,6 +42,13 @@ public static bool MayReachNitroApp(this HttpContext context, PathString path)
&& DefaultHttpResponseFormatter.PrefersHtml(acceptHeader.AcceptMediaTypes);
}

// Inside a route group, path is relative to the group. On an endpoint, an empty slug route
// value identifies the endpoint path instead.
private static bool IsGraphQLEndpointPath(HttpContext context, PathString path)
=> context.Request.IsEndpointPath(path)
|| (context.GetEndpoint() is not null
&& string.IsNullOrEmpty(context.GetRouteValue(NitroAppMiddleware.SlugRouteValueName)?.ToString()));

public static string? TryGetCostSwitch(this HttpContext context)
{
var headers = context.Request.Headers;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

<ItemGroup>
<PackageReference Include="ChilliCream.Nitro.App" />
<PackageReference Include="ChilliCream.Nitro.App.Embed" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

<ItemGroup>
<PackageReference Include="ChilliCream.Nitro.App" />
<PackageReference Include="ChilliCream.Nitro.App.Embed" />
</ItemGroup>

<ItemGroup>
Expand Down
Loading
Loading