When defining GraphQL types (like GraphQLInterfaceType or GraphQLObjectType), you may encounter situations where a field refers to a type that is still being defined (e.g., a Character interface having a field friends that returns a list of Character).
To resolve this circular dependency, you must pass the dictionary of fields as a thunk (a lambda function) rather than a direct dictionary. This allows the GraphQL-core engine to evaluate the fields only after the types have been initialized.
# Using a lambda (thunk) to allow the interface to refer to itself
character_interface = GraphQLInterfaceType('Character', lambda: {
'id': GraphQLField(GraphQLNonNull(GraphQLString)),
'friends': GraphQLField(GraphQLList(character_interface)), # Recursive reference
# ... other fields
}, resolve_type=get_character_type)