@@ -9,6 +9,8 @@ use graph::components::server::query::ServerResponse;
9
9
use graph:: components:: server:: query:: ServerResult ;
10
10
use graph:: components:: versions:: ApiVersion ;
11
11
use graph:: data:: query:: QueryResult ;
12
+ use graph:: data:: query:: SqlQueryMode ;
13
+ use graph:: data:: query:: SqlQueryReq ;
12
14
use graph:: data:: subgraph:: DeploymentHash ;
13
15
use graph:: data:: subgraph:: SubgraphName ;
14
16
use graph:: env:: ENV_VARS ;
@@ -21,6 +23,8 @@ use graph::hyper::{body::Body, header::HeaderValue};
21
23
use graph:: hyper:: { Method , Request , Response , StatusCode } ;
22
24
use graph:: prelude:: serde_json;
23
25
use graph:: prelude:: serde_json:: json;
26
+ use graph:: prelude:: CacheWeight as _;
27
+ use graph:: prelude:: QueryError ;
24
28
use graph:: semver:: VersionReq ;
25
29
use graph:: slog:: error;
26
30
use graph:: slog:: Logger ;
@@ -195,6 +199,51 @@ where
195
199
Ok ( result. as_http_response ( ) )
196
200
}
197
201
202
+ async fn handle_sql_query < T : Body > ( & self , request : Request < T > ) -> ServerResult {
203
+ let body = request
204
+ . collect ( )
205
+ . await
206
+ . map_err ( |_| ServerError :: InternalError ( "Failed to read request body" . into ( ) ) ) ?
207
+ . to_bytes ( ) ;
208
+ let sql_req: SqlQueryReq = serde_json:: from_slice ( & body)
209
+ . map_err ( |e| ServerError :: ClientError ( format ! ( "{}" , e) ) ) ?;
210
+
211
+ let mode = sql_req. mode ;
212
+ let result = self
213
+ . graphql_runner
214
+ . cheap_clone ( )
215
+ . run_sql_query ( sql_req)
216
+ . await
217
+ . map_err ( |e| ServerError :: QueryError ( QueryError :: from ( e) ) ) ;
218
+
219
+ use SqlQueryMode :: * ;
220
+ let response_obj = match ( result, mode) {
221
+ ( Ok ( result) , Info ) => {
222
+ json ! ( {
223
+ "count" : result. len( ) ,
224
+ "bytes" : result. weight( ) ,
225
+ } )
226
+ }
227
+ ( Ok ( result) , Data ) => {
228
+ json ! ( {
229
+ "data" : result,
230
+ } )
231
+ }
232
+ ( Err ( e) , _) => json ! ( {
233
+ "error" : e. to_string( ) ,
234
+ } ) ,
235
+ } ;
236
+
237
+ let response_str = serde_json:: to_string ( & response_obj) . unwrap ( ) ;
238
+
239
+ Ok ( Response :: builder ( )
240
+ . status ( 200 )
241
+ . header ( ACCESS_CONTROL_ALLOW_ORIGIN , "*" )
242
+ . header ( CONTENT_TYPE , "application/json" )
243
+ . body ( Full :: from ( response_str) )
244
+ . unwrap ( ) )
245
+ }
246
+
198
247
// Handles OPTIONS requests
199
248
fn handle_graphql_options < T > ( & self , _request : Request < T > ) -> ServerResult {
200
249
Ok ( Response :: builder ( )
@@ -327,7 +376,9 @@ where
327
376
let dest = format ! ( "/{}/graphql" , filtered_path) ;
328
377
self . handle_temp_redirect ( dest)
329
378
}
330
-
379
+ ( Method :: POST , & [ "subgraphs" , "sql" ] | & [ "subgraphs" , "sql" , "" ] ) => {
380
+ self . handle_sql_query ( req) . await
381
+ }
331
382
( Method :: POST , & [ "subgraphs" , "id" , subgraph_id] ) => {
332
383
self . handle_graphql_query_by_id ( subgraph_id. to_owned ( ) , req)
333
384
. await
@@ -395,14 +446,15 @@ where
395
446
396
447
#[ cfg( test) ]
397
448
mod tests {
449
+ use graph:: data:: store:: SqlQueryObject ;
398
450
use graph:: data:: value:: { Object , Word } ;
399
451
use graph:: http_body_util:: { BodyExt , Full } ;
400
452
use graph:: hyper:: body:: Bytes ;
401
453
use graph:: hyper:: header:: { CONTENT_LENGTH , CONTENT_TYPE } ;
402
454
use graph:: hyper:: { Method , Request , StatusCode } ;
403
455
use graph:: prelude:: serde_json:: json;
404
456
405
- use graph:: data:: query:: { QueryResults , QueryTarget } ;
457
+ use graph:: data:: query:: { QueryResults , QueryTarget , SqlQueryReq } ;
406
458
use graph:: prelude:: * ;
407
459
408
460
use crate :: test_utils;
@@ -449,6 +501,13 @@ mod tests {
449
501
fn metrics ( & self ) -> Arc < dyn GraphQLMetrics > {
450
502
Arc :: new ( TestGraphQLMetrics )
451
503
}
504
+
505
+ async fn run_sql_query (
506
+ self : Arc < Self > ,
507
+ _req : SqlQueryReq ,
508
+ ) -> Result < Vec < SqlQueryObject > , QueryExecutionError > {
509
+ unimplemented ! ( )
510
+ }
452
511
}
453
512
454
513
#[ tokio:: test]
0 commit comments