Skip to content

Commit d2be3dd

Browse files
authored
Fixed source generator issues. (#7967)
1 parent 35c8091 commit d2be3dd

File tree

80 files changed

+3434
-128
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

80 files changed

+3434
-128
lines changed

src/CookieCrumble/src/CookieCrumble/MarkdownLanguages.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@ public static class MarkdownLanguages
77
public const string Json = "json";
88
public const string Yaml = "yaml";
99
public const string Text = "text";
10+
public const string Sql = "sql";
1011
}

src/GreenDonut/src/GreenDonut.Data.EntityFramework/Expressions/QueryHelpers.cs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,39 @@ public static IQueryable<T> EnsureOrderPropsAreSelected<T>(
2323
return ReplaceSelector(query, updatedSelector);
2424
}
2525

26+
public static IQueryable<T> EnsureGroupPropsAreSelected<T, TKey>(
27+
IQueryable<T> query,
28+
Expression<Func<T, TKey>> keySelector)
29+
{
30+
var selector = ExtractCurrentSelector(query);
31+
if (selector is null)
32+
{
33+
return query;
34+
}
35+
36+
var body = GetMemberExpression(keySelector);
37+
if (body is null)
38+
{
39+
return query;
40+
}
41+
42+
var updatedSelector = AddPropertiesInSelector(selector, [body]);
43+
return ReplaceSelector(query, updatedSelector);
44+
45+
static MemberExpression? GetMemberExpression(Expression<Func<T, TKey>> keySelector)
46+
{
47+
var body = keySelector.Body;
48+
49+
if (body is UnaryExpression unaryExpr)
50+
{
51+
body = unaryExpr.Operand;
52+
}
53+
54+
return body as MemberExpression;
55+
}
56+
57+
}
58+
2659
private static Expression<Func<T, T>>? ExtractCurrentSelector<T>(
2760
IQueryable<T> query)
2861
{

src/GreenDonut/src/GreenDonut.Data.EntityFramework/Extensions/PagingQueryableExtensions.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,7 @@ public static async ValueTask<Dictionary<TKey, Page<TValue>>> ToBatchPageAsync<T
363363
}
364364

365365
source = QueryHelpers.EnsureOrderPropsAreSelected(source);
366+
source = QueryHelpers.EnsureGroupPropsAreSelected(source, keySelector);
366367

367368
// we need to move the ordering into the select expression we are constructing
368369
// so that the groupBy will not remove it. The first thing we do here is to extract the order expressions
@@ -528,7 +529,7 @@ private sealed class InterceptorHolder
528529
public PagingQueryInterceptor? Interceptor { get; set; }
529530
}
530531

531-
private static PagingQueryInterceptor? TryGetQueryInterceptor()
532+
internal static PagingQueryInterceptor? TryGetQueryInterceptor()
532533
=> _interceptor.Value?.Interceptor;
533534

534535
internal static void SetQueryInterceptor(PagingQueryInterceptor pagingQueryInterceptor)

src/GreenDonut/src/GreenDonut.Data.EntityFramework/PagingQueryInterceptor.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,16 @@ public void Dispose()
3737
_disposed = true;
3838
}
3939
}
40+
41+
/// <summary>
42+
/// This method allows to publish a query to the current interceptor.
43+
/// </summary>
44+
/// <param name="query">
45+
/// The query that is about to be executed.
46+
/// </param>
47+
/// <typeparam name="T">
48+
/// The type of the items in the query.
49+
/// </typeparam>
50+
public static void Publish<T>(IQueryable<T> query)
51+
=> PagingQueryableExtensions.TryGetQueryInterceptor()?.OnBeforeExecute(query);
4052
}

src/GreenDonut/src/GreenDonut.Data.Primitives/SortDefinition.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,34 @@ public SortDefinition<T> AddDescending<TResult>(
9696
return new SortDefinition<T>(operations);
9797
}
9898

99+
/// <summary>
100+
/// Applies the sort definition if the definition is empty.
101+
/// </summary>
102+
/// <param name="applyIfEmpty">
103+
/// The sort definition to apply if the current definition is empty.
104+
/// </param>
105+
/// <returns>
106+
/// The updated sort definition.
107+
/// </returns>
108+
/// <exception cref="ArgumentNullException">
109+
/// Thrown when the <paramref name="applyIfEmpty"/> is <see langword="null"/>.
110+
/// </exception>
111+
public SortDefinition<T> IfEmpty(
112+
Func<SortDefinition<T>, SortDefinition<T>> applyIfEmpty)
113+
{
114+
if (applyIfEmpty == null)
115+
{
116+
throw new ArgumentNullException(nameof(applyIfEmpty));
117+
}
118+
119+
if (Operations.Length == 0)
120+
{
121+
return applyIfEmpty(this);
122+
}
123+
124+
return this;
125+
}
126+
99127
/// <summary>
100128
/// Returns a string representation of the sort definition.
101129
/// </summary>

src/GreenDonut/src/GreenDonut.Data/Extensions/GreenDonutPaginationBatchingDataLoaderExtensions.cs

Lines changed: 7 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Runtime.CompilerServices;
44
using GreenDonut.Data.Internal;
55
using static GreenDonut.Data.GreenDonutPredicateDataLoaderExtensions;
6+
using static GreenDonut.Data.Internal.DataLoaderStateHelper;
67

78
// ReSharper disable once CheckNamespace
89
namespace GreenDonut.Data;
@@ -44,13 +45,13 @@ public static IDataLoader<TKey, Page<TValue>> WithPagingArguments<TKey, TValue>(
4445
/// Branches a DataLoader with the provided <see cref="PagingArguments"/>.
4546
/// </summary>
4647
/// <param name="dataLoader">
47-
/// The DataLoader that shall be branched.
48+
/// The DataLoader that shall be branched.
4849
/// </param>
4950
/// <param name="pagingArguments">
50-
/// The paging arguments that shall exist as state in the branched DataLoader.
51+
/// The paging arguments that shall exist as state in the branched DataLoader.
5152
/// </param>
5253
/// <param name="context">
53-
/// The query context that shall exist as state in the branched DataLoader.
54+
/// The query context that shall exist as state in the branched DataLoader.
5455
/// </param>
5556
/// <typeparam name="TKey">
5657
/// The key type of the DataLoader.
@@ -84,38 +85,6 @@ private static IDataLoader<TKey, Page<TValue>> WithInternal<TKey, TValue>(
8485
var branchKey = pagingArguments.ComputeHash(context);
8586
var state = new PagingState<TValue>(pagingArguments, context);
8687
return (IQueryDataLoader<TKey, Page<TValue>>)dataLoader.Branch(branchKey, CreateBranch, state);
87-
88-
static IDataLoader CreateBranch(
89-
string branchKey,
90-
IDataLoader<TKey, Page<TValue>> dataLoader,
91-
PagingState<TValue> state)
92-
{
93-
var branch = new QueryDataLoader<TKey, Page<TValue>>(
94-
(DataLoaderBase<TKey, Page<TValue>>)dataLoader,
95-
branchKey);
96-
97-
branch.SetState(DataLoaderStateKeys.PagingArgs, state.PagingArgs);
98-
99-
if (state.Context is not null)
100-
{
101-
if (state.Context.Selector is not null)
102-
{
103-
branch.SetState(DataLoaderStateKeys.Selector, state.Context.Selector);
104-
}
105-
106-
if (state.Context.Predicate is not null)
107-
{
108-
branch.SetState(DataLoaderStateKeys.Predicate, state.Context.Predicate);
109-
}
110-
111-
if (state.Context.Sorting is not null)
112-
{
113-
branch.SetState(DataLoaderStateKeys.Sorting, state.Context.Sorting);
114-
}
115-
}
116-
117-
return branch;
118-
}
11988
}
12089

12190
/// <summary>
@@ -166,7 +135,7 @@ public static IDataLoader<TKey, Page<TValue>> Select<TElement, TKey, TValue>(
166135

167136
var branchKey = selector.ComputeHash();
168137
var state = new QueryState(DataLoaderStateKeys.Selector, new DefaultSelectorBuilder(selector));
169-
return (IQueryDataLoader<TKey, Page<TValue>>)dataLoader.Branch(branchKey, DataLoaderStateHelper.CreateBranch,
138+
return (IQueryDataLoader<TKey, Page<TValue>>)dataLoader.Branch(branchKey, CreateBranch,
170139
state);
171140
}
172141

@@ -209,7 +178,7 @@ public static IDataLoader<TKey, Page<TValue>> Where<TKey, TValue>(
209178
var branchKey = predicate.ComputeHash();
210179
var state = new QueryState(DataLoaderStateKeys.Predicate,
211180
GetOrCreateBuilder(dataLoader.ContextData, predicate));
212-
return (IQueryDataLoader<TKey, Page<TValue>>)dataLoader.Branch(branchKey, DataLoaderStateHelper.CreateBranch,
181+
return (IQueryDataLoader<TKey, Page<TValue>>)dataLoader.Branch(branchKey, CreateBranch,
213182
state);
214183
}
215184

@@ -251,7 +220,7 @@ public static IDataLoader<TKey, Page<TValue>> OrderBy<TKey, TValue>(
251220

252221
var branchKey = sortDefinition.ComputeHash();
253222
var state = new QueryState(DataLoaderStateKeys.Sorting, sortDefinition);
254-
return (IQueryDataLoader<TKey, Page<TValue>>)dataLoader.Branch(branchKey, DataLoaderStateHelper.CreateBranch,
223+
return (IQueryDataLoader<TKey, Page<TValue>>)dataLoader.Branch(branchKey, CreateBranch,
255224
state);
256225
}
257226

@@ -391,6 +360,4 @@ private static int EstimateIntLength(int? value)
391360

392361
return length + 2;
393362
}
394-
395-
private readonly record struct PagingState<T>(PagingArguments PagingArgs, QueryContext<T>? Context = null);
396363
}

src/GreenDonut/src/GreenDonut.Data/Extensions/GreenDonutQueryContextDataLoaderExtensions.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using GreenDonut.Data.Internal;
2+
using static GreenDonut.Data.Internal.DataLoaderStateHelper;
23

34
// ReSharper disable once CheckNamespace
45
namespace GreenDonut.Data;
@@ -48,7 +49,7 @@ public static IDataLoader<TKey, TValue> With<TKey, TValue>(
4849
}
4950

5051
var branchKey = context.ComputeHash();
51-
return (IQueryDataLoader<TKey, TValue>)dataLoader.Branch(branchKey, DataLoaderStateHelper.CreateBranch, context);
52+
return (IQueryDataLoader<TKey, TValue>)dataLoader.Branch(branchKey, CreateBranch, context);
5253
}
5354

5455
/// <summary>
@@ -91,7 +92,7 @@ public static IDataLoader<TKey, TValue[]> With<TKey, TValue>(
9192
}
9293

9394
var branchKey = context.ComputeHash();
94-
return (IQueryDataLoader<TKey, TValue[]>)dataLoader.Branch(branchKey, DataLoaderStateHelper.CreateBranch, context);
95+
return (IQueryDataLoader<TKey, TValue[]>)dataLoader.Branch(branchKey, CreateBranch, context);
9596
}
9697

9798
/// <summary>
@@ -134,6 +135,6 @@ public static IDataLoader<TKey, List<TValue>> With<TKey, TValue>(
134135
}
135136

136137
var branchKey = context.ComputeHash();
137-
return (IQueryDataLoader<TKey, List<TValue>>)dataLoader.Branch(branchKey, DataLoaderStateHelper.CreateBranch, context);
138+
return (IQueryDataLoader<TKey, List<TValue>>)dataLoader.Branch(branchKey, CreateBranch, context);
138139
}
139140
}

src/GreenDonut/src/GreenDonut.Data/Internal/DataLoaderStateHelper.cs

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,12 @@ internal static IDataLoader CreateBranch<TKey, TValue, TElement>(
2929

3030
if (state.Selector is not null)
3131
{
32-
branch.SetState(DataLoaderStateKeys.Selector, state.Selector);
32+
branch.SetState(DataLoaderStateKeys.Selector, new DefaultSelectorBuilder(state.Selector));
3333
}
3434

3535
if (state.Predicate is not null)
3636
{
37-
branch.SetState(DataLoaderStateKeys.Predicate, state.Predicate);
37+
branch.SetState(DataLoaderStateKeys.Predicate, new DefaultPredicateBuilder(state.Predicate));
3838
}
3939

4040
if (state.Sorting is not null)
@@ -45,6 +45,39 @@ internal static IDataLoader CreateBranch<TKey, TValue, TElement>(
4545
return branch;
4646
}
4747

48+
internal static IDataLoader CreateBranch<TKey, TValue, TElement>(
49+
string branchKey,
50+
IDataLoader<TKey, Page<TElement>> dataLoader,
51+
PagingState<TValue> state)
52+
where TKey : notnull
53+
{
54+
var branch = new QueryDataLoader<TKey, Page<TValue>>(
55+
(DataLoaderBase<TKey, Page<TValue>>)dataLoader,
56+
branchKey);
57+
58+
branch.SetState(DataLoaderStateKeys.PagingArgs, state.PagingArgs);
59+
60+
if (state.Context is not null)
61+
{
62+
if (state.Context.Selector is not null)
63+
{
64+
branch.SetState(DataLoaderStateKeys.Selector, new DefaultSelectorBuilder(state.Context.Selector));
65+
}
66+
67+
if (state.Context.Predicate is not null)
68+
{
69+
branch.SetState(DataLoaderStateKeys.Predicate, new DefaultPredicateBuilder(state.Context.Predicate));
70+
}
71+
72+
if (state.Context.Sorting is not null)
73+
{
74+
branch.SetState(DataLoaderStateKeys.Sorting, state.Context.Sorting);
75+
}
76+
}
77+
78+
return branch;
79+
}
80+
4881
internal static string ComputeHash<TValue>(this QueryContext<TValue> state)
4982
{
5083
var hasher = ExpressionHasherPool.Shared.Get();

src/GreenDonut/src/GreenDonut.Data/Internal/DataLoaderStateKeys.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@ internal static class DataLoaderStateKeys
55
public const string Selector = "GreenDonut.Data.Selector";
66
public const string Predicate = "GreenDonut.Data.Predicate";
77
public const string Sorting = "GreenDonut.Data.Sorting";
8-
public const string PagingArgs = "GreenDonut.Data.Pagination.PagingArgs";
8+
public const string PagingArgs = "GreenDonut.Data.PagingArgs";
99
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
namespace GreenDonut.Data.Internal;
2+
3+
internal sealed record PagingState<T>(PagingArguments PagingArgs, QueryContext<T>? Context = null);
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
namespace GreenDonut.Data.Internal;
22

3-
internal readonly record struct QueryState(string Key, object Value);
3+
internal sealed record QueryState(string Key, object Value);

src/HotChocolate/Core/src/Execution/Processing/Tasks/ResolverTask.Execute.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ private async ValueTask ExecuteResolverPipelineAsync(CancellationToken cancellat
133133
var serviceScope = _operationContext.Services.CreateAsyncScope();
134134
_context.Services = serviceScope.ServiceProvider;
135135
_context.RegisterForCleanup(serviceScope.DisposeAsync);
136-
_operationContext.ServiceScopeInitializer.Initialize(_context.RequestServices, _context.Services);
136+
_operationContext.ServiceScopeInitializer.Initialize(_context, _context.RequestServices, _context.Services);
137137
}
138138

139139
await _context.ResolverPipeline!(_context).ConfigureAwait(false);

src/HotChocolate/Core/src/Types.Analyzers/FileBuilders/DataLoaderFileBuilder.cs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -316,24 +316,25 @@ public void WriteDataLoaderLoadMethod(
316316
((INamedTypeSymbol)parameter.Type).TypeArguments[0].ToFullyQualified(),
317317
DataLoaderInfo.Sorting);
318318
_writer.WriteLine();
319-
_writer.WriteIndentedLine(
320-
"if({0}_selector is null && {0}_predicate is null && {0}_sortDefinition is null)");
321-
_writer.WriteIndentedLine("{");
322-
_writer.IncreaseIndent();
319+
323320
_writer.WriteIndentedLine(
324321
"var {0} = global::{1}<{2}>.Empty;",
325322
parameter.VariableName,
326323
WellKnownTypes.QueryContext,
327324
((INamedTypeSymbol)parameter.Type).TypeArguments[0].ToFullyQualified());
328-
_writer.DecreaseIndent();
329-
_writer.WriteIndentedLine("}");
330-
_writer.WriteLine();
331325
_writer.WriteIndentedLine(
332-
"var {0} = new global::{1}<{2}>({0}_selector?, " +
326+
"if({0}_selector is not null || {0}_predicate is not null || {0}_sortDefinition is not null)",
327+
parameter.VariableName);
328+
_writer.WriteIndentedLine("{");
329+
_writer.IncreaseIndent();
330+
_writer.WriteIndentedLine(
331+
"{0} = new global::{1}<{2}>({0}_selector, " +
333332
"{0}_predicate, {0}_sortDefinition);",
334333
parameter.VariableName,
335334
WellKnownTypes.QueryContext,
336335
((INamedTypeSymbol)parameter.Type).TypeArguments[0].ToFullyQualified());
336+
_writer.DecreaseIndent();
337+
_writer.WriteIndentedLine("}");
337338
}
338339
else if (parameter.Kind is DataLoaderParameterKind.PagingArguments)
339340
{

src/HotChocolate/Core/src/Types.Analyzers/FileBuilders/ModuleFileBuilder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ public void WriteRegisterRootTypeExtension(string key, OperationType operation,
105105
using (_writer.IncreaseIndent())
106106
{
107107
_writer.WriteIndentedLine("\"{0}\",", key);
108-
_writer.WriteIndentedLine("global::HotChocolate.Types.OperationType.{0},", operation);
108+
_writer.WriteIndentedLine("global::HotChocolate.Types.OperationTypeNames.{0},", operation);
109109
_writer.WriteIndentedLine("() => {0}.Initialize));", extensionType);
110110
}
111111
}

0 commit comments

Comments
 (0)