Skip to content

Commit e3531b7

Browse files
committed
Merge branch 'v8'
2 parents 3013326 + fab4691 commit e3531b7

File tree

5 files changed

+34
-3
lines changed

5 files changed

+34
-3
lines changed

src/GraphQLParser.ApiTests/GraphQLParser.approved.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff 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)

src/GraphQLParser.Tests/Visitors/ASTVisitorTests.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff 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
}

src/GraphQLParser/Visitors/ASTVisitor.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff 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
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
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+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
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+
}

0 commit comments

Comments
 (0)