Skip to content

Commit 9088c88

Browse files
authored
Cancellationtoken tuning (#285)
* add virtual method for setting cancellationToken * add example
1 parent 7e31bdc commit 9088c88

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

samples/Samples.Server/GraphQLHttpMiddlewareWithLogs.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@
55
using Microsoft.Extensions.Logging;
66
using Newtonsoft.Json;
77
using System;
8+
using System.Threading;
89
using System.Threading.Tasks;
910

1011
namespace GraphQL.Samples.Server
1112
{
12-
// Example of a custom GraphQL Middleware which logs errors to Microsoft.Extensions.Logging API
13+
// Example of a custom GraphQL Middleware that sends execution result to Microsoft.Extensions.Logging API
1314
public class GraphQLHttpMiddlewareWithLogs<TSchema> : GraphQLHttpMiddleware<TSchema>
1415
where TSchema : ISchema
1516
{
@@ -35,5 +36,12 @@ protected override Task RequestExecutedAsync(in GraphQLRequestExecutionResult re
3536

3637
return base.RequestExecutedAsync(requestExecutionResult);
3738
}
39+
40+
protected override CancellationToken GetCancellationToken(HttpContext context)
41+
{
42+
// custom CancellationToken example
43+
var cts = CancellationTokenSource.CreateLinkedTokenSource(base.GetCancellationToken(context), new CancellationTokenSource(TimeSpan.FromSeconds(5)).Token);
44+
return cts.Token;
45+
}
3846
}
3947
}

src/Transports.AspNetCore/GraphQLHttpMiddleware.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
using System.Collections.Generic;
1212
using System.IO;
1313
using System.Net.Http.Headers;
14+
using System.Threading;
1415
using System.Threading.Tasks;
1516

1617
namespace GraphQL.Server.Transports.AspNetCore
@@ -99,6 +100,7 @@ public async Task InvokeAsync(HttpContext context)
99100
userContext = new Dictionary<string, object>(); // in order to allow resolvers to exchange their state through this object
100101

101102
var executer = context.RequestServices.GetRequiredService<IGraphQLExecuter<TSchema>>();
103+
var token = GetCancellationToken(context);
102104

103105
// normal execution with single graphql request
104106
if (gqlBatchRequest == null)
@@ -109,7 +111,7 @@ public async Task InvokeAsync(HttpContext context)
109111
gqlRequest.Query,
110112
gqlRequest.GetInputs(),
111113
userContext,
112-
context.RequestAborted).ConfigureAwait(false);
114+
token).ConfigureAwait(false);
113115

114116
await RequestExecutedAsync(new GraphQLRequestExecutionResult(gqlRequest, result, stopwatch.Elapsed));
115117

@@ -129,7 +131,7 @@ public async Task InvokeAsync(HttpContext context)
129131
request.Query,
130132
request.GetInputs(),
131133
userContext,
132-
context.RequestAborted).ConfigureAwait(false);
134+
token).ConfigureAwait(false);
133135

134136
await RequestExecutedAsync(new GraphQLRequestExecutionResult(gqlRequest, result, stopwatch.Elapsed, i));
135137

@@ -140,6 +142,8 @@ public async Task InvokeAsync(HttpContext context)
140142
}
141143
}
142144

145+
protected virtual CancellationToken GetCancellationToken(HttpContext context) => context.RequestAborted;
146+
143147
protected virtual Task RequestExecutedAsync(in GraphQLRequestExecutionResult requestExecutionResult)
144148
{
145149
// nothing to do in this middleware

0 commit comments

Comments
 (0)