Skip to content

Commit

Permalink
Integrated projections into new configuration API
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelstaib committed Oct 18, 2020
1 parent baef95e commit 014784c
Show file tree
Hide file tree
Showing 13 changed files with 126 additions and 102 deletions.
@@ -1,5 +1,7 @@
using System;
using HotChocolate;
using HotChocolate.Data.Filters;
using HotChocolate.Data.Projections;
using HotChocolate.Data.Sorting;
using HotChocolate.Execution.Configuration;

Expand Down Expand Up @@ -119,5 +121,66 @@ public static class HotChocolateDataRequestBuilderExtensions
string? name = null)
where TConvention : class, ISortConvention =>
builder.ConfigureSchema(s => s.AddSorting<TConvention>(name));

/// <summary>
/// Adds filtering support.
/// </summary>
/// <param name="builder">
/// The <see cref="IRequestExecutorBuilder"/>.
/// </param>
/// <returns>
/// Returns the <see cref="IRequestExecutorBuilder"/>.
/// </returns>
public static IRequestExecutorBuilder AddProjections(
this IRequestExecutorBuilder builder) =>
AddProjections(builder, x => x.AddDefaults());

/// <summary>
/// Adds filtering support.
/// </summary>
/// <param name="builder">
/// The <see cref="IRequestExecutorBuilder"/>.
/// </param>
/// <param name="configure">
/// Configures the convention.
/// </param>
/// <param name="name">
/// The filter convention name.
/// </param>
/// <returns>
/// Returns the <see cref="IRequestExecutorBuilder"/>.
/// </returns>
public static IRequestExecutorBuilder AddProjections(
this IRequestExecutorBuilder builder,
Action<IProjectionConventionDescriptor> configure,
string? name = null) =>
builder.ConfigureSchema(s => s
.TryAddTypeInterceptor<ProjectionTypeInterceptor>()
.TryAddConvention<IProjectionConvention>(
sp => new ProjectionConvention(configure),
name));

/// <summary>
/// Adds filtering support.
/// </summary>
/// <param name="builder">
/// The <see cref="IRequestExecutorBuilder"/>.
/// </param>
/// <param name="name">
/// The filter convention name.
/// </param>
/// <typeparam name="TConvention">
/// The concrete filter convention type.
/// </typeparam>
/// <returns>
/// Returns the <see cref="IRequestExecutorBuilder"/>.
/// </returns>
public static IRequestExecutorBuilder AddProjections<TConvention>(
this IRequestExecutorBuilder builder,
string? name = null)
where TConvention : class, IProjectionConvention =>
builder.ConfigureSchema(s => s
.TryAddTypeInterceptor<ProjectionTypeInterceptor>()
.TryAddConvention<IProjectionConvention, TConvention>(name));
}
}
@@ -1,9 +1,10 @@
using System;
using HotChocolate.Data.Filters;
using HotChocolate.Language;
using HotChocolate.Types;
using HotChocolate.Types.Descriptors;

namespace HotChocolate.Data.Filters
namespace HotChocolate.Types
{
public static class FilterFieldDescriptorExtensions
{
Expand Down

This file was deleted.

Expand Up @@ -11,7 +11,7 @@
using static HotChocolate.Data.DataResources;
using static HotChocolate.Data.ThrowHelper;

namespace HotChocolate.Data
namespace HotChocolate.Types
{
public static class FilterObjectFieldDescriptorExtensions
{
Expand Down
@@ -1,11 +1,12 @@
using System;
using HotChocolate.Data.Filters;

namespace HotChocolate.Data.Filters
namespace HotChocolate
{
/// <summary>
/// Provides filtering extensions for the <see cref="ISchemaBuilder"/>.
/// </summary>
public static class SchemaBuilderExtensions
public static class FilterSchemaBuilderExtensions
{
/// <summary>
/// Adds filtering support.
Expand Down
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using HotChocolate.Types;

namespace HotChocolate.Data.Filters
{
Expand Down
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using HotChocolate.Types;

namespace HotChocolate.Data.Filters
{
Expand Down
@@ -1,11 +1,12 @@
using System;
using HotChocolate.Data.Projections;

namespace HotChocolate.Data.Projections
namespace HotChocolate
{
/// <summary>
/// Provides filtering extensions for the <see cref="ISchemaBuilder"/>.
/// </summary>
public static class SchemaBuilderExtensions
public static class ProjectionsSchemaBuilderExtensions
{
/// <summary>
/// Adds filtering support.
Expand Down
@@ -1,5 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using HotChocolate.Configuration;
Expand All @@ -9,55 +8,11 @@
using HotChocolate.Types.Descriptors.Definitions;
using HotChocolate.Data.Projections;
using HotChocolate.Execution;
using HotChocolate.Execution.Processing;
using HotChocolate.Language;
using static HotChocolate.Data.Projections.ProjectionProvider;
using static HotChocolate.Execution.Processing.SelectionOptimizerHelper;

namespace HotChocolate.Types
{
public class ProjectionOptimizer : ISelectionOptimizer
{
private readonly IProjectionProvider _convention;

public ProjectionOptimizer(
IProjectionProvider convention)
{
_convention = convention;
}

public void OptimizeSelectionSet(SelectionOptimizerContext context)
{
var processedFields = new HashSet<string>();
while (!processedFields.SetEquals(context.Fields.Keys))
{
var fieldsToProcess = new HashSet<string>(context.Fields.Keys);
fieldsToProcess.ExceptWith(processedFields);
foreach (var field in fieldsToProcess)
{
context.Fields[field] =
_convention.RewriteSelection(context, context.Fields[field]);
processedFields.Add(field);
}
}
}

public bool AllowFragmentDeferral(
SelectionOptimizerContext context,
InlineFragmentNode fragment)
{
return false;
}

public bool AllowFragmentDeferral(
SelectionOptimizerContext context,
FragmentSpreadNode fragmentSpread,
FragmentDefinitionNode fragmentDefinition)
{
return false;
}
}

public static class ProjectionObjectFieldDescriptorExtensions
{
private static readonly MethodInfo _factoryTemplate =
Expand Down
48 changes: 48 additions & 0 deletions src/HotChocolate/Data/src/Data/Projections/ProjectionOptimizer.cs
@@ -0,0 +1,48 @@
using System.Collections.Generic;
using HotChocolate.Execution.Processing;
using HotChocolate.Language;

namespace HotChocolate.Data.Projections
{
public class ProjectionOptimizer : ISelectionOptimizer
{
private readonly IProjectionProvider _convention;

public ProjectionOptimizer(
IProjectionProvider convention)
{
_convention = convention;
}

public void OptimizeSelectionSet(SelectionOptimizerContext context)
{
var processedFields = new HashSet<string>();
while (!processedFields.SetEquals(context.Fields.Keys))
{
var fieldsToProcess = new HashSet<string>(context.Fields.Keys);
fieldsToProcess.ExceptWith(processedFields);
foreach (var field in fieldsToProcess)
{
context.Fields[field] =
_convention.RewriteSelection(context, context.Fields[field]);
processedFields.Add(field);
}
}
}

public bool AllowFragmentDeferral(
SelectionOptimizerContext context,
InlineFragmentNode fragment)
{
return false;
}

public bool AllowFragmentDeferral(
SelectionOptimizerContext context,
FragmentSpreadNode fragmentSpread,
FragmentDefinitionNode fragmentDefinition)
{
return false;
}
}
}

This file was deleted.

Expand Up @@ -5,13 +5,12 @@
using HotChocolate.Data.Sorting;
using HotChocolate.Internal;
using HotChocolate.Resolvers;
using HotChocolate.Types;
using HotChocolate.Types.Descriptors;
using HotChocolate.Types.Descriptors.Definitions;
using static HotChocolate.Data.DataResources;
using static HotChocolate.Data.ThrowHelper;

namespace HotChocolate.Data
namespace HotChocolate.Types
{
public static class SortObjectFieldDescriptorExtensions
{
Expand Down
@@ -1,11 +1,12 @@
using System;
using HotChocolate.Data.Sorting;

namespace HotChocolate.Data.Sorting
namespace HotChocolate
{
/// <summary>
/// Provides Sorting extensions for the <see cref="ISchemaBuilder"/>.
/// </summary>
public static class SchemaBuilderExtensions
public static class SortingSchemaBuilderExtensions
{
/// <summary>
/// Adds Sorting support.
Expand Down

0 comments on commit 014784c

Please sign in to comment.