Skip to content

Commit

Permalink
Set CacheControl via typed response headers
Browse files Browse the repository at this point in the history
  • Loading branch information
tobias-tengler committed Apr 13, 2024
1 parent caac508 commit 32bbfc8
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Text.Json;
using HotChocolate.Execution.Serialization;
using Microsoft.AspNetCore.Http;
using Microsoft.Net.Http.Headers;
#if !NET6_0_OR_GREATER
using Microsoft.Net.Http.Headers;
#endif
Expand Down Expand Up @@ -168,10 +169,10 @@ public async ValueTask FormatAsync(
response.StatusCode = statusCode;

if (result.ContextData is not null &&
result.ContextData.TryGetValue(CacheControlHeaderValue, out var value) &&
value is string cacheControlHeaderValue)
result.ContextData.TryGetValue(WellKnownContextData.CacheControlHeaderValue, out var value) &&
value is CacheControlHeaderValue cacheControlHeaderValue)
{
response.Headers.CacheControl = cacheControlHeaderValue;
response.GetTypedHeaders().CacheControl = cacheControlHeaderValue;
}

OnWriteResponseHeaders(queryResult, format, response.Headers);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
using System;
using System.Runtime.CompilerServices;
using HotChocolate.Execution.Processing;
using HotChocolate.Language;
using HotChocolate.Types;
using HotChocolate.Types.Introspection;
using HotChocolate.Utilities;
using Microsoft.Net.Http.Headers;
using IHasDirectives = HotChocolate.Types.IHasDirectives;

namespace HotChocolate.Caching;
Expand All @@ -13,10 +15,6 @@ namespace HotChocolate.Caching;
/// </summary>
internal sealed class CacheControlConstraintsOptimizer : IOperationOptimizer
{
private const string _cacheControlValueTemplate = "{0}, max-age={1}";
private const string _cacheControlPrivateScope = "private";
private const string _cacheControlPublicScope = "public";

public void OptimizeOperation(OperationOptimizerContext context)
{
if (context.Definition.Operation is not OperationType.Query ||
Expand All @@ -31,18 +29,12 @@ public void OptimizeOperation(OperationOptimizerContext context)

if (constraints.MaxAge is not null)
{
var cacheType = constraints.Scope switch
var headerValue = new CacheControlHeaderValue
{
CacheControlScope.Private => _cacheControlPrivateScope,
CacheControlScope.Public => _cacheControlPublicScope,
_ => throw ThrowHelper.UnexpectedCacheControlScopeValue(constraints.Scope),
Private = constraints.Scope == CacheControlScope.Private,
MaxAge = TimeSpan.FromSeconds(constraints.MaxAge.Value),
};

var headerValue = string.Format(
_cacheControlValueTemplate,
cacheType,
constraints.MaxAge);

context.ContextData.Add(
WellKnownContextData.CacheControlConstraints,
new ImmutableCacheConstraints(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,12 @@
</EmbeddedResource>
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'net8.0'">
<PackageReference Include="Microsoft.Net.Http.Headers" Version="8.0.4" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'net6.0' OR '$(TargetFramework)' == 'net7.0'">
<PackageReference Include="Microsoft.Net.Http.Headers" Version="2.2.0" />
</ItemGroup>

</Project>
7 changes: 4 additions & 3 deletions src/HotChocolate/Caching/src/Caching/QueryCacheMiddleware.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Threading.Tasks;
using HotChocolate.Execution;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Net.Http.Headers;
using static HotChocolate.WellKnownContextData;

namespace HotChocolate.Caching;
Expand Down Expand Up @@ -30,8 +31,8 @@ public async ValueTask InvokeAsync(IRequestContext context)
}

if (context.Operation?.ContextData is null ||
!context.Operation.ContextData.TryGetValue(CacheControlHeaderValue, out var value) ||
value is not string cacheControlHeaderValue)
!context.Operation.ContextData.TryGetValue(WellKnownContextData.CacheControlHeaderValue, out var value) ||
value is not CacheControlHeaderValue cacheControlHeaderValue)
{
return;
}
Expand All @@ -45,7 +46,7 @@ queryResult.ContextData is not null
? new ExtensionData(queryResult.ContextData)
: new ExtensionData();

contextData.Add(CacheControlHeaderValue, cacheControlHeaderValue);
contextData.Add(WellKnownContextData.CacheControlHeaderValue, cacheControlHeaderValue);

context.Result = new QueryResult(
data: queryResult.Data,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@
]
}
],
"Body": "{}"
"Body": "{\"data\":{\"field\":\"\"}}"
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
{
"Key": "Cache-Control",
"Value": [
"public, max-age=2000"
"max-age=2000"
]
}
],
Expand All @@ -15,5 +15,5 @@
]
}
],
"Body": "{}"
"Body": "{\"data\":{\"field\":\"\"}}"
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
{
"Key": "Cache-Control",
"Value": [
"public, max-age=0"
"max-age=0"
]
}
],
Expand All @@ -15,5 +15,5 @@
]
}
],
"Body": "{}"
"Body": "{\"data\":{\"field\":\"\"}}"
}

0 comments on commit 32bbfc8

Please sign in to comment.