Skip to content

Commit 5e9facc

Browse files
author
Mattia Baldari
committed
Fix compatibility with graphql-core 3.2.7+ using TypeFields
Prior to this change, schemadiff failed to import with graphql-core 3.2.7+ because TypeResolvers was removed from the public API. This change creates the _get_type_resolvers() helper to use TypeFields, the official replacement class introduced in graphql-core 3.2.7, with graceful fallback to older import strategies. The fallback order is: 1. Try TypeFields (official, graphql-core >= 3.2.7) 2. Try TypeResolvers (legacy, graphql-core 3.2.0-3.2.6) 3. Try TypeFieldResolvers (legacy, graphql-core < 3.2.0) Benefits: - Uses official TypeFields class per maintainer recommendation - Maintains backward compatibility with all graphql-core versions - Simpler and more maintainable than reconstructing from introspection - Better performance using native classes References: graphql-python/graphql-core#246 (comment)
1 parent 7581e38 commit 5e9facc

File tree

1 file changed

+29
-5
lines changed

1 file changed

+29
-5
lines changed

schemadiff/diff/schema.py

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,6 @@
88
is_interface_type,
99
)
1010

11-
try:
12-
from graphql.type.introspection import TypeResolvers
13-
except ImportError:
14-
from graphql.type.introspection import TypeFieldResolvers as TypeResolvers # graphql-core < 3.2.0
15-
1611
from schemadiff.changes.directive import RemovedDirective, AddedDirective
1712
from schemadiff.changes.schema import (
1813
SchemaQueryTypeChanged,
@@ -32,6 +27,35 @@
3227
from schemadiff.diff.union_type import UnionType
3328
from schemadiff.diff.input_object_type import InputObjectType
3429

30+
def _get_type_resolvers():
31+
"""Get TypeResolvers/TypeFields with fallback for different graphql-core versions."""
32+
# Try modern graphql-core (>= 3.2.7) - TypeFields is the official replacement
33+
try:
34+
from graphql.type.introspection import TypeFields
35+
return TypeFields
36+
except ImportError:
37+
pass
38+
39+
# Try graphql-core 3.2.0 - 3.2.6
40+
try:
41+
from graphql.type.introspection import TypeResolvers
42+
return TypeResolvers
43+
except ImportError:
44+
pass
45+
46+
# Fallback for older graphql-core (< 3.2.0)
47+
try:
48+
from graphql.type.introspection import TypeFieldResolvers
49+
return TypeFieldResolvers
50+
except ImportError:
51+
raise ImportError(
52+
"Could not import TypeFields, TypeResolvers, or TypeFieldResolvers from graphql-core. "
53+
"Please upgrade to a supported version of graphql-core."
54+
)
55+
56+
57+
TypeResolvers = _get_type_resolvers()
58+
3559
type_kind = partial(TypeResolvers.kind, _info={})
3660

3761

0 commit comments

Comments
 (0)