def visit_conversion( self, tp: AnyType, conversion: Optional[Any], dynamic: bool, next_conversion: Optional[AnyConversion] = None, ): ref_types = [] if not dynamic: for ref_tp in self.resolve_conversion(tp): ref_types.append(ref_tp) if self._incr_ref(get_type_name(ref_tp).json_schema, ref_tp): return if not is_hashable(tp): return super().visit_conversion(tp, conversion, dynamic, next_conversion) # 2 because the first type encountered of the recursive cycle can have no ref # (see test_recursive_by_conversion_schema) if self._rec_guard[(tp, self._conversions)] > 2: raise TypeError(f"Recursive type {tp} need a ref") self._rec_guard[(tp, self._conversions)] += 1 try: super().visit_conversion(tp, conversion, dynamic, next_conversion) except Unsupported: for ref_tp in ref_types: self.refs.pop(get_type_name(ref_tp).json_schema, ...) # type: ignore finally: self._rec_guard[(tp, self._conversions)] -= 1
def get_node_name(tp): if is_union_of(tp, NoneType) and len(get_args2(tp)): tp = next(arg for arg in get_args2(tp) if arg is not NoneType) ref = get_type_name(tp).graphql if ref is None: raise TypeError( f"Node {tp} must have a ref registered to be used with connection") return ref
def visit_conversion( self, tp: AnyType, conversion: Optional[Conv], dynamic: bool, next_conversion: Optional[AnyConversion] = None, ) -> TypeFactory[GraphQLTp]: if not dynamic and self.is_id(tp) or tp == ID: return TypeFactory(lambda *_: graphql.GraphQLNonNull(self.id_type)) factory = super().visit_conversion(tp, conversion, dynamic, next_conversion) if not dynamic: factory = factory.merge(get_type_name(tp), get_schema(tp)) if get_args(tp): factory = factory.merge(schema=get_schema(get_origin(tp))) return factory # type: ignore
def visit_conversion( self, tp: AnyType, conversion: Optional[Conv], dynamic: bool, next_conversion: Optional[AnyConversion] = None, ) -> JsonSchema: schemas = [] if not dynamic: for ref_tp in self.resolve_conversion(tp): ref_schema = self.ref_schema(get_type_name(ref_tp).json_schema) if ref_schema is not None: return ref_schema if get_args(tp): schemas.append(get_schema(get_origin_or_type(tp))) schemas.append(get_schema(tp)) result = super().visit_conversion(tp, conversion, dynamic, next_conversion) return reduce(full_schema, schemas, result)
def test_collection_type_name(): type_name("test")(Sequence[A]) assert get_type_name(List[A]) == get_type_name(Collection[A]) == ("test", "test")
def _node_key(cls) -> str: node_name = get_type_name(cls).graphql if node_name is None: raise TypeError(f"Node {cls} has no type_name registered") return node_name