Exemplo n.º 1
0
def _convert_from_pydantic_to_strawberry_type(
    type_: Union[StrawberryType, type], data_from_model=None, extra=None
):
    data = data_from_model if data_from_model is not None else extra

    if isinstance(type_, StrawberryOptional):
        if data is None:
            return data
        return _convert_from_pydantic_to_strawberry_type(
            type_.of_type, data_from_model=data, extra=extra
        )
    if isinstance(type_, StrawberryUnion):
        for option_type in type_.types:
            if hasattr(option_type, "_pydantic_type"):
                source_type = option_type._pydantic_type  # type: ignore
            else:
                source_type = cast(type, option_type)
            if isinstance(data, source_type):
                return _convert_from_pydantic_to_strawberry_type(
                    option_type, data_from_model=data, extra=extra
                )
    if isinstance(type_, EnumDefinition):
        return data
    if isinstance(type_, StrawberryList):
        items = []
        for index, item in enumerate(data):
            items.append(
                _convert_from_pydantic_to_strawberry_type(
                    type_.of_type,
                    data_from_model=item,
                    extra=extra[index] if extra else None,
                )
            )

        return items
    elif is_scalar(type_):
        return data
    else:
        # in the case of an interface, the concrete type may be more specific
        # than the type in the field definition
        if hasattr(type(data), "_strawberry_type"):
            type_ = type(data)._strawberry_type
        return convert_pydantic_model_to_strawberry_class(
            type_, model_instance=data_from_model, extra=extra
        )
Exemplo n.º 2
0
def _convert_from_pydantic_to_strawberry_field(field: FieldDefinition,
                                               data_from_model=None,
                                               extra=None):
    data = data_from_model or extra

    if field.is_list:
        child = field.child
        items = [None for _ in data]

        for index, item in enumerate(data):
            items[index] = _convert_from_pydantic_to_strawberry_field(
                cast(FieldDefinition, child),
                data_from_model=item,
                extra=extra[index] if extra else None,
            )

        return items
    elif is_scalar(field.type):  # type: ignore
        return data
    else:
        return convert_pydantic_model_to_strawberry_class(
            field.type, model_instance=data_from_model, extra=extra)
Exemplo n.º 3
0
def _convert_from_pydantic_to_strawberry_field(field: StrawberryField,
                                               data_from_model=None,
                                               extra=None):
    data = data_from_model if data_from_model is not None else extra

    if field.is_list:
        assert field.child is not None

        items = [None for _ in data]

        for index, item in enumerate(data):
            items[index] = _convert_from_pydantic_to_strawberry_field(
                field.child,
                data_from_model=item,
                extra=extra[index] if extra else None,
            )

        return items
    elif is_scalar(field.type):  # type: ignore
        return data
    else:
        return convert_pydantic_model_to_strawberry_class(
            field.type, model_instance=data_from_model, extra=extra)
Exemplo n.º 4
0
def get_type_for_annotation(annotation: Type,
                            type_map: TypeMap) -> GraphQLType:
    graphql_type: Optional[GraphQLType] = None

    # this adds support for type annotations that use
    # strings, without having to use get_type_hints

    if type(annotation) == str and annotation in type_map:
        return type_map[annotation].implementation  # type: ignore

    if is_scalar(annotation):
        graphql_type = get_scalar_type(annotation, type_map)

    elif hasattr(annotation, "_enum_definition"):
        graphql_type = get_enum_type(annotation._enum_definition, type_map)
    elif hasattr(annotation, "_type_definition"):
        from .object_type import get_object_type

        graphql_type = get_object_type(annotation, type_map)

    if not graphql_type:
        raise ValueError(f"Unable to get GraphQL type for {annotation}")

    return graphql_type
Exemplo n.º 5
0
def _is_scalar(type_: Union[StrawberryType, type]) -> TypeGuard[type]:
    # isinstance(type_, StrawberryScalar)  # noqa: E800
    return is_scalar(type_)
Exemplo n.º 6
0
def _is_scalar(type_: Type) -> bool:
    # isinstance(type_, StrawberryScalar)  # noqa: E800
    return is_scalar(type_)
Exemplo n.º 7
0
def _is_scalar(
    type_: Union[StrawberryType, type],
    scalar_registry: Dict[object, Union[ScalarWrapper, ScalarDefinition]],
) -> TypeGuard[type]:
    # isinstance(type_, StrawberryScalar)  # noqa: E800
    return is_scalar(type_, scalar_registry)