Skip to content

Commit 318fc43

Browse files
committed
Revert "Remove deprecated get_field_def_fn argument of TypeInfo"
This reverts commit 03a44cb Replicates graphql/graphql-js@e3ac35c
1 parent aa47a83 commit 318fc43

File tree

1 file changed

+17
-8
lines changed

1 file changed

+17
-8
lines changed

src/graphql/utilities/type_info.py

+17-8
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from __future__ import annotations # Python < 3.10
22

3-
from typing import Any, List, Optional, Union, cast
3+
from typing import Any, Callable, List, Optional, Union, cast
44

55
from ..language import (
66
ArgumentNode,
@@ -52,6 +52,11 @@
5252
__all__ = ["TypeInfo", "TypeInfoVisitor"]
5353

5454

55+
GetFieldDefFn = Callable[
56+
[GraphQLSchema, GraphQLType, FieldNode], Optional[GraphQLField]
57+
]
58+
59+
5560
class TypeInfo:
5661
"""Utility class for keeping track of type definitions.
5762
@@ -65,11 +70,14 @@ def __init__(
6570
self,
6671
schema: GraphQLSchema,
6772
initial_type: Optional[GraphQLType] = None,
73+
get_field_def_fn: Optional[GetFieldDefFn] = None,
6874
) -> None:
6975
"""Initialize the TypeInfo for the given GraphQL schema.
7076
7177
Initial type may be provided in rare cases to facilitate traversals beginning
7278
somewhere other than documents.
79+
80+
The optional last parameter is deprecated and will be removed in v3.3.
7381
"""
7482
self._schema = schema
7583
self._type_stack: List[Optional[GraphQLOutputType]] = []
@@ -80,6 +88,7 @@ def __init__(
8088
self._directive: Optional[GraphQLDirective] = None
8189
self._argument: Optional[GraphQLArgument] = None
8290
self._enum_value: Optional[GraphQLEnumValue] = None
91+
self._get_field_def: GetFieldDefFn = get_field_def_fn or get_field_def
8392
if initial_type:
8493
if is_input_type(initial_type):
8594
self._input_type_stack.append(cast(GraphQLInputType, initial_type))
@@ -149,7 +158,7 @@ def enter_selection_set(self, node: SelectionSetNode) -> None:
149158
def enter_field(self, node: FieldNode) -> None:
150159
parent_type = self.get_parent_type()
151160
if parent_type:
152-
field_def = get_field_def(self._schema, parent_type, node)
161+
field_def = self._get_field_def(self._schema, parent_type, node)
153162
field_type = field_def.type if field_def else None
154163
else:
155164
field_def = field_type = None
@@ -268,24 +277,24 @@ def leave_enum_value(self) -> None:
268277

269278

270279
def get_field_def(
271-
schema: GraphQLSchema, parent_type: GraphQLCompositeType, field_node: FieldNode
280+
schema: GraphQLSchema, parent_type: GraphQLType, field_node: FieldNode
272281
) -> Optional[GraphQLField]:
273282
"""Get field definition.
274283
275284
Not exactly the same as the executor's definition of
276285
:func:`graphql.execution.get_field_def`, in this statically evaluated environment
277286
we do not always have an Object type, and need to handle Interface and Union types.
278287
"""
279-
field_name = field_node.name.value
280-
if field_name == "__schema" and schema.query_type is parent_type:
288+
name = field_node.name.value
289+
if name == "__schema" and schema.query_type is parent_type:
281290
return SchemaMetaFieldDef
282-
if field_name == "__type" and schema.query_type is parent_type:
291+
if name == "__type" and schema.query_type is parent_type:
283292
return TypeMetaFieldDef
284-
if field_name == "__typename" and is_composite_type(parent_type):
293+
if name == "__typename" and is_composite_type(parent_type):
285294
return TypeNameMetaFieldDef
286295
if is_object_type(parent_type) or is_interface_type(parent_type):
287296
parent_type = cast(Union[GraphQLObjectType, GraphQLInterfaceType], parent_type)
288-
return parent_type.fields.get(field_name)
297+
return parent_type.fields.get(name)
289298
return None
290299

291300

0 commit comments

Comments
 (0)