def print_type(gtype, nonnull=True, except_types=()): if isinstance(gtype, except_types): raise ValidationError(f'{gtype} is not a valid type') literal = None if is_union(gtype): if is_optional(gtype): return f'{print_type(gtype.__args__[0], nonnull=False, except_types=except_types)}' # noqa else: raise ValidationError( f'Native Union type is not supported except Optional') elif is_list(gtype): literal = f'[{print_type(gtype.__args__[0], except_types=except_types)}]' # noqa elif isinstance(gtype, types.ObjectType): literal = f'{gtype.__name__}' elif gtype in VALID_BASIC_TYPES: literal = VALID_BASIC_TYPES[gtype] elif gtype is None or gtype == type(None): # noqa return 'null' elif isinstance(gtype, types.UnionType): literal = f'{gtype.__name__}' elif isinstance(gtype, types.EnumType): literal = f'{gtype.__name__}' elif isinstance(gtype, types.InputType): literal = f'{gtype.__name__}' elif isinstance(gtype, types.InterfaceType): literal = f'{gtype.__name__}' else: raise ValidationError(f'Can not convert type {gtype} to GraphQL type') if nonnull: literal += '!' return literal
def validate(cls): for name, field in cls.__fields__.items(): if name not in cls.VALID_ROOT_TYPES: raise ValidationError( f'The valid root type must be {cls.VALID_ROOT_TYPES},' f' rather than {name}') if not isinstance(field, Field): raise ValidationError(f'{field} is an invalid field type') if not is_optional(field.ftype): raise ValidationError( f'The return type of root object should be Optional') if not isinstance(field.ftype.__args__[0], ObjectType): raise ValidationError( f'The typt of root object must be an Object, rather than {field.ftype}' ) ObjectType.validate(cls)
def load_variable(variable, ptype): if isinstance(ptype, types.InputType): data = {} keys = ptype.__dataclass_fields__.keys() for key, value in variable.items(): snake_cases = to_snake_case(key) data[snake_cases if key not in keys else key] = load_variable( value, ptype.__fields__[snake_cases].ftype) return ptype(**data) elif is_list(ptype): return [load_variable(i, ptype.__args__[0]) for i in variable] elif is_optional(ptype): if variable is None: return None return load_variable(variable, ptype.__args__[0]) elif isinstance(ptype, types.EnumType): return getattr(ptype, variable) else: return variable
def __check_return_type__(cls, return_type, result): if is_optional(return_type): if result is None: return True return cls.__check_return_type__(return_type.__args__[0], result) elif is_list(return_type): if len(result) == 0: return True for item in result: if not cls.__check_return_type__(return_type.__args__[0], item): return False return True elif isinstance(return_type, types.UnionType): for member in return_type.members: if cls.__check_return_type__(member, result): return True elif isinstance(result, return_type): return True return False
def test_is_optional(): assert is_optional(typing.Optional[str]) is True assert is_optional(typing.Union[str, None]) is True assert is_optional(typing.Union[str, int, None]) is False