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

Fix QueryResult creation in QueryCacheMiddleware #7057

Closed
wants to merge 2 commits into from
Closed
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
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>
32 changes: 18 additions & 14 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 All @@ -9,7 +10,7 @@ internal sealed class QueryCacheMiddleware
{
private readonly ICacheControlOptions _options;
private readonly RequestDelegate _next;

private QueryCacheMiddleware(
RequestDelegate next,
[SchemaService] ICacheControlOptionsAccessor optionsAccessor)
Expand All @@ -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,18 +46,21 @@ queryResult.ContextData is not null
? new ExtensionData(queryResult.ContextData)
: new ExtensionData();

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

context.Result = new QueryResult(
queryResult.Data,
queryResult.Errors,
queryResult.Extensions,
contextData,
queryResult.Items,
queryResult.Incremental,
queryResult.Label,
queryResult.Path,
queryResult.HasNext);
data: queryResult.Data,
errors: queryResult.Errors,
extension: queryResult.Extensions,
contextData: contextData,
items: queryResult.Items,
incremental: queryResult.Incremental,
label: queryResult.Label,
path: queryResult.Path,
hasNext: queryResult.HasNext,
// TODO: This is probably problematic
cleanupTasks: [],
Comment on lines +61 to +62
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This still needs to be addressed

isDataSet: queryResult.IsDataSet);
}
}

Expand All @@ -67,4 +71,4 @@ internal static RequestCoreMiddleware Create()
var middleware = new QueryCacheMiddleware(next, options);
return context => middleware.InvokeAsync(context);
};
}
}
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\":\"\"}}"
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>

<ItemGroup>
<InternalsVisibleTo Include="HotChocolate.Caching" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
<PackageReference Include="Microsoft.Bcl.AsyncInterfaces" Version="6.0.0" />
<PackageReference Include="System.Collections.Immutable" Version="6.0.0" />
Expand Down
Loading