Skip to content

Commit

Permalink
Updated Templates (ChilliCream#2335)
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelstaib authored Sep 17, 2020
1 parent 838ce49 commit f84ac7a
Show file tree
Hide file tree
Showing 36 changed files with 274 additions and 218 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,22 @@ public static IServiceCollection AddGraphQL(
schema)
.Services;

[Obsolete(
"Use the new configuration API -> " +
"services.AddGraphQLServer().AddQueryType<Query>()...")]
public static IServiceCollection AddGraphQL(
this IServiceCollection services,
Func<IServiceProvider, ISchema> schemaFactory,
int maxAllowedRequestSize = 20 * 1000 * 1000) =>
RequestExecutorBuilderLegacyHelper.SetSchema(
services
.AddGraphQLServerCore(maxAllowedRequestSize)
.AddGraphQL()
.AddHttpRequestInterceptor()
.AddSubscriptionServices(),
schemaFactory)
.Services;

[Obsolete(
"Use the new configuration API -> " +
"services.AddGraphQLServer().AddQueryType<Query>()...")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,32 @@ public async Task AddGraphQL_With_UseGraphQL()
result.MatchSnapshot();
}

[Fact]
public async Task AddGraphQL_With_UseGraphQL_With_Factory()
{
// arrange
TestServer server = ServerFactory.Create(
services => services
.AddGraphQL(sp =>
SchemaBuilder.New()
.AddServices(sp)
.AddQueryType(d => d
.Name("Query")
.Field("hello")
.Resolve("world"))
.Create()),
app => app
.UseWebSockets()
.UseGraphQL());

// act
ClientQueryResult result = await server.PostAsync(
new ClientQueryRequest { Query = "{ __typename }" });

// assert
result.MatchSnapshot();
}

[Fact]
public async Task AddGraphQL_With_UseGraphQL_With_SchemaBuilder()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,5 +75,38 @@ public static IRequestExecutorBuilder SetSchema(

return builder.Configure(options => options.Schema = schema);
}

/// <summary>
/// Sets the schema builder that shall be used to configure the request executor.
/// </summary>
/// <param name="builder">
/// The <see cref="IRequestExecutorBuilder"/>.
/// </param>
/// <param name="schemaFactory">
/// The factory to create the schema.
/// </param>
/// <returns>
/// An <see cref="IRequestExecutorBuilder"/> that can be used to configure a schema
/// and its execution.
/// </returns>
[Obsolete(
"This helper only exists to allow legacy schema handling. " +
"Consider moving to the new configuration API.")]
public static IRequestExecutorBuilder SetSchema(
IRequestExecutorBuilder builder,
Func<IServiceProvider, ISchema> schemaFactory)
{
if (builder is null)
{
throw new ArgumentNullException(nameof(builder));
}

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

return builder.Configure((s, o) => o.Schema = schemaFactory(s));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@ namespace Microsoft.Extensions.DependencyInjection
{
public static partial class SchemaRequestExecutorBuilderExtensions
{
public static IRequestExecutorBuilder SetPagingSettings(
public static IRequestExecutorBuilder SetPagingOptions(
this IRequestExecutorBuilder builder,
PagingSettings settings)
PagingOptions options)
{
if (builder is null)
{
throw new ArgumentNullException(nameof(builder));
}

return builder.ConfigureSchema(s => s.SetPagingSettings(settings));
return builder.ConfigureSchema(s => s.SetPagingOptions(options));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ namespace HotChocolate.Types.Pagination
{
public abstract class CursorPagingHandler : IPagingHandler
{
protected CursorPagingHandler(PagingSettings settings)
protected CursorPagingHandler(PagingOptions options)
{
DefaultPageSize = settings.DefaultPageSize ?? PagingDefaults.DefaultPageSize;
MaxPageSize = settings.MaxPageSize ?? PagingDefaults.MaxPageSize;
DefaultPageSize = options.DefaultPageSize ?? PagingDefaults.DefaultPageSize;
MaxPageSize = options.MaxPageSize ?? PagingDefaults.MaxPageSize;

if (MaxPageSize < DefaultPageSize)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ public abstract class CursorPagingProvider : IPagingProvider

IPagingHandler IPagingProvider.CreateHandler(
IExtendedType source,
PagingSettings settings) =>
CreateHandler(source, settings);
PagingOptions options) =>
CreateHandler(source, options);

protected abstract CursorPagingHandler CreateHandler(
IExtendedType source,
PagingSettings settings);
PagingOptions options);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,24 @@ public static class PagingObjectFieldDescriptorExtensions
public static IObjectFieldDescriptor UsePaging<TSchemaType, TEntity>(
this IObjectFieldDescriptor descriptor,
GetCursorPagingProvider? resolvePagingProvider = null,
PagingSettings settings = default)
PagingOptions options = default)
where TSchemaType : class, IOutputType =>
UsePaging<TSchemaType>(descriptor, typeof(TEntity), resolvePagingProvider, settings);
UsePaging<TSchemaType>(descriptor, typeof(TEntity), resolvePagingProvider, options);

public static IObjectFieldDescriptor UsePaging<TSchemaType>(
this IObjectFieldDescriptor descriptor,
Type? entityType = null,
GetCursorPagingProvider? resolvePagingProvider = null,
PagingSettings settings = default)
PagingOptions options = default)
where TSchemaType : class, IOutputType =>
UsePaging(descriptor, typeof(TSchemaType), entityType, resolvePagingProvider, settings);
UsePaging(descriptor, typeof(TSchemaType), entityType, resolvePagingProvider, options);

public static IObjectFieldDescriptor UsePaging(
this IObjectFieldDescriptor descriptor,
Type? type = null,
Type? entityType = null,
GetCursorPagingProvider? resolvePagingProvider = null,
PagingSettings settings = default)
PagingOptions options = default)
{
if (descriptor is null)
{
Expand All @@ -48,27 +48,27 @@ public static IObjectFieldDescriptor UsePaging(
type,
entityType,
(services, source) => resolvePagingProvider(services, source),
settings);
options);

descriptor
.Extend()
.OnBeforeCreate(
(c, d) => d.Type = CreateConnectionTypeRef(
c, d.ResolverMember ?? d.Member, type, settings));
c, d.ResolverMember ?? d.Member, type, options));

return descriptor;
}

public static IInterfaceFieldDescriptor UsePaging<TSchemaType>(
this IInterfaceFieldDescriptor descriptor,
PagingSettings settings = default)
PagingOptions options = default)
where TSchemaType : class, IOutputType =>
UsePaging(descriptor, typeof(TSchemaType), settings);
UsePaging(descriptor, typeof(TSchemaType), options);

public static IInterfaceFieldDescriptor UsePaging(
this IInterfaceFieldDescriptor descriptor,
Type? type = null,
PagingSettings settings = default)
PagingOptions options = default)
{
if (descriptor is null)
{
Expand All @@ -79,7 +79,7 @@ public static IInterfaceFieldDescriptor UsePaging(
.AddPagingArguments()
.Extend()
.OnBeforeCreate(
(c, d) => d.Type = CreateConnectionTypeRef(c, d.Member, type, settings));
(c, d) => d.Type = CreateConnectionTypeRef(c, d.Member, type, options));

return descriptor;
}
Expand Down Expand Up @@ -118,7 +118,7 @@ private static ITypeReference CreateConnectionTypeRef(
IDescriptorContext context,
MemberInfo? resolverMember,
Type? type,
PagingSettings settings)
PagingOptions options)
{
// first we will try and infer the schema type of the collection.
IExtendedType schemaType = PagingHelper.GetSchemaType(
Expand All @@ -135,12 +135,12 @@ private static ITypeReference CreateConnectionTypeRef(
throw PagingObjectFieldDescriptorExtensions_InvalidType();
}

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

// once we have identified the correct type we will create the
// paging result type from it.
IExtendedType connectionType = context.TypeInspector.GetType(
settings.IncludeTotalCount ?? false
options.IncludeTotalCount ?? false
? typeof(ConnectionCountType<>).MakeGenericType(schemaType.Source)
: typeof(ConnectionType<>).MakeGenericType(schemaType.Source));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ protected override void TryConfigure(
{
ofd.UsePaging(
Type,
settings: new PagingSettings
options: new PagingOptions
{
DefaultPageSize = _defaultPageSize,
MaxPageSize = _maxPageSize,
Expand All @@ -85,7 +85,7 @@ protected override void TryConfigure(
{
ifd.UsePaging(
Type,
new PagingSettings
new PagingOptions
{
DefaultPageSize = _defaultPageSize,
MaxPageSize = _maxPageSize,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ namespace HotChocolate.Types.Pagination
{
public class QueryableCursorPagingHandler<TEntity> : CursorPagingHandler
{
public QueryableCursorPagingHandler(PagingSettings settings)
: base(settings)
public QueryableCursorPagingHandler(PagingOptions options)
: base(options)
{
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public override bool CanHandle(IExtendedType source)

protected override CursorPagingHandler CreateHandler(
IExtendedType source,
PagingSettings settings)
PagingOptions options)
{
if (source is null)
{
Expand All @@ -33,11 +33,11 @@ protected override CursorPagingHandler CreateHandler(

return (CursorPagingHandler)_createHandler
.MakeGenericMethod(source.ElementType?.Source ?? source.Source)
.Invoke(null, new object[] { settings })!;
.Invoke(null, new object[] { options })!;
}

private static QueryableCursorPagingHandler<TEntity> CreateHandlerInternal<TEntity>(
PagingSettings settings) =>
new QueryableCursorPagingHandler<TEntity>(settings);
PagingOptions options) =>
new QueryableCursorPagingHandler<TEntity>(options);
}
}
Loading

0 comments on commit f84ac7a

Please sign in to comment.