-
-
Notifications
You must be signed in to change notification settings - Fork 749
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
77 changed files
with
7,014 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<Project> | ||
<Import Project="$([MSBuild]::GetPathOfFileAbove('Directory.Build.props', '$(MSBuildThisFileDirectory)..\'))" /> | ||
|
||
<PropertyGroup> | ||
<TargetFrameworks>$(Library3TargetFrameworks)</TargetFrameworks> | ||
<Nullable>enable</Nullable> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
</PropertyGroup> | ||
|
||
</Project> |
36 changes: 36 additions & 0 deletions
36
src/HotChocolate/CostAnalysis/HotChocolate.CostAnalysis.sln
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +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}") = "src", "src", "{3F8C3AE9-6085-43F1-A593-CC7C0B9A4989}" | ||
EndProject | ||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HotChocolate.CostAnalysis", "src\CostAnalysis\HotChocolate.CostAnalysis.csproj", "{CBC13F2C-A2CC-43C7-A60B-DA83ADCD0314}" | ||
EndProject | ||
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test", "test", "{10D8894D-98C1-4F32-A6DE-8E2268E0D69B}" | ||
EndProject | ||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HotChocolate.CostAnalysis.Tests", "test\CostAnalysis.Tests\HotChocolate.CostAnalysis.Tests.csproj", "{6F56773B-1192-4951-8F4A-6C2E5835410D}" | ||
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 | ||
{CBC13F2C-A2CC-43C7-A60B-DA83ADCD0314}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | ||
{CBC13F2C-A2CC-43C7-A60B-DA83ADCD0314}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||
{CBC13F2C-A2CC-43C7-A60B-DA83ADCD0314}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||
{CBC13F2C-A2CC-43C7-A60B-DA83ADCD0314}.Release|Any CPU.Build.0 = Release|Any CPU | ||
{6F56773B-1192-4951-8F4A-6C2E5835410D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | ||
{6F56773B-1192-4951-8F4A-6C2E5835410D}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||
{6F56773B-1192-4951-8F4A-6C2E5835410D}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||
{6F56773B-1192-4951-8F4A-6C2E5835410D}.Release|Any CPU.Build.0 = Release|Any CPU | ||
EndGlobalSection | ||
GlobalSection(NestedProjects) = preSolution | ||
{CBC13F2C-A2CC-43C7-A60B-DA83ADCD0314} = {3F8C3AE9-6085-43F1-A593-CC7C0B9A4989} | ||
{6F56773B-1192-4951-8F4A-6C2E5835410D} = {10D8894D-98C1-4F32-A6DE-8E2268E0D69B} | ||
EndGlobalSection | ||
EndGlobal |
66 changes: 66 additions & 0 deletions
66
src/HotChocolate/CostAnalysis/src/CostAnalysis/Attributes/CostAttribute.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
using System.Reflection; | ||
using HotChocolate.CostAnalysis.Directives; | ||
using HotChocolate.Types; | ||
using HotChocolate.Types.Descriptors; | ||
|
||
namespace HotChocolate.CostAnalysis.Attributes; | ||
|
||
[AttributeUsage( | ||
AttributeTargets.Class | ||
| AttributeTargets.Enum | ||
| AttributeTargets.Method | ||
| AttributeTargets.Parameter | ||
| AttributeTargets.Property | ||
| AttributeTargets.Struct)] | ||
public sealed class CostAttribute : DescriptorAttribute | ||
{ | ||
private readonly string _weight; | ||
|
||
/// <summary> | ||
/// Applies the <c>@cost</c> directive. The purpose of the <c>cost</c> directive is to define a | ||
/// <c>weight</c> for GraphQL types, fields, and arguments. Static analysis can use these | ||
/// weights when calculating the overall cost of a query or response. | ||
/// </summary> | ||
/// <param name="weight"> | ||
/// The <c>weight</c> argument defines what value to add to the overall cost for every | ||
/// appearance, or possible appearance, of a type, field, argument, etc. | ||
/// </param> | ||
public CostAttribute(string weight) | ||
{ | ||
if (weight is null) | ||
{ | ||
throw new ArgumentNullException(nameof(weight)); | ||
} | ||
|
||
_weight = weight; | ||
} | ||
|
||
protected internal override void TryConfigure( | ||
IDescriptorContext context, | ||
IDescriptor descriptor, | ||
ICustomAttributeProvider element) | ||
{ | ||
switch (descriptor) | ||
{ | ||
case IArgumentDescriptor argumentDescriptor: | ||
argumentDescriptor.Directive(new CostDirective(_weight)); | ||
break; | ||
|
||
case IEnumTypeDescriptor enumTypeDescriptor: | ||
enumTypeDescriptor.Directive(new CostDirective(_weight)); | ||
break; | ||
|
||
case IInputFieldDescriptor inputFieldDescriptor: | ||
inputFieldDescriptor.Directive(new CostDirective(_weight)); | ||
break; | ||
|
||
case IObjectFieldDescriptor objectFieldDescriptor: | ||
objectFieldDescriptor.Directive(new CostDirective(_weight)); | ||
break; | ||
|
||
case IObjectTypeDescriptor objectTypeDescriptor: | ||
objectTypeDescriptor.Directive(new CostDirective(_weight)); | ||
break; | ||
} | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
src/HotChocolate/CostAnalysis/src/CostAnalysis/Attributes/ListSizeAttribute.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
using System.Reflection; | ||
using HotChocolate.CostAnalysis.Directives; | ||
using HotChocolate.Types; | ||
using HotChocolate.Types.Descriptors; | ||
|
||
namespace HotChocolate.CostAnalysis.Attributes; | ||
|
||
/// <summary> | ||
/// Applies the <c>@listSize</c> directive. The purpose of the <c>@listSize</c> directive is to | ||
/// either inform the static analysis about the size of returned lists (if that information is | ||
/// statically available), or to point the analysis to where to find that information. | ||
/// </summary> | ||
public sealed class ListSizeAttribute : ObjectFieldDescriptorAttribute | ||
{ | ||
private readonly int? _assumedSize; | ||
|
||
/// <summary> | ||
/// The maximum length of the list returned by this field. | ||
/// </summary> | ||
public int AssumedSize | ||
{ | ||
get => _assumedSize ?? 0; | ||
init => _assumedSize = value; | ||
} | ||
|
||
/// <summary> | ||
/// The arguments of this field with numeric type that are slicing arguments. Their value | ||
/// determines the size of the returned list. | ||
/// </summary> | ||
public string[]? SlicingArguments { get; set; } | ||
|
||
/// <summary> | ||
/// The subfield(s) that the list size applies to. | ||
/// </summary> | ||
public string[]? SizedFields { get; set; } | ||
|
||
/// <summary> | ||
/// Whether to require a single slicing argument in the query. If that is not the case (i.e., if | ||
/// none or multiple slicing arguments are present), the static analysis will throw an error. | ||
/// </summary> | ||
public bool RequireOneSlicingArgument { get; init; } = true; | ||
|
||
protected override void OnConfigure( | ||
IDescriptorContext context, | ||
IObjectFieldDescriptor descriptor, | ||
MemberInfo member) | ||
{ | ||
descriptor.Directive( | ||
new ListSizeDirective( | ||
_assumedSize, | ||
SlicingArguments?.ToList(), | ||
SizedFields?.ToList(), | ||
RequireOneSlicingArgument)); | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
src/HotChocolate/CostAnalysis/src/CostAnalysis/Caching/DefaultCostMetricsCache.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
using System.Diagnostics.CodeAnalysis; | ||
using HotChocolate.Utilities; | ||
|
||
namespace HotChocolate.CostAnalysis.Caching; | ||
|
||
internal sealed class DefaultCostMetricsCache(int capacity = 100) : ICostMetricsCache | ||
{ | ||
private readonly Cache<CostMetrics> _cache = new(capacity); | ||
|
||
public int Capacity => _cache.Capacity; | ||
|
||
public int Count => _cache.Usage; | ||
|
||
public bool TryGetCostMetrics( | ||
string operationId, | ||
[NotNullWhen(true)] out CostMetrics? costMetrics) | ||
=> _cache.TryGet(operationId, out costMetrics); | ||
|
||
public void TryAddCostMetrics( | ||
string operationId, | ||
CostMetrics costMetrics) | ||
=> _cache.GetOrCreate(operationId, () => costMetrics); | ||
|
||
public void Clear() => _cache.Clear(); | ||
} |
53 changes: 53 additions & 0 deletions
53
src/HotChocolate/CostAnalysis/src/CostAnalysis/Caching/ICostMetricsCache.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
using System.Diagnostics.CodeAnalysis; | ||
|
||
namespace HotChocolate.CostAnalysis.Caching; | ||
|
||
internal interface ICostMetricsCache | ||
{ | ||
/// <summary> | ||
/// Gets the maximum number of <c>CostMetrics</c> instances that can be cached. The default | ||
/// value is <c>100</c>. The minimum allowed value is <c>10</c>. | ||
/// </summary> | ||
int Capacity { get; } | ||
|
||
/// <summary> | ||
/// Gets the number of <c>CostMetrics</c> instances residing in the cache. | ||
/// </summary> | ||
int Count { get; } | ||
|
||
/// <summary> | ||
/// Tries to get a <c>CostMetrics</c> instance by <paramref name="operationId" />. | ||
/// </summary> | ||
/// <param name="operationId"> | ||
/// The internal operation ID. | ||
/// </param> | ||
/// <param name="costMetrics"> | ||
/// The <c>CostMetrics</c> instance that is associated with the ID or null | ||
/// if no <c>CostMetrics</c> instance was found that matches the specified ID. | ||
/// </param> | ||
/// <returns> | ||
/// <c>true</c> if a <c>CostMetrics</c> instance was found that matches the specified | ||
/// <paramref name="operationId"/>, otherwise <c>false</c>. | ||
/// </returns> | ||
bool TryGetCostMetrics( | ||
string operationId, | ||
[NotNullWhen(true)] out CostMetrics? costMetrics); | ||
|
||
/// <summary> | ||
/// Tries to add a new <c>CostMetrics</c> instance to the cache. | ||
/// </summary> | ||
/// <param name="operationId"> | ||
/// The internal operation ID. | ||
/// </param> | ||
/// <param name="costMetrics"> | ||
/// The <c>CostMetrics</c> instance that shall be cached. | ||
/// </param> | ||
void TryAddCostMetrics( | ||
string operationId, | ||
CostMetrics costMetrics); | ||
|
||
/// <summary> | ||
/// Clears all items from the cache. | ||
/// </summary> | ||
void Clear(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
namespace HotChocolate.CostAnalysis; | ||
|
||
/// <summary>https://ibm.github.io/graphql-specs/cost-spec.html#sec-__cost</summary> | ||
internal sealed class Cost(CostMetrics requestCosts) | ||
{ | ||
public CostMetrics RequestCosts { get; } = requestCosts; | ||
} |
Oops, something went wrong.