{"id":8726,"date":"2023-09-29T04:57:30","date_gmt":"2023-09-29T04:57:30","guid":{"rendered":"https:\/\/www.bacancytechnology.com\/qanda\/?p=8726"},"modified":"2023-10-13T06:32:51","modified_gmt":"2023-10-13T06:32:51","slug":"cancellation-token-in-graphql-for-net","status":"publish","type":"post","link":"https:\/\/www.bacancytechnology.com\/qanda\/dot-net\/cancellation-token-in-graphql-for-net","title":{"rendered":"GraphQL for .NET &#8211; Cancellation Token"},"content":{"rendered":"<p>We can pass Cancellation token in GraphQL\u2019s endpoint using IHttpContextAccessor.<\/p>\n<p>In the below example we are injecting <strong>IHttpContextAccessor<\/strong> in GetAccountQuery class\u2019s constructor, and then accessing cancellation taken as <strong>\u2018accessor.HttpContext.RequestAborted\u2019<\/strong> and then passing it into GraphQL\u2019s endpoint.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">public class GetAccountQuery : ObjectGraphType\r\n{\r\n    public GetAccountQuery(IResolver resolver, IHttpContextAccessor accessor)\r\n    {\r\n        FieldAsync&lt;UserType&gt;(\r\n            \"GetAccount\",\r\n            arguments: new QueryArguments(  \r\n                new QueryArgument&lt;NonNullGraphType&lt;IdGraphType&gt;&gt; { Name = \"userId\" }),\r\n            resolve: async context =&gt;\r\n            {\r\n                var cancellationToken = accessor.HttpContext.RequestAborted; \/\/ Get cancellation token from the context\r\n                return await resolver.ResolveAsync(context, cancellationToken);\r\n            });\r\n    }\r\n}\r\n<\/pre>\n<p>Make sure you have registered <strong>\u2018IHttpContextAccessor\u2019<\/strong> in the Program.cs file using below line of code:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">builder.Services.AddSingleton&lt;IHttpContextAccessor, HttpContextAccessor&gt;();\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>We can pass Cancellation token in GraphQL\u2019s endpoint using IHttpContextAccessor. In the below example we are injecting IHttpContextAccessor in GetAccountQuery class\u2019s constructor, and then accessing cancellation taken as \u2018accessor.HttpContext.RequestAborted\u2019 and then passing it into GraphQL\u2019s endpoint. public class GetAccountQuery : ObjectGraphType { public GetAccountQuery(IResolver resolver, IHttpContextAccessor accessor) { FieldAsync&lt;UserType&gt;( &#8220;GetAccount&#8221;, arguments: new QueryArguments( new QueryArgument&lt;NonNullGraphType&lt;IdGraphType&gt;&gt; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":8827,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[20],"tags":[],"class_list":["post-8726","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-dot-net"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/8726"}],"collection":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/comments?post=8726"}],"version-history":[{"count":4,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/8726\/revisions"}],"predecessor-version":[{"id":8730,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/8726\/revisions\/8730"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/media\/8827"}],"wp:attachment":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/media?parent=8726"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/categories?post=8726"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/tags?post=8726"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}