diff --git a/src/HotChocolate/AspNetCore/src/AspNetCore/Serialization/DefaultHttpResponseFormatter.cs b/src/HotChocolate/AspNetCore/src/AspNetCore/Serialization/DefaultHttpResponseFormatter.cs index dd3b6150003..ebda46135a2 100644 --- a/src/HotChocolate/AspNetCore/src/AspNetCore/Serialization/DefaultHttpResponseFormatter.cs +++ b/src/HotChocolate/AspNetCore/src/AspNetCore/Serialization/DefaultHttpResponseFormatter.cs @@ -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 @@ -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); diff --git a/src/HotChocolate/Caching/src/Caching/CacheControlConstraintsOptimizer.cs b/src/HotChocolate/Caching/src/Caching/CacheControlConstraintsOptimizer.cs index c5ab0c77759..5fc510342f4 100644 --- a/src/HotChocolate/Caching/src/Caching/CacheControlConstraintsOptimizer.cs +++ b/src/HotChocolate/Caching/src/Caching/CacheControlConstraintsOptimizer.cs @@ -1,3 +1,5 @@ +using System; +using System.Net.Http.Headers; using System.Runtime.CompilerServices; using HotChocolate.Execution.Processing; using HotChocolate.Language; @@ -13,10 +15,6 @@ namespace HotChocolate.Caching; /// 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 || @@ -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( diff --git a/src/HotChocolate/Caching/src/Caching/QueryCacheMiddleware.cs b/src/HotChocolate/Caching/src/Caching/QueryCacheMiddleware.cs index 53aa4f54fcd..a7ac945e308 100644 --- a/src/HotChocolate/Caching/src/Caching/QueryCacheMiddleware.cs +++ b/src/HotChocolate/Caching/src/Caching/QueryCacheMiddleware.cs @@ -1,3 +1,4 @@ +using System.Net.Http.Headers; using System.Threading.Tasks; using HotChocolate.Execution; using Microsoft.Extensions.DependencyInjection; @@ -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; } @@ -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,