Пример #1
0
    def __init__(
        self,
        _type,
        pagination=None,
        fields=None,
        extra_filter_meta=None,
        filterset_class=None,
        *args,
        **kwargs,
    ):

        _fields = _type._meta.filter_fields
        _model = _type._meta.model

        self.fields = fields or _fields
        meta = dict(model=_model, fields=self.fields)
        if extra_filter_meta:
            meta.update(extra_filter_meta)

        filterset_class = filterset_class or _type._meta.filterset_class
        self.filterset_class = get_filterset_class(filterset_class, **meta)
        self.filtering_args = get_filtering_args_from_filterset(
            self.filterset_class, _type)
        kwargs.setdefault("args", {})
        kwargs["args"].update(self.filtering_args)

        if "id" not in kwargs["args"].keys():
            self.filtering_args.update({
                "id":
                Argument(
                    ID,
                    description="Django object unique identification field")
            })
            kwargs["args"].update({
                "id":
                Argument(
                    ID,
                    description="Django object unique identification field")
            })

        pagination = pagination or graphql_api_settings.DEFAULT_PAGINATION_CLASS(
        )

        if pagination is not None:
            assert isinstance(pagination, BaseDjangoGraphqlPagination), (
                'You need to pass a valid DjangoGraphqlPagination in DjangoFilterPaginateListField, received "{}".'
            ).format(pagination)

            pagination_kwargs = pagination.to_graphql_fields()

            self.pagination = pagination
            kwargs.update(**pagination_kwargs)

        if not kwargs.get("description", None):
            kwargs["description"] = "{} list".format(
                _type._meta.model.__name__)

        super(DjangoFilterPaginateListField,
              self).__init__(List(_type), *args, **kwargs)
Пример #2
0
    def _init_pagination_list(cls, pagination):
        pagination_ = pagination
        if pagination_ is None:
            pagination_ = graphql_api_settings.DEFAULT_PAGINATION_CLASS() \
                if graphql_api_settings.DEFAULT_PAGINATION_CLASS else None

        if pagination_ is not None:
            assert isinstance(pagination, BaseDjangoGraphqlPagination), (
                'You need to pass a valid DjangoGraphqlPagination in DjangoFilterPaginateListField, received "{}".'
            ).format(pagination_)

            pagination_kwargs = pagination_.to_graphql_fields()

            return pagination_, pagination_kwargs
        return None