Skip to content

Commit 744411a

Browse files
committed
Add CancellationToken to async methods
CancellationToken arguments have been added to the async methods in the QueryExecutor and GraphQueryExecute classes. This change allows the invocation of these methods to be cancelled if needed, thereby enhancing the control over the execution flow of asynchronous calls.
1 parent 39ed5b7 commit 744411a

2 files changed

Lines changed: 8 additions & 9 deletions

File tree

src/Linq2GraphQL.Client/GraphQueryExecute.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,15 @@ public GraphQueryExecute(GraphClient client, OperationType operationType, QueryN
2222

2323
public T BaseResult { get; set; }
2424

25-
public async Task<T> ExecuteBaseAsync()
25+
public async Task<T> ExecuteBaseAsync(CancellationToken cancellationToken = default)
2626
{
27-
BaseResult = await queryExecutor.ExecuteRequestAsync(QueryNode.Name, await GetRequestAsync());
27+
BaseResult = await queryExecutor.ExecuteRequestAsync(QueryNode.Name, await GetRequestAsync(), cancellationToken);
2828
return BaseResult;
2929
}
3030

31-
public async Task<TResult> ExecuteAsync()
31+
public async Task<TResult> ExecuteAsync(CancellationToken cancellationToken = default)
3232
{
33-
return ConvertResult(await ExecuteBaseAsync());
33+
return ConvertResult(await ExecuteBaseAsync(cancellationToken));
3434
}
3535

3636
}

src/Linq2GraphQL.Client/QueryExecutor.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,18 @@ internal QueryExecutor(GraphClient client)
1414
this.client = client;
1515
}
1616

17-
internal async Task<T> ExecuteRequestAsync(string name, GraphQLRequest graphRequest)
17+
internal async Task<T> ExecuteRequestAsync(string name, GraphQLRequest graphRequest, CancellationToken cancellationToken = default)
1818
{
19-
var json = JsonSerializer.Serialize(graphRequest, client.SerializerOptions);
20-
using var response = await client.HttpClient.PostAsJsonAsync("", graphRequest, client.SerializerOptions);
19+
using var response = await client.HttpClient.PostAsJsonAsync("", graphRequest, client.SerializerOptions, cancellationToken: cancellationToken);
2120

2221
if (!response.IsSuccessStatusCode)
2322
{
24-
var content = await response.Content.ReadAsStringAsync();
23+
var content = await response.Content.ReadAsStringAsync(cancellationToken);
2524
throw new GraphQueryRequestException($"Http error! Status code {response.StatusCode} Error: {content}",
2625
graphRequest.Query);
2726
}
2827

29-
var con = await response.Content.ReadAsStringAsync();
28+
var con = await response.Content.ReadAsStringAsync(cancellationToken);
3029
return ProcessResponse(con, name, graphRequest.Query);
3130
}
3231

0 commit comments

Comments
 (0)