Skip to content

Commit

Permalink
Add Integration for Marten (#5301)
Browse files Browse the repository at this point in the history
  • Loading branch information
jabellard committed Nov 19, 2022
1 parent f3239e4 commit 59337ea
Show file tree
Hide file tree
Showing 265 changed files with 20,480 additions and 516 deletions.
1 change: 1 addition & 0 deletions .build/Helpers.cs
Expand Up @@ -23,6 +23,7 @@ static class Helpers
Path.Combine("HotChocolate", "Utilities"),
Path.Combine("HotChocolate", "Data"),
Path.Combine("HotChocolate", "Filters"),
Path.Combine("HotChocolate", "Marten"),
Path.Combine("HotChocolate", "MongoDb"),
Path.Combine("HotChocolate", "Neo4J"),
Path.Combine("HotChocolate", "Stitching"),
Expand Down
Expand Up @@ -9,6 +9,7 @@

#nullable enable

// ReSharper disable once CheckNamespace
namespace HotChocolate;

public static partial class SchemaBuilderExtensions
Expand Down Expand Up @@ -86,7 +87,7 @@ public static partial class SchemaBuilderExtensions
nameof(convention));
}

return builder.AddConvention(convention, (s) => concreteConvention, scope);
return builder.AddConvention(convention, _ => concreteConvention, scope);
}

public static ISchemaBuilder AddConvention(
Expand Down Expand Up @@ -147,12 +148,12 @@ public static partial class SchemaBuilderExtensions
where T : IConvention =>
builder.AddConvention(typeof(T), convention, scope);

public static ISchemaBuilder AddConvention<TConvetion, TConcreteConvention>(
public static ISchemaBuilder AddConvention<TConvention, TConcreteConvention>(
this ISchemaBuilder builder,
string? scope = null)
where TConvetion : IConvention
where TConvention : IConvention
where TConcreteConvention : IConvention =>
builder.AddConvention(typeof(TConvetion), typeof(TConcreteConvention), scope);
builder.AddConvention(typeof(TConvention), typeof(TConcreteConvention), scope);

public static ISchemaBuilder TryAddConvention(
this ISchemaBuilder builder,
Expand Down Expand Up @@ -291,7 +292,7 @@ public static partial class SchemaBuilderExtensions
directives = new();
builder.ContextData[SchemaDirectives] = directives;
}

directives.Add(directive);
return builder;
}
Expand Down
Expand Up @@ -6,8 +6,8 @@

namespace HotChocolate.Data.Filters.Expressions;

public class QueryableComparableNotLowerThanHandler
: QueryableComparableOperationHandler

public class QueryableComparableNotLowerThanHandler : QueryableComparableOperationHandler
{
public QueryableComparableNotLowerThanHandler(
ITypeConverter typeConverter,
Expand Down
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using HotChocolate.Configuration;
using HotChocolate.Language;
using HotChocolate.Types;
Expand All @@ -8,15 +9,14 @@
namespace HotChocolate.Data.Filters.Expressions;

/// <summary>
/// The base of a mongodb operation handler specific for
/// The base of a <see cref="IQueryable{T}"/> operation handler specific for
/// <see cref="IComparableOperationFilterInputType "/>
/// If the <see cref="FilterTypeInterceptor"/> encounters a operation field that implements
/// <see cref="IComparableOperationFilterInputType "/> and matches the operation identifier
/// defined in <see cref="QueryableComparableOperationHandler.Operation"/> the handler is bound
/// to the field
/// </summary>
public abstract class QueryableComparableOperationHandler
: QueryableOperationHandlerBase
public abstract class QueryableComparableOperationHandler : QueryableOperationHandlerBase
{
protected QueryableComparableOperationHandler(
ITypeConverter typeConverter,
Expand All @@ -31,6 +31,9 @@ public abstract class QueryableComparableOperationHandler
/// </summary>
protected abstract int Operation { get; }

/// <summary>
/// Provides access to the type converter,
/// </summary>
protected ITypeConverter TypeConverter { get; }

/// <summary>
Expand Down
@@ -1,8 +1,6 @@
using System;
using System.Diagnostics.CodeAnalysis;
using System.Linq.Expressions;
using HotChocolate.Data.Filters.Internal;
using HotChocolate.Internal;
using HotChocolate.Language;
using HotChocolate.Types;

Expand All @@ -16,6 +14,9 @@ protected QueryableOperationHandlerBase(InputParser inputParser)
InputParser = inputParser;
}

/// <summary>
/// Provides access to the input parser.
/// </summary>
protected InputParser InputParser { get; }

public override bool TryHandleOperation(
Expand Down
Expand Up @@ -58,7 +58,17 @@ async ValueTask ExecuteAsync(FieldDelegate next, IMiddlewareContext context)
}
}

private static ApplyFiltering CreateApplicator<TEntityType>(string argumentName)
protected virtual bool IsInMemoryQuery<TEntityType>(object? input)
{
if (input is QueryableExecutable<TEntityType> { InMemory: var inMemory })
{
return inMemory;
}

return input is not IQueryable || input is EnumerableQuery;
}

private ApplyFiltering CreateApplicator<TEntityType>(string argumentName)
{
return (context, input) =>
{
Expand All @@ -85,22 +95,17 @@ private static ApplyFiltering CreateApplicator<TEntityType>(string argumentName)
}
if (argument.Type is IFilterInputType filterInput &&
context.Selection.Field.ContextData.TryGetValue(
ContextVisitFilterArgumentKey,
out var executorObj) &&
context.Selection.Field.ContextData
.TryGetValue(ContextVisitFilterArgumentKey, out var executorObj) &&
executorObj is VisitFilterArgument executor)
{
var inMemory =
input is QueryableExecutable<TEntityType> { InMemory: true } ||
input is not IQueryable ||
input is EnumerableQuery;
var inMemory = IsInMemoryQuery<TEntityType>(input);
var visitorContext =
executor(filter, filterInput, inMemory);
var visitorContext = executor(filter, filterInput, inMemory);
// compile expression tree
if (visitorContext.TryCreateLambda(
out Expression<Func<TEntityType, bool>>? where))
out Expression<Func<TEntityType, bool>>? where))
{
input = input switch
{
Expand Down Expand Up @@ -137,8 +142,7 @@ private static ApplyFiltering CreateApplicator<TEntityType>(string argumentName)
IFilterInputType filterInput,
bool inMemory)
{
var visitorContext =
new QueryableFilterContext(filterInput, inMemory);
var visitorContext = new QueryableFilterContext(filterInput, inMemory);

// rewrite GraphQL input object into expression tree.
Visitor.Visit(valueNode, visitorContext);
Expand Down
Expand Up @@ -26,8 +26,7 @@ public QueryableSortProvider()
{
}

public QueryableSortProvider(
Action<ISortProviderDescriptor<QueryableSortContext>> configure)
public QueryableSortProvider(Action<ISortProviderDescriptor<QueryableSortContext>> configure)
: base(configure)
{
}
Expand Down Expand Up @@ -55,7 +54,17 @@ public override FieldMiddleware CreateExecutor<TEntityType>(string argumentName)
}
}

private static ApplySorting CreateApplicatorAsync<TEntityType>(string argumentName)
protected virtual bool IsInMemoryQuery<TEntityType>(object? input)
{
if (input is QueryableExecutable<TEntityType> { InMemory: var inMemory })
{
return inMemory;
}

return input is not IQueryable || input is EnumerableQuery;
}

private ApplySorting CreateApplicatorAsync<TEntityType>(string argumentName)
{
return (context, input) =>
{
Expand All @@ -65,12 +74,12 @@ private static ApplySorting CreateApplicatorAsync<TEntityType>(string argumentNa
// if no sort is defined we can stop here and yield back control.
var skipSorting =
context.LocalContextData.TryGetValue(SkipSortingKey, out var skip) &&
skip is true;
context.LocalContextData.TryGetValue(SkipSortingKey, out var skip) &&
skip is true;
// ensure sorting is only applied once
context.LocalContextData =
context.LocalContextData.SetItem(SkipSortingKey, true);
context.LocalContextData.SetItem(SkipSortingKey, true);
if (sort.IsNull() || skipSorting)
{
Expand All @@ -80,20 +89,13 @@ private static ApplySorting CreateApplicatorAsync<TEntityType>(string argumentNa
if (argument.Type is ListType lt &&
lt.ElementType is NonNullType nn &&
nn.NamedType() is ISortInputType sortInput &&
context.Selection.Field.ContextData.TryGetValue(
ContextVisitSortArgumentKey,
out var executorObj) &&
context.Selection.Field.ContextData
.TryGetValue(ContextVisitSortArgumentKey, out var executorObj) &&
executorObj is VisitSortArgument executor)
{
var inMemory =
input is QueryableExecutable<TEntityType> { InMemory: true } ||
input is not IQueryable ||
input is EnumerableQuery;
var inMemory = IsInMemoryQuery<TEntityType>(input);
var visitorContext = executor(
sort,
sortInput,
inMemory);
var visitorContext = executor(sort, sortInput, inMemory);
// compile expression tree
if (visitorContext.Errors.Count > 0)
Expand Down Expand Up @@ -130,9 +132,7 @@ private static ApplySorting CreateApplicatorAsync<TEntityType>(string argumentNa
ISortInputType filterInput,
bool inMemory)
{
var visitorContext = new QueryableSortContext(
filterInput,
inMemory);
var visitorContext = new QueryableSortContext(filterInput, inMemory);

// rewrite GraphQL input object into expression tree.
Visitor.Visit(valueNode, visitorContext);
Expand Down
Expand Up @@ -39,10 +39,10 @@ public async Task Create_BooleanEqual_Expression()
.Create());

// assert
await SnapshotExtensions.AddResult(
SnapshotExtensions.AddResult(
Snapshot
.Create(), res1, "true"), res2, "false")
await Snapshot
.Create()
.AddResult(res1, "true")
.AddResult(res2, "false")
.MatchAsync();
}

Expand All @@ -64,10 +64,10 @@ public async Task Create_BooleanNotEqual_Expression()
.Create());

// assert
await SnapshotExtensions.AddResult(
SnapshotExtensions.AddResult(
Snapshot
.Create(), res1, "true"), res2, "false")
await Snapshot
.Create()
.AddResult(res1, "true")
.AddResult(res2, "false")
.MatchAsync();
}

Expand All @@ -94,11 +94,11 @@ public async Task Create_NullableBooleanEqual_Expression()
.Create());

// assert
await SnapshotExtensions.AddResult(
SnapshotExtensions.AddResult(
SnapshotExtensions.AddResult(
Snapshot
.Create(), res1, "true"), res2, "false"), res3, "null")
await Snapshot
.Create()
.AddResult(res1, "true")
.AddResult(res2, "false")
.AddResult(res3, "null")
.MatchAsync();
}

Expand Down Expand Up @@ -126,11 +126,11 @@ public async Task Create_NullableBooleanNotEqual_Expression()
.Create());

// assert
await SnapshotExtensions.AddResult(
SnapshotExtensions.AddResult(
SnapshotExtensions.AddResult(
Snapshot
.Create(), res1, "true"), res2, "false"), res3, "null")
await Snapshot
.Create()
.AddResult(res1, "true")
.AddResult(res2, "false")
.AddResult(res3, "null")
.MatchAsync();
}

Expand Down

0 comments on commit 59337ea

Please sign in to comment.