@@ -21,6 +21,56 @@ public async Task<IReadOnlyList<Cell>> GetCellsAsync(CancellationToken ct = defa
2121 public async Task < IReadOnlyList < Operation > > GetOperationsAsync ( CancellationToken ct = default )
2222 => await QueryAsync < Operation > ( "query { Operations { id tenantId type status createdAt } }" , "Operations" , ct ) ;
2323
24+ public async Task < Tenant > CreateTenantAsync ( Tenant tenant , CancellationToken ct = default )
25+ {
26+ var mutation = @"mutation($input: CreateTenantInput!) {
27+ createTenant(input: $input) { id displayName domain tier status cellId }
28+ }" ;
29+ var variables = new
30+ {
31+ input = new
32+ {
33+ id = tenant . Id ,
34+ pk = tenant . Id ,
35+ displayName = tenant . DisplayName ,
36+ domain = tenant . Domain ,
37+ tier = tenant . Tier ,
38+ status = tenant . Status ,
39+ cellId = tenant . CellId
40+ }
41+ } ;
42+ return await MutationAsync < Tenant > ( mutation , variables , "createTenant" , ct ) ;
43+ }
44+
45+ public async Task < Tenant > UpdateTenantAsync ( Tenant tenant , CancellationToken ct = default )
46+ {
47+ var mutation = @"mutation($id: ID!, $input: UpdateTenantInput!) {
48+ updateTenant(id: $id, input: $input) { id displayName domain tier status cellId }
49+ }" ;
50+ var variables = new
51+ {
52+ id = tenant . Id ,
53+ input = new
54+ {
55+ displayName = tenant . DisplayName ,
56+ domain = tenant . Domain ,
57+ tier = tenant . Tier ,
58+ status = tenant . Status ,
59+ cellId = tenant . CellId
60+ }
61+ } ;
62+ return await MutationAsync < Tenant > ( mutation , variables , "updateTenant" , ct ) ;
63+ }
64+
65+ public async Task DeleteTenantAsync ( string id , string partitionKey , CancellationToken ct = default )
66+ {
67+ var mutation = @"mutation($id: ID!, $pk: String!) {
68+ deleteTenant(id: $id, partitionKeyValue: $pk)
69+ }" ;
70+ var variables = new { id , pk = partitionKey } ;
71+ await MutationAsync < object > ( mutation , variables , "deleteTenant" , ct ) ;
72+ }
73+
2474 private async Task < IReadOnlyList < T > > QueryAsync < T > ( string query , string rootField , CancellationToken ct )
2575 {
2676 var payload = new { query } ;
@@ -41,4 +91,21 @@ private async Task<IReadOnlyList<T>> QueryAsync<T>(string query, string rootFiel
4191 }
4292 return list ;
4393 }
94+
95+ private async Task < T > MutationAsync < T > ( string query , object variables , string rootField , CancellationToken ct )
96+ {
97+ var payload = new { query , variables } ;
98+ using var req = new HttpRequestMessage ( HttpMethod . Post , "" )
99+ {
100+ Content = new StringContent ( JsonSerializer . Serialize ( payload ) , Encoding . UTF8 , "application/json" )
101+ } ;
102+ using var res = await Client . SendAsync ( req , ct ) ;
103+ res . EnsureSuccessStatusCode ( ) ;
104+ using var stream = await res . Content . ReadAsStreamAsync ( ct ) ;
105+ using var doc = await JsonDocument . ParseAsync ( stream , cancellationToken : ct ) ;
106+ var data = doc . RootElement . GetProperty ( "data" ) . GetProperty ( rootField ) ;
107+ if ( typeof ( T ) == typeof ( object ) ) return default ! ;
108+ var result = data . Deserialize < T > ( new JsonSerializerOptions { PropertyNameCaseInsensitive = true } ) ;
109+ return result ! ;
110+ }
44111}
0 commit comments