@@ -353,6 +353,25 @@ def parse_result(
353353 for child in self ._children :
354354 child .parse_result (data , output , progress_data )
355355
356+ def _query_data (self , con : ServerAPI ) -> dict [str , Any ]:
357+ """Send single query to server and return 'data' of the response."""
358+ query_str = self .calculate_query ()
359+ variables = self .get_variables_values ()
360+ response = con .query_graphql (query_str , variables )
361+ if response .errors :
362+ raise GraphQlQueryFailed (response .errors , query_str , variables )
363+
364+ data = response .data .get ("data" )
365+ if data is None :
366+ # Parsing 'None' would not change pagination state and the same
367+ # query would be sent again in an infinite loop.
368+ raise GraphQlQueryError (
369+ f"GraphQl query '{ self ._name } ' response does not contain"
370+ f" 'data'. Response: { str (response .data )[:1000 ]} "
371+ f"\n Query:\n { query_str } \n Variables: { variables } "
372+ )
373+ return data
374+
356375 def query (self , con : ServerAPI ) -> dict [str , Any ]:
357376 """Do a query from server.
358377
@@ -366,15 +385,8 @@ def query(self, con: ServerAPI) -> dict[str, Any]:
366385 progress_data = {}
367386 output = {}
368387 while self .need_query :
369- query_str = self .calculate_query ()
370- variables = self .get_variables_values ()
371- response = con .query_graphql (
372- query_str ,
373- variables
374- )
375- if response .errors :
376- raise GraphQlQueryFailed (response .errors , query_str , variables )
377- self .parse_result (response .data ["data" ], output , progress_data )
388+ data = self ._query_data (con )
389+ self .parse_result (data , output , progress_data )
378390
379391 return output
380392
@@ -394,30 +406,16 @@ def continuous_query(
394406 if self .has_multiple_edge_fields :
395407 output = {}
396408 while self .need_query :
397- query_str = self .calculate_query ()
398- variables = self .get_variables_values ()
399-
400- response = con .query_graphql (query_str , variables )
401- if response .errors :
402- raise GraphQlQueryFailed (
403- response .errors , query_str , variables
404- )
405- self .parse_result (response .data ["data" ], output , progress_data )
409+ data = self ._query_data (con )
410+ self .parse_result (data , output , progress_data )
406411
407412 yield output
408413
409414 else :
410415 while self .need_query :
411416 output = {}
412- query_str = self .calculate_query ()
413- variables = self .get_variables_values ()
414- response = con .query_graphql (query_str , variables )
415- if response .errors :
416- raise GraphQlQueryFailed (
417- response .errors , query_str , variables
418- )
419-
420- self .parse_result (response .data ["data" ], output , progress_data )
417+ data = self ._query_data (con )
418+ self .parse_result (data , output , progress_data )
421419
422420 yield output
423421
@@ -945,6 +943,14 @@ def parse_result(
945943 change_cursor = False
946944
947945 if change_cursor and self ._need_query :
946+ if new_cursor is None :
947+ # Without cursor the pagination would start from beginning
948+ raise GraphQlQueryError (
949+ f"Field '{ self .path } ' reported another page without"
950+ " a cursor. Stopped pagination after"
951+ f" { self ._fetched_counter } items."
952+ )
953+
948954 if new_cursor == self ._cursor :
949955 raise GraphQlQueryError (
950956 "Cursor didn't change during pagination."
0 commit comments