Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
69290dd
Started rewriting the middleware pipeline factory
michaelstaib Jan 14, 2019
1c7108e
Merge branch 'master' into micha/UseField
michaelstaib Jan 16, 2019
390729f
Merge branch 'master' into micha/UseField
michaelstaib Jan 16, 2019
a50b939
Merge branch 'master' into micha/UseField
michaelstaib Jan 18, 2019
7155c50
Refined middleware
michaelstaib Jan 18, 2019
6f801de
Work in progress
michaelstaib Jan 18, 2019
a9d4d20
work in progress
michaelstaib Jan 18, 2019
960b4bc
moved pr template
michaelstaib Jan 18, 2019
bc6bee0
moved pr template
michaelstaib Jan 18, 2019
494f5c2
fixed compilation errors in the execute middleware
michaelstaib Jan 18, 2019
ebf17ca
Finished core refactoring
michaelstaib Jan 18, 2019
69095f6
Fixed compile issues
michaelstaib Jan 18, 2019
d99b4bc
Fixed test issues
michaelstaib Jan 18, 2019
e6c73a0
fixed test
michaelstaib Jan 18, 2019
24a53ed
Fixed test
michaelstaib Jan 18, 2019
d172d7f
fixed test
michaelstaib Jan 18, 2019
68d103c
fixed test
michaelstaib Jan 18, 2019
57a9ef8
Skipped stitching tests
michaelstaib Jan 18, 2019
8c34365
sonar fix
michaelstaib Jan 19, 2019
d679cce
Fixed sonar issues
michaelstaib Jan 20, 2019
96a5d1d
Merged master
michaelstaib Jan 20, 2019
400cdfa
Updated Changelog
michaelstaib Jan 20, 2019
b7d33f5
Fixed sonar issues
michaelstaib Jan 20, 2019
bc63d20
Added tests
michaelstaib Jan 20, 2019
f02e1fa
added tests that show the execution bound field middleware works
michaelstaib Jan 20, 2019
83c982f
added tests that show the execution bound field middleware works
michaelstaib Jan 20, 2019
88eb3cf
Removed example code from better code hub analysis
michaelstaib Jan 20, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .bettercodehub.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
component_depth: 2
languages:
- csharp
exclude:
- /examples/.*
- /benchmarks/.*
- /src/Templates/.*
- .*\.js
File renamed without changes.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Query complexity validation rules [#80](https://github.com/ChilliCream/hotchocolate/issues/80)
- Added support for relay global object identification specification [specification](http://facebook.github.io/relay/graphql/objectidentification.htm).
- Added Source Code Link for NuGet support.
- Added support for a executer scoped field middleware [#482](https://github.com/ChilliCream/hotchocolate/issues/482).

### Changed

Expand Down
8 changes: 2 additions & 6 deletions src/Core/Core.Tests/Execution/ExecutionContextTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@ public void ContextDataArePassedOn()

var variables = new Mock<IVariableCollection>();

var directives = new DirectiveLookup(new Dictionary<ObjectType, IDictionary<FieldNode, IReadOnlyCollection<IDirective>>>());

var contextData = new Dictionary<string, object>
{
{ "abc", "123" }
Expand All @@ -45,7 +43,7 @@ public void ContextDataArePassedOn()
// act
var executionContext = new ExecutionContext(
schema, serviceScope, operation.Object,
variables.Object, directives, contextData,
variables.Object, fs => null, contextData,
CancellationToken.None);

// assert
Expand Down Expand Up @@ -77,8 +75,6 @@ public void CloneExecutionContext()

var variables = new Mock<IVariableCollection>();

var directives = new DirectiveLookup(new Dictionary<ObjectType, IDictionary<FieldNode, IReadOnlyCollection<IDirective>>>());

var contextData = new Dictionary<string, object>
{
{ "abc", "123" }
Expand All @@ -87,7 +83,7 @@ public void CloneExecutionContext()
// act
var executionContext = new ExecutionContext(
schema, serviceScope, operation.Object,
variables.Object, directives, contextData,
variables.Object, fs => null, contextData,
CancellationToken.None);
IExecutionContext cloned = executionContext.Clone();

Expand Down
106 changes: 106 additions & 0 deletions src/Core/Core.Tests/Execution/ExecutionFieldMiddleware.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
using System.Threading.Tasks;
using ChilliCream.Testing;
using HotChocolate.Resolvers;
using Xunit;

namespace HotChocolate.Execution
{
public class ExecutionFieldMiddleware
{
[Fact]
public async Task ExecuteFieldWithExecutionMiddleware()
{
// arrange
var schema = Schema.Create(
"type Query { a: String }",
c => c.Use(next => context =>
{
context.Result = "a";
return next.Invoke(context);
}));

IQueryExecutor executor = schema.MakeExecutable(b =>
b.UseDefaultPipeline()
.UseField(next => async context =>
{
await next.Invoke(context);

if (context.Result is string s)
{
context.Result = s.ToUpperInvariant();
}
}));

// act
IExecutionResult result = await executor.ExecuteAsync("{ a }");

// assert
result.Snapshot();
}

[Fact]
public async Task ExecuteFieldWithExecutionClassMiddleware()
{
// arrange
var schema = Schema.Create(
"type Query { a: String }",
c => c.Use(next => context =>
{
context.Result = "a";
return next.Invoke(context);
}));

IQueryExecutor executor = schema.MakeExecutable(b =>
b.UseDefaultPipeline().UseField<ToUpperMiddleware>());

// act
IExecutionResult result = await executor.ExecuteAsync("{ a }");

// assert
result.Snapshot();
}

[Fact]
public async Task ExecuteFieldWithExecutionClassMiddlewareWithFactory()
{
// arrange
var schema = Schema.Create(
"type Query { a: String }",
c => c.Use(next => context =>
{
context.Result = "a";
return next.Invoke(context);
}));

IQueryExecutor executor = schema.MakeExecutable(b =>
b.UseDefaultPipeline()
.UseField((sp, next) => new ToUpperMiddleware(next)));

// act
IExecutionResult result = await executor.ExecuteAsync("{ a }");

// assert
result.Snapshot();
}

public class ToUpperMiddleware
{
private readonly FieldDelegate _next;

public ToUpperMiddleware(FieldDelegate next)
{
_next = next;
}

public async Task InvokeAsync(IMiddlewareContext context)
{
await _next.Invoke(context);

if (context.Result is string s)
{
context.Result = s.ToUpperInvariant();
}
}
}
}
}
74 changes: 74 additions & 0 deletions src/Core/Core.Tests/Execution/IntrospectionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,60 @@ public async Task ExecuteGraphiQLIntrospectionQuery()
result.Snapshot();
}

[Fact]
public async Task FieldMiddlewareDoesNotHaveAnEffectOnIntrospection()
{
// arrange
string query = "{ __typename a }";

var schema = Schema.Create(c =>
{
c.RegisterExtendedScalarTypes();
c.RegisterType<Query>();
c.Use(next => async context =>
{
await next.Invoke(context);

if (context.Result is string s)
{
context.Result = s.ToUpperInvariant();
}
});
});

IQueryExecutor executor = schema.MakeExecutable();

// act
IExecutionResult result = await executor.ExecuteAsync(query);

// assert
Assert.Empty(result.Errors);
result.Snapshot();
}

[Fact]
public async Task DirectiveMiddlewareDoesWorkOnIntrospection()
{
// arrange
string query = "{ __typename @upper a }";

var schema = Schema.Create(c =>
{
c.RegisterExtendedScalarTypes();
c.RegisterType<Query>();
c.RegisterDirective<UpperDirectiveType>();
});

IQueryExecutor executor = schema.MakeExecutable();

// act
IExecutionResult result = await executor.ExecuteAsync(query);

// assert
Assert.Empty(result.Errors);
result.Snapshot();
}

private static Schema CreateSchema()
{
return Schema.Create(c =>
Expand Down Expand Up @@ -119,5 +173,25 @@ protected override void Configure(IObjectTypeDescriptor descriptor)
.Resolver(() => "foo.a");
}
}

private sealed class UpperDirectiveType
: DirectiveType
{
protected override void Configure(
IDirectiveTypeDescriptor descriptor)
{
descriptor.Name("upper");
descriptor.Location(DirectiveLocation.Field);
descriptor.Middleware(next => async context =>
{
await next.Invoke(context);

if (context.Result is string s)
{
context.Result = s.ToUpperInvariant();
}
});
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,14 @@ public async Task ParseQueryMiddleware_ValidQuery_DocumentIsSet()
}
}.ToReadOnly();

var context = new QueryContext(
schema, MiddlewareTools.CreateEmptyRequestServiceScope(), request);
var context = new QueryContext
(
schema,
MiddlewareTools.CreateEmptyRequestServiceScope(),
request,
fs => fs.Field.Middleware
);

context.Document = Parser.Default.Parse(request.Query);
context.Operation = new Operation(
context.Document,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,13 @@ public async Task ExecuteOperationMiddleware_Mutation_ExecutedSerially()
typeof(IErrorHandler),
ErrorHandler.Default));

var context = new QueryContext(
schema, services.CreateRequestServiceScope(), request)
var context = new QueryContext
(
schema,
services.CreateRequestServiceScope(),
request,
fs => fs.Field.Middleware
)
{
Document = query,
Operation = operation,
Expand All @@ -67,7 +72,7 @@ public async Task ExecuteOperationMiddleware_Mutation_ExecutedSerially()
var middleware = new ExecuteOperationMiddleware(
c => Task.CompletedTask,
strategyResolver,
new Cache<DirectiveLookup>(10));
new Cache<DirectiveMiddlewareCompiler>(10));

// act
await middleware.InvokeAsync(context);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,13 @@ type Query {
typeof(IErrorHandler),
ErrorHandler.Default));

var context = new QueryContext(
schema, services.CreateRequestServiceScope(), request)
var context = new QueryContext
(
schema,
services.CreateRequestServiceScope(),
request,
fs => fs.Field.Middleware
)
{
Document = query,
Operation = operation,
Expand Down Expand Up @@ -131,8 +136,13 @@ type Query {
typeof(IErrorHandler),
ErrorHandler.Default));

var context = new QueryContext(
schema, services.CreateRequestServiceScope(), request)
var context = new QueryContext
(
schema,
services.CreateRequestServiceScope(),
request,
fs => fs.Field.Middleware
)
{
Document = query,
Operation = operation,
Expand Down Expand Up @@ -212,8 +222,13 @@ input FooInput {
typeof(IErrorHandler),
ErrorHandler.Default));

var context = new QueryContext(
schema, services.CreateRequestServiceScope(), request)
var context = new QueryContext
(
schema,
services.CreateRequestServiceScope(),
request,
fs => fs.Field.Middleware
)
{
Document = query,
Operation = operation,
Expand Down Expand Up @@ -290,17 +305,22 @@ input FooInput {
typeof(IErrorHandler),
ErrorHandler.Default));

var context = new QueryContext(
schema, services.CreateRequestServiceScope(), request)
var context = new QueryContext
(
schema,
services.CreateRequestServiceScope(),
request,
fs => fs.Field.Middleware
)
{
Document = query,
Operation = operation,
Variables = new VariableValueBuilder(
schema, operation.Definition)
.CreateValues(new Dictionary<string, object>
{
schema, operation.Definition)
.CreateValues(new Dictionary<string, object>
{
{ "i", count }
})
})
};

var middleware = new MaxComplexityMiddleware(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,13 @@ public async Task ParseQueryMiddleware_ValidQuery_DocumentIsSet()

var request = new QueryRequest("{ a }").ToReadOnly();

var context = new QueryContext(
var context = new QueryContext
(
schema,
MiddlewareTools.CreateEmptyRequestServiceScope(),
request);
request,
fs => fs.Field.Middleware
);

var middleware = new ParseQueryMiddleware(
c => Task.CompletedTask,
Expand All @@ -43,10 +46,13 @@ public Task ParseQueryMiddleware_InvalidQuery_DocumentNull()

var request = new QueryRequest("{").ToReadOnly();

var context = new QueryContext(
var context = new QueryContext
(
schema,
MiddlewareTools.CreateEmptyRequestServiceScope(),
request);
request,
fs => fs.Field.Middleware
);

var middleware = new ParseQueryMiddleware(
c => Task.CompletedTask,
Expand Down
Loading