Skip to content

Commit

Permalink
Add ModifyPagingOptions (#7285)
Browse files Browse the repository at this point in the history
  • Loading branch information
tobias-tengler authored Jul 23, 2024
1 parent b6403e2 commit 2a3e24f
Show file tree
Hide file tree
Showing 22 changed files with 140 additions and 120 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public static partial class SchemaRequestExecutorBuilderExtensions
/// <exception cref="ArgumentNullException">
/// The <paramref name="builder"/> is <c>null</c>.
/// </exception>
[Obsolete("Use ModifyPagingOptions instead.")]
public static IRequestExecutorBuilder SetPagingOptions(
this IRequestExecutorBuilder builder,
PagingOptions options)
Expand All @@ -37,4 +38,30 @@ public static IRequestExecutorBuilder SetPagingOptions(

return builder.ConfigureSchema(s => s.SetPagingOptions(options));
}

/// <summary>
/// Modifies the global paging options.
/// </summary>
/// <param name="builder">
/// The <see cref="IRequestExecutorBuilder"/>.
/// </param>
/// <param name="configure">
/// A delegate to modify the paging options.
/// </param>
public static IRequestExecutorBuilder ModifyPagingOptions(
this IRequestExecutorBuilder builder,
Action<PagingOptions> configure)
{
if (builder is null)
{
throw new ArgumentNullException(nameof(builder));
}

if (configure is null)
{
throw new ArgumentNullException(nameof(configure));
}

return builder.ConfigureSchema(s => s.ModifyPagingOptions(configure));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ namespace Microsoft.Extensions.DependencyInjection;
/// </summary>
public static partial class SchemaRequestExecutorBuilderExtensions
{
[Obsolete("Use ModifyOptions instead.")]
public static IRequestExecutorBuilder SetOptions(
this IRequestExecutorBuilder builder,
IReadOnlySchemaOptions options)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public static IObjectFieldDescriptor UsePaging<TNodeType, TEntity>(
this IObjectFieldDescriptor descriptor,
GetCursorPagingProvider? resolvePagingProvider = null,
string? connectionName = null,
PagingOptions options = default)
PagingOptions? options = null)
where TNodeType : class, IOutputType =>
UsePaging<TNodeType>(
descriptor,
Expand Down Expand Up @@ -73,7 +73,7 @@ public static IObjectFieldDescriptor UsePaging<TNodeType>(
Type? entityType = null,
GetCursorPagingProvider? resolvePagingProvider = null,
string? connectionName = null,
PagingOptions options = default)
PagingOptions? options = null)
where TNodeType : class, IOutputType =>
UsePaging(
descriptor,
Expand Down Expand Up @@ -112,7 +112,7 @@ public static IObjectFieldDescriptor UsePaging(
Type? entityType = null,
GetCursorPagingProvider? resolvePagingProvider = null,
string? connectionName = null,
PagingOptions options = default)
PagingOptions? options = null)
{
if (descriptor is null)
{
Expand All @@ -131,7 +131,7 @@ public static IObjectFieldDescriptor UsePaging(
.Extend()
.OnBeforeCreate((c, d) =>
{
var pagingOptions = c.GetSettings(options);
var pagingOptions = c.GetPagingOptions(options);
var backward = pagingOptions.AllowBackwardPagination ?? AllowBackwardPagination;
d.State = d.State.Add(WellKnownContextData.PagingOptions, pagingOptions);
d.Flags |= FieldFlags.Connection;
Expand Down Expand Up @@ -199,15 +199,15 @@ d.Type is ExtendedTypeReference extendedTypeRef &&
public static IInterfaceFieldDescriptor UsePaging<TNodeType>(
this IInterfaceFieldDescriptor descriptor,
string? connectionName = null,
PagingOptions options = default)
PagingOptions? options = null)
where TNodeType : class, IOutputType =>
UsePaging(descriptor, typeof(TNodeType), connectionName, options);

public static IInterfaceFieldDescriptor UsePaging(
this IInterfaceFieldDescriptor descriptor,
Type? nodeType = null,
string? connectionName = null,
PagingOptions options = default)
PagingOptions? options = null)
{
if (descriptor is null)
{
Expand All @@ -218,7 +218,7 @@ public static IInterfaceFieldDescriptor UsePaging(
.Extend()
.OnBeforeCreate((c, d) =>
{
var pagingOptions = c.GetSettings(options);
var pagingOptions = c.GetPagingOptions(options);
var backward = pagingOptions.AllowBackwardPagination ?? AllowBackwardPagination;
d.State = d.State.Add(WellKnownContextData.PagingOptions, pagingOptions);
d.Flags |= FieldFlags.Connection;
Expand Down Expand Up @@ -332,7 +332,7 @@ private static TypeReference CreateConnectionTypeRef(
MemberInfo? resolverMember,
string? connectionName,
TypeReference? nodeType,
PagingOptions options)
PagingOptions? options)
{
var typeInspector = context.TypeInspector;

Expand All @@ -355,7 +355,7 @@ private static TypeReference CreateConnectionTypeRef(
namedType);
}

options = context.GetSettings(options);
options = context.GetPagingOptions(options);

// last but not least we create a type reference that can be put on the field definition
// to tell the type discovery that this field needs this result type.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public static IObjectFieldDescriptor UseOffsetPaging<TSchemaType>(
Type? itemType = null,
GetOffsetPagingProvider? resolvePagingProvider = null,
string? collectionSegmentName = null,
PagingOptions options = default)
PagingOptions? options = null)
where TSchemaType : IOutputType =>
UseOffsetPaging(
descriptor,
Expand Down Expand Up @@ -86,7 +86,7 @@ public static IObjectFieldDescriptor UseOffsetPaging(
Type? entityType = null,
GetOffsetPagingProvider? resolvePagingProvider = null,
string? collectionSegmentName = null,
PagingOptions options = default)
PagingOptions? options = null)
{
if (descriptor is null)
{
Expand All @@ -107,7 +107,7 @@ public static IObjectFieldDescriptor UseOffsetPaging(
.Extend()
.OnBeforeCreate((c, d) =>
{
var pagingOptions = c.GetSettings(options);
var pagingOptions = c.GetPagingOptions(options);
if (string.IsNullOrEmpty(collectionSegmentName))
{
collectionSegmentName =
Expand Down Expand Up @@ -159,7 +159,7 @@ d.Type is SyntaxTypeReference syntaxTypeRef &&
public static IInterfaceFieldDescriptor UseOffsetPaging<TSchemaType>(
this IInterfaceFieldDescriptor descriptor,
string? collectionSegmentName = null,
PagingOptions options = default)
PagingOptions? options = null)
where TSchemaType : class, IOutputType =>
UseOffsetPaging(descriptor, typeof(TSchemaType), collectionSegmentName, options);

Expand All @@ -185,7 +185,7 @@ public static IInterfaceFieldDescriptor UseOffsetPaging(
this IInterfaceFieldDescriptor descriptor,
Type? itemType = null,
string? collectionSegmentName = null,
PagingOptions options = default)
PagingOptions? options = null)
{
if (descriptor is null)
{
Expand All @@ -198,7 +198,7 @@ public static IInterfaceFieldDescriptor UseOffsetPaging(
.Extend()
.OnBeforeCreate((c, d) =>
{
var pagingOptions = c.GetSettings(options);
var pagingOptions = c.GetPagingOptions(options);
if (string.IsNullOrEmpty(collectionSegmentName))
{
collectionSegmentName =
Expand Down Expand Up @@ -267,7 +267,7 @@ private static TypeReference CreateTypeRef(
MemberInfo? resolverMember,
string? collectionSegmentName,
TypeReference? itemsType,
PagingOptions options)
PagingOptions? options)
{
var typeInspector = context.TypeInspector;

Expand All @@ -285,7 +285,7 @@ private static TypeReference CreateTypeRef(
namedType);
}

options = context.GetSettings(options);
options = context.GetPagingOptions(options);

// last but not leas we create a type reference that can be put on the field definition
// to tell the type discovery that this field needs this result type.
Expand Down
7 changes: 7 additions & 0 deletions src/HotChocolate/Core/src/Types/Contracts/ISchemaBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using HotChocolate.Resolvers;
using HotChocolate.Types;
using HotChocolate.Types.Descriptors;
using HotChocolate.Types.Pagination;

#nullable enable

Expand All @@ -31,9 +32,15 @@ public interface ISchemaBuilder

ISchemaBuilder SetSchema(Action<ISchemaTypeDescriptor> configure);

[Obsolete("Use ModifyOptions instead.")]
ISchemaBuilder SetOptions(IReadOnlySchemaOptions options);

ISchemaBuilder ModifyOptions(Action<SchemaOptions> configure);

[Obsolete("Use ModifyPagingOptions instead.")]
ISchemaBuilder SetPagingOptions(PagingOptions options);

ISchemaBuilder ModifyPagingOptions(Action<PagingOptions> configure);

ISchemaBuilder Use(FieldMiddleware middleware);

Expand Down

This file was deleted.

3 changes: 3 additions & 0 deletions src/HotChocolate/Core/src/Types/SchemaBuilder.Setup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using HotChocolate.Types.Factories;
using HotChocolate.Types.Helpers;
using HotChocolate.Types.Interceptors;
using HotChocolate.Types.Pagination;
using HotChocolate.Types.Relay;
using HotChocolate.Utilities;
using HotChocolate.Utilities.Introspection;
Expand Down Expand Up @@ -104,6 +105,8 @@ public static DescriptorContext CreateContext(
lazySchema,
typeInterceptor);

context.ContextData[typeof(PagingOptions).FullName!] = builder._pagingOptions;

return context;
}

Expand Down
43 changes: 22 additions & 21 deletions src/HotChocolate/Core/src/Types/SchemaBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
using HotChocolate.Resolvers;
using HotChocolate.Types;
using HotChocolate.Types.Descriptors;
using HotChocolate.Types.Descriptors.Definitions;
using HotChocolate.Types.Factories;
using HotChocolate.Types.Interceptors;
using HotChocolate.Types.Introspection;
Expand Down Expand Up @@ -43,6 +42,7 @@ public partial class SchemaBuilder : ISchemaBuilder
];

private SchemaOptions _options = new();
private PagingOptions _pagingOptions = new();
private IsOfTypeFallback? _isOfType;
private IServiceProvider? _services;
private CreateRef? _schema;
Expand Down Expand Up @@ -105,6 +105,7 @@ public ISchemaBuilder SetSchema(Action<ISchemaTypeDescriptor> configure)
}

/// <inheritdoc />
[Obsolete("Use ModifyOptions instead.")]
public ISchemaBuilder SetOptions(IReadOnlySchemaOptions options)
{
if (options is null)
Expand All @@ -128,6 +129,26 @@ public ISchemaBuilder ModifyOptions(Action<SchemaOptions> configure)
return this;
}

/// <inheritdoc />
[Obsolete("Use ModifyPagingOptions instead.")]
public ISchemaBuilder SetPagingOptions(PagingOptions options)
{
_pagingOptions = options;
return this;
}

/// <inheritdoc />
public ISchemaBuilder ModifyPagingOptions(Action<PagingOptions> configure)
{
if (configure is null)
{
throw new ArgumentNullException(nameof(configure));
}

configure(_pagingOptions);
return this;
}

/// <inheritdoc />
public ISchemaBuilder Use(FieldMiddleware middleware)
{
Expand Down Expand Up @@ -455,24 +476,4 @@ public ISchemaBuilder TryAddTypeInterceptor(TypeInterceptor interceptor)
/// Returns a new instance of <see cref="SchemaBuilder"/>.
/// </returns>
public static SchemaBuilder New() => new();

private sealed class CopyOptions : TypeInterceptor
{
public override void OnBeforeCompleteType(ITypeCompletionContext completionContext, DefinitionBase definition)
{
if (definition is SchemaTypeDefinition schemaDef)
{
var key = typeof(PagingOptions).FullName!;

if (completionContext.DescriptorContext.ContextData.TryGetValue(key, out var value))
{
schemaDef.ContextData[key] = value;
}
else
{
schemaDef.ContextData[key] = new PagingOptions();
}
}
}
}
}
22 changes: 11 additions & 11 deletions src/HotChocolate/Core/src/Types/Types/Pagination/PagingHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@

namespace HotChocolate.Types.Pagination;

public static class PagingHelper
internal static class PagingHelper
{
public static IObjectFieldDescriptor UsePaging(
IObjectFieldDescriptor descriptor,
Type? entityType,
GetPagingProvider resolvePagingProvider,
PagingOptions options)
PagingOptions? options)
{
if (descriptor is null)
{
Expand All @@ -38,7 +38,7 @@ public static IObjectFieldDescriptor UsePaging(
c,
d,
entityType,
options.ProviderName,
options?.ProviderName,
resolvePagingProvider,
options,
placeholder),
Expand All @@ -54,10 +54,10 @@ private static void ApplyConfiguration(
Type? entityType,
string? name,
GetPagingProvider resolvePagingProvider,
PagingOptions options,
PagingOptions? options,
FieldMiddlewareDefinition placeholder)
{
options = context.GetSettings(options);
options = context.GetPagingOptions(options);
entityType ??= context.GetType<IOutputType>(definition.Type!).ToRuntimeType();

var source = GetSourceType(context.TypeInspector, definition, entityType);
Expand Down Expand Up @@ -194,16 +194,16 @@ public static bool TryGetNamedType(
return false;
}

public static PagingOptions GetSettings(
public static PagingOptions GetPagingOptions(
this ITypeCompletionContext context,
PagingOptions options) =>
context.DescriptorContext.GetSettings(options);
PagingOptions? options) =>
context.DescriptorContext.GetPagingOptions(options);

public static PagingOptions GetSettings(
public static PagingOptions GetPagingOptions(
this IDescriptorContext context,
PagingOptions options)
PagingOptions? options)
{
options = options.Copy();
options = options?.Copy() ?? new();

if (context.ContextData.TryGetValue(typeof(PagingOptions).FullName!, out var o) &&
o is PagingOptions global)
Expand Down
Loading

0 comments on commit 2a3e24f

Please sign in to comment.