-
-
Notifications
You must be signed in to change notification settings - Fork 744
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added field middleware that is bound to the query executer. (#482)
- Loading branch information
1 parent
27b4a17
commit e034f03
Showing
54 changed files
with
1,066 additions
and
746 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
106 changes: 106 additions & 0 deletions
106
src/Core/Core.Tests/Execution/ExecutionFieldMiddleware.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.