File tree Expand file tree Collapse file tree 5 files changed +34
-3
lines changed
GraphQLParser.Tests/Visitors Expand file tree Collapse file tree 5 files changed +34
-3
lines changed Original file line number Diff line number Diff line change @@ -840,6 +840,10 @@ namespace GraphQLParser.Visitors
840840 public int MaxDepth { get; set; }
841841 public System.Collections.Generic.Stack<GraphQLParser.AST.ASTNode> Parents { get; set; }
842842 }
843+ public struct DefaultVisitorContext : GraphQLParser.Visitors.IASTVisitorContext
844+ {
845+ public System.Threading.CancellationToken CancellationToken { get; set; }
846+ }
843847 public interface IASTVisitorContext
844848 {
845849 System.Threading.CancellationToken CancellationToken { get; }
@@ -871,6 +875,10 @@ namespace GraphQLParser.Visitors
871875 public MaxDepthVisitor() { }
872876 public override System.Threading.Tasks.ValueTask VisitAsync(GraphQLParser.AST.ASTNode? node, TContext context) { }
873877 }
878+ public struct NullVisitorContext : GraphQLParser.Visitors.IASTVisitorContext
879+ {
880+ public System.Threading.CancellationToken CancellationToken { get; }
881+ }
874882 public static class PrintContextExtensions
875883 {
876884 public static System.Threading.Tasks.ValueTask WriteAsync<TContext>(this TContext context, GraphQLParser.ROM value)
Original file line number Diff line number Diff line change @@ -33,12 +33,13 @@ public void ASTVisitor_Should_Throw_On_Unknown_Node()
3333 }
3434
3535 [ Fact ]
36- public void ASTVisitor_Should_Respect_CancellationToken ( )
36+ public void ASTVisitor_Should_Pass_CancellationToken ( )
3737 {
3838 var document = "scalar JSON" . Parse ( ) ;
3939 var visitor = new MyVisitor ( ) ;
4040 using var cts = new CancellationTokenSource ( 500 ) ;
4141 var context = new Context { CancellationToken = cts . Token } ;
42+ context . CancellationToken . ThrowIfCancellationRequested ( ) ;
4243
4344 Should . Throw < OperationCanceledException > ( ( ) => visitor . VisitAsync ( document , context ) . GetAwaiter ( ) . GetResult ( ) ) ;
4445 }
@@ -48,6 +49,7 @@ private sealed class MyVisitor : ASTVisitor<Context>
4849 protected override async ValueTask VisitScalarTypeDefinitionAsync ( GraphQLScalarTypeDefinition scalarTypeDefinition , Context context )
4950 {
5051 await Task . Delay ( 700 ) ;
52+ context . CancellationToken . ThrowIfCancellationRequested ( ) ;
5153 await base . VisitScalarTypeDefinitionAsync ( scalarTypeDefinition , context ) ;
5254 }
5355 }
Original file line number Diff line number Diff line change @@ -597,8 +597,6 @@ protected virtual async ValueTask VisitInputObjectTypeExtensionAsync(GraphQLInpu
597597 /// <param name="context">Context passed into all INodeVisitor.VisitXXX methods.</param>
598598 public virtual ValueTask VisitAsync ( ASTNode ? node , TContext context )
599599 {
600- context . CancellationToken . ThrowIfCancellationRequested ( ) ;
601-
602600 return node == null
603601 ? default
604602 : node switch
Original file line number Diff line number Diff line change 1+ namespace GraphQLParser . Visitors ;
2+
3+ /// <summary>
4+ /// An implementation of <see cref="IASTVisitorContext"/> that only contains a <see cref="CancellationToken"/>.
5+ /// Ideal for use in cases where there is no context variables.
6+ /// </summary>
7+ public struct DefaultVisitorContext : IASTVisitorContext
8+ {
9+ /// <inheritdoc/>
10+ public CancellationToken CancellationToken { get ; set ; }
11+ }
Original file line number Diff line number Diff line change 1+ namespace GraphQLParser . Visitors ;
2+
3+ /// <summary>
4+ /// An implementation of <see cref="IASTVisitorContext"/> that does nothing.
5+ /// Ideal for use in cases where the visitor runs synchronously, there is no context
6+ /// variables, and cancellation is not required.
7+ /// </summary>
8+ public struct NullVisitorContext : IASTVisitorContext
9+ {
10+ /// <inheritdoc/>
11+ public readonly CancellationToken CancellationToken => default ;
12+ }
You can’t perform that action at this time.
0 commit comments