Bacancy Technology
Bacancy Technology represents the connected world, offering innovative and customer-centric information technology experiences, enabling Enterprises, Associates and the Society to Rise™.
12+
Countries where we have happy customers
1050+
Agile enabled employees
06
World wide offices
12+
Years of Experience
05
Agile Coaches
14
Certified Scrum Masters
1000+
Clients projects
1458
Happy customers
Artificial Intelligence
Machine Learning
Salesforce
Microsoft
SAP
September 29, 2023
We can pass Cancellation token in GraphQL’s endpoint using IHttpContextAccessor.
In the below example we are injecting IHttpContextAccessor in GetAccountQuery class’s constructor, and then accessing cancellation taken as ‘accessor.HttpContext.RequestAborted’ and then passing it into GraphQL’s endpoint.
public class GetAccountQuery : ObjectGraphType { public GetAccountQuery(IResolver resolver, IHttpContextAccessor accessor) { FieldAsync<UserType>( "GetAccount", arguments: new QueryArguments( new QueryArgument<NonNullGraphType<IdGraphType>> { Name = "userId" }), resolve: async context => { var cancellationToken = accessor.HttpContext.RequestAborted; // Get cancellation token from the context return await resolver.ResolveAsync(context, cancellationToken); }); } }
Make sure you have registered ‘IHttpContextAccessor’ in the Program.cs file using below line of code:
builder.Services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();