Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
10 changes: 9 additions & 1 deletion samples/Samples.Server/GraphQLHttpMiddlewareWithLogs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using System;
using System.Threading;
using System.Threading.Tasks;

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

return base.RequestExecutedAsync(requestExecutionResult);
}

protected override CancellationToken GetCancellationToken(HttpContext context)
{
// custom CancellationToken example
var cts = CancellationTokenSource.CreateLinkedTokenSource(base.GetCancellationToken(context), new CancellationTokenSource(TimeSpan.FromSeconds(5)).Token);
return cts.Token;
}
}
}
8 changes: 6 additions & 2 deletions src/Transports.AspNetCore/GraphQLHttpMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using System.Collections.Generic;
using System.IO;
using System.Net.Http.Headers;
using System.Threading;
using System.Threading.Tasks;

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

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

// normal execution with single graphql request
if (gqlBatchRequest == null)
Expand All @@ -109,7 +111,7 @@ public async Task InvokeAsync(HttpContext context)
gqlRequest.Query,
gqlRequest.GetInputs(),
userContext,
context.RequestAborted).ConfigureAwait(false);
token).ConfigureAwait(false);

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

Expand All @@ -129,7 +131,7 @@ public async Task InvokeAsync(HttpContext context)
request.Query,
request.GetInputs(),
userContext,
context.RequestAborted).ConfigureAwait(false);
token).ConfigureAwait(false);

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

Expand All @@ -140,6 +142,8 @@ public async Task InvokeAsync(HttpContext context)
}
}

protected virtual CancellationToken GetCancellationToken(HttpContext context) => context.RequestAborted;

protected virtual Task RequestExecutedAsync(in GraphQLRequestExecutionResult requestExecutionResult)
{
// nothing to do in this middleware
Expand Down