33import graphene
44from graphene import Schema , ObjectType
55from graphene .types .definitions import GrapheneObjectType
6+ from graphene .types .scalars import ScalarOptions
67from graphene .types .union import UnionOptions
78from graphene .utils .str_converters import to_camel_case
8- from graphql import parse , GraphQLScalarType
9+ from graphql import parse , GraphQLScalarType , GraphQLNonNull
910
1011
1112def field_name_to_type_attribute (schema : Schema , model : Any ) -> Callable [[str ], str ]:
@@ -47,9 +48,11 @@ def is_valid_compound_key(type_name: str, key: str, schema: Schema):
4748
4849 while key_nodes :
4950 selection_node , parent_object_type = key_nodes [0 ]
50-
51- for field in selection_node .selection_set .selections :
51+ if isinstance (parent_object_type , GraphQLNonNull ):
52+ parent_type_fields = parent_object_type .of_type .fields
53+ else :
5254 parent_type_fields = parent_object_type .fields
55+ for field in selection_node .selection_set .selections :
5356 if schema .auto_camelcase :
5457 field_name = to_camel_case (field .name .value )
5558 else :
@@ -62,14 +65,20 @@ def is_valid_compound_key(type_name: str, key: str, schema: Schema):
6265 if field .selection_set :
6366 # If the field has sub-selections, add it to node mappings to check for valid subfields
6467
65- if isinstance (field_type , GraphQLScalarType ):
68+ if isinstance (field_type , GraphQLScalarType ) or (
69+ isinstance (field_type , GraphQLNonNull )
70+ and isinstance (field_type .of_type , GraphQLScalarType )
71+ ):
6672 # sub-selections are added to a scalar type, key is not valid
6773 return False
6874
6975 key_nodes .append ((field , field_type ))
7076 else :
7177 # If there are no sub-selections for a field, it should be a scalar
72- if not isinstance (field_type , GraphQLScalarType ):
78+ if not isinstance (field_type , GraphQLScalarType ) and not (
79+ isinstance (field_type , GraphQLNonNull )
80+ and isinstance (field_type .of_type , GraphQLScalarType )
81+ ):
7382 return False
7483
7584 key_nodes .pop (0 ) # Remove the current node as it is fully processed
@@ -80,8 +89,10 @@ def is_valid_compound_key(type_name: str, key: str, schema: Schema):
8089def get_attributed_fields (attribute : str , schema : Schema ):
8190 fields = {}
8291 for type_name , type_ in schema .graphql_schema .type_map .items ():
83- if not hasattr (type_ , "graphene_type" ) or isinstance (
84- type_ .graphene_type ._meta , UnionOptions
92+ if (
93+ not hasattr (type_ , "graphene_type" )
94+ or isinstance (type_ .graphene_type ._meta , UnionOptions )
95+ or isinstance (type_ .graphene_type ._meta , ScalarOptions )
8596 ):
8697 continue
8798 for field in list (type_ .graphene_type ._meta .fields ):
0 commit comments