26
26
OperationType ,
27
27
SelectionSetNode ,
28
28
)
29
- from ..pyutils import inspect , is_invalid , is_nullish , AwaitableOrValue , FrozenList
29
+ from ..pyutils import (
30
+ inspect ,
31
+ is_invalid ,
32
+ is_nullish ,
33
+ AwaitableOrValue ,
34
+ FrozenList ,
35
+ Path ,
36
+ )
30
37
from ..utilities import get_operation_root_type , type_from_ast
31
38
from ..type import (
32
39
GraphQLAbstractType ,
42
49
GraphQLFieldResolver ,
43
50
GraphQLTypeResolver ,
44
51
GraphQLResolveInfo ,
45
- ResponsePath ,
46
52
SchemaMetaFieldDef ,
47
53
TypeMetaFieldDef ,
48
54
TypeNameMetaFieldDef ,
57
63
from .values import get_argument_values , get_directive_values , get_variable_values
58
64
59
65
__all__ = [
60
- "add_path" ,
61
66
"assert_valid_execution_arguments" ,
62
67
"default_field_resolver" ,
63
68
"default_type_resolver" ,
64
69
"execute" ,
65
70
"get_field_def" ,
66
- "response_path_as_list" ,
67
71
"ExecutionResult" ,
68
72
"ExecutionContext" ,
69
73
"Middleware" ,
@@ -100,6 +104,7 @@ class ExecutionResult(NamedTuple):
100
104
errors : Optional [List [GraphQLError ]]
101
105
102
106
107
+ # noinspection PyTypeHints
103
108
ExecutionResult .__new__ .__defaults__ = (None , None ) # type: ignore
104
109
105
110
Middleware = Optional [Union [Tuple , List , MiddlewareManager ]]
@@ -377,7 +382,7 @@ def execute_fields_serially(
377
382
self ,
378
383
parent_type : GraphQLObjectType ,
379
384
source_value : Any ,
380
- path : Optional [ResponsePath ],
385
+ path : Optional [Path ],
381
386
fields : Dict [str , List [FieldNode ]],
382
387
) -> AwaitableOrValue [Dict [str , Any ]]:
383
388
"""Execute the given fields serially.
@@ -386,7 +391,7 @@ def execute_fields_serially(
386
391
"""
387
392
results : Dict [str , Any ] = {}
388
393
for response_name , field_nodes in fields .items ():
389
- field_path = add_path (path , response_name )
394
+ field_path = Path (path , response_name )
390
395
result = self .resolve_field (
391
396
parent_type , source_value , field_nodes , field_path
392
397
)
@@ -427,7 +432,7 @@ def execute_fields(
427
432
self ,
428
433
parent_type : GraphQLObjectType ,
429
434
source_value : Any ,
430
- path : Optional [ResponsePath ],
435
+ path : Optional [Path ],
431
436
fields : Dict [str , List [FieldNode ]],
432
437
) -> AwaitableOrValue [Dict [str , Any ]]:
433
438
"""Execute the given fields concurrently.
@@ -438,7 +443,7 @@ def execute_fields(
438
443
awaitable_fields : List [str ] = []
439
444
append_awaitable = awaitable_fields .append
440
445
for response_name , field_nodes in fields .items ():
441
- field_path = add_path (path , response_name )
446
+ field_path = Path (path , response_name )
442
447
result = self .resolve_field (
443
448
parent_type , source_value , field_nodes , field_path
444
449
)
@@ -559,7 +564,7 @@ def build_resolve_info(
559
564
field_def : GraphQLField ,
560
565
field_nodes : List [FieldNode ],
561
566
parent_type : GraphQLObjectType ,
562
- path : ResponsePath ,
567
+ path : Path ,
563
568
) -> GraphQLResolveInfo :
564
569
# The resolve function's first argument is a collection of information about
565
570
# the current execution state.
@@ -582,7 +587,7 @@ def resolve_field(
582
587
parent_type : GraphQLObjectType ,
583
588
source : Any ,
584
589
field_nodes : List [FieldNode ],
585
- path : ResponsePath ,
590
+ path : Path ,
586
591
) -> AwaitableOrValue [Any ]:
587
592
"""Resolve the field on the given source object.
588
593
@@ -652,7 +657,7 @@ def complete_value_catching_error(
652
657
return_type : GraphQLOutputType ,
653
658
field_nodes : List [FieldNode ],
654
659
info : GraphQLResolveInfo ,
655
- path : ResponsePath ,
660
+ path : Path ,
656
661
result : Any ,
657
662
) -> AwaitableOrValue [Any ]:
658
663
"""Complete a value while catching an error.
@@ -694,10 +699,10 @@ def handle_field_error(
694
699
self ,
695
700
raw_error : Exception ,
696
701
field_nodes : List [FieldNode ],
697
- path : ResponsePath ,
702
+ path : Path ,
698
703
return_type : GraphQLOutputType ,
699
704
) -> None :
700
- error = located_error (raw_error , field_nodes , response_path_as_list ( path ))
705
+ error = located_error (raw_error , field_nodes , path . as_list ( ))
701
706
702
707
# If the field type is non-nullable, then it is resolved without any protection
703
708
# from errors, however it still properly locates the error.
@@ -713,7 +718,7 @@ def complete_value(
713
718
return_type : GraphQLOutputType ,
714
719
field_nodes : List [FieldNode ],
715
720
info : GraphQLResolveInfo ,
716
- path : ResponsePath ,
721
+ path : Path ,
717
722
result : Any ,
718
723
) -> AwaitableOrValue [Any ]:
719
724
"""Complete a value.
@@ -797,7 +802,7 @@ def complete_list_value(
797
802
return_type : GraphQLList [GraphQLOutputType ],
798
803
field_nodes : List [FieldNode ],
799
804
info : GraphQLResolveInfo ,
800
- path : ResponsePath ,
805
+ path : Path ,
801
806
result : Iterable [Any ],
802
807
) -> AwaitableOrValue [Any ]:
803
808
"""Complete a list value.
@@ -821,7 +826,7 @@ def complete_list_value(
821
826
for index , item in enumerate (result ):
822
827
# No need to modify the info object containing the path, since from here on
823
828
# it is not ever accessed by resolver functions.
824
- field_path = add_path ( path , index )
829
+ field_path = path . add_key ( index )
825
830
completed_item = self .complete_value_catching_error (
826
831
item_type , field_nodes , info , field_path , item
827
832
)
@@ -866,7 +871,7 @@ def complete_abstract_value(
866
871
return_type : GraphQLAbstractType ,
867
872
field_nodes : List [FieldNode ],
868
873
info : GraphQLResolveInfo ,
869
- path : ResponsePath ,
874
+ path : Path ,
870
875
result : Any ,
871
876
) -> AwaitableOrValue [Any ]:
872
877
"""Complete an abstract value.
@@ -947,7 +952,7 @@ def complete_object_value(
947
952
return_type : GraphQLObjectType ,
948
953
field_nodes : List [FieldNode ],
949
954
info : GraphQLResolveInfo ,
950
- path : ResponsePath ,
955
+ path : Path ,
951
956
result : Any ,
952
957
) -> AwaitableOrValue [Dict [str , Any ]]:
953
958
"""Complete an Object value by executing all sub-selections."""
@@ -981,7 +986,7 @@ def collect_and_execute_subfields(
981
986
self ,
982
987
return_type : GraphQLObjectType ,
983
988
field_nodes : List [FieldNode ],
984
- path : ResponsePath ,
989
+ path : Path ,
985
990
result : Any ,
986
991
) -> AwaitableOrValue [Dict [str , Any ]]:
987
992
"""Collect sub-fields to execute to complete this value."""
@@ -1041,29 +1046,6 @@ def assert_valid_execution_arguments(
1041
1046
)
1042
1047
1043
1048
1044
- def response_path_as_list (path : ResponsePath ) -> List [Union [str , int ]]:
1045
- """Get response path as a list.
1046
-
1047
- Given a ResponsePath (found in the `path` entry in the information provided as the
1048
- last argument to a field resolver), return a list of the path keys.
1049
- """
1050
- flattened : List [Union [str , int ]] = []
1051
- append = flattened .append
1052
- curr : Optional [ResponsePath ] = path
1053
- while curr :
1054
- append (curr .key )
1055
- curr = curr .prev
1056
- return flattened [::- 1 ]
1057
-
1058
-
1059
- def add_path (prev : Optional [ResponsePath ], key : Union [str , int ]) -> ResponsePath :
1060
- """Add a key to a response path.
1061
-
1062
- Given a ResponsePath and a key, return a new ResponsePath containing the new key.
1063
- """
1064
- return ResponsePath (prev , key )
1065
-
1066
-
1067
1049
def get_field_def (
1068
1050
schema : GraphQLSchema , parent_type : GraphQLObjectType , field_name : str
1069
1051
) -> GraphQLField :
0 commit comments