예제 #1
0
 def connection_resolver(resolver, connection, default_manager, root, args,
                         context, info):
     iterable = resolver(root, args, context, info)
     if iterable is None:
         iterable = default_manager
     iterable = maybe_queryset(iterable)
     if isinstance(iterable, QuerySet):
         if iterable is not default_manager:
             iterable &= maybe_queryset(default_manager)
         _len = iterable.count()
     else:
         _len = len(iterable)
     connection = connection_from_list_slice(
         iterable,
         args,
         slice_start=0,
         list_length=_len,
         list_slice_length=_len,
         connection_type=connection,
         edge_type=connection.Edge,
         pageinfo_type=PageInfo,
     )
     connection.iterable = iterable
     connection.length = _len
     return connection
예제 #2
0
    def connection_resolver(cls, resolver, connection, model, root, info,
                            **args):
        iterable = resolver(root, info, **args)

        if not iterable:
            iterable, _len = cls.get_query(model, info, **args)

            if root:
                # If we have a root, we must be at least 1 layer in, right?
                _len = 0
        else:
            _len = len(iterable)

        connection = connection_from_list_slice(
            iterable,
            args,
            slice_start=0,
            list_length=_len,
            list_slice_length=_len,
            connection_type=connection,
            pageinfo_type=PageInfo,
            edge_type=connection.Edge,
        )
        connection.iterable = iterable
        connection.length = _len
        return connection
예제 #3
0
 def resolve_connection(cls, connection, default_search, args, results):
     if results is None:
         results = default_search
     if isinstance(results, Search):
         if results is not default_search:
             results = cls.merge_querysets(default_search, results)
         query = args.get('query')
         if query:
             default_field = args.get('default_field')
             results = results.query('query_string',
                                     default_field=default_field,
                                     query=query)
         results = results.execute()
     _len = results.hits.total
     connection = connection_from_list_slice(
         results.hits,
         args,
         slice_start=0,
         list_length=_len,
         list_slice_length=_len,
         connection_type=connection,
         edge_type=connection.Edge,
         pageinfo_type=PageInfo,
     )
     connection.iterable = results.hits
     connection.length = _len
     return connection
예제 #4
0
 def resolve_connection(cls, connection, args, iterable, max_limit=None):
     common_args = {
         "connection_type": connection,
         "edge_type": connection.Edge,
         "pageinfo_type": PageInfo,
     }
     if isinstance(iterable, list):
         common_args["args"] = args
         _len = len(iterable)
         connection = connection_from_list_slice(
             iterable,
             slice_start=0,
             list_length=_len,
             list_slice_length=_len,
             **common_args,
         )
     else:
         iterable, sort_by = sort_queryset_for_connection(iterable=iterable,
                                                          args=args)
         args["sort_by"] = sort_by
         common_args["args"] = args
         connection = connection_from_queryset_slice(
             iterable, **common_args)
     connection.iterable = iterable
     return connection
예제 #5
0
    def connection_resolver(self, resolver, connection, root, info, **args):
        args['first'] = min(args.get('first', self._max_first),
                            self._max_first)

        # Ignore the four base args in the resolver, they're used in the slicing below
        resolver_args = {
            key: args[key]
            for key in args if key not in ['first', 'last', 'before', 'after']
        }
        iterable = resolver(root, info, **resolver_args)
        if iterable is None:
            iterable = []
        if type(iterable) == list:
            _len = len(iterable)
        else:
            _len = iterable.count()
        connection = connection_from_list_slice(
            iterable,
            args,
            slice_start=0,
            list_length=_len,
            list_slice_length=_len,
            connection_type=connection,
            pageinfo_type=relay.connection.PageInfo,
            edge_type=connection.Edge,
        )
        connection.iterable = iterable
        connection.count = _len
        return connection
예제 #6
0
    def default_resolver(self, _root, info, **args):
        args = args or {}

        connection_args = {
            'first': args.pop('first', None),
            'last': args.pop('last', None),
            'before': args.pop('before', None),
            'after': args.pop('after', None)
        }

        _id = args.pop('id', None)

        if _id is not None:
            iterables = [get_node_from_global_id(self.node_type, info, _id)]
            list_length = 1
        elif callable(getattr(self.model, 'objects', None)):
            iterables = self.get_queryset(self.model, info, **args)
            list_length = iterables.count()
        else:
            iterables = []
            list_length = 0

        connection = connection_from_list_slice(
            list_slice=iterables,
            args=connection_args,
            list_length=list_length,
            connection_type=self.type,
            edge_type=self.type.Edge,
            pageinfo_type=graphene.PageInfo,
        )
        connection.iterable = iterables
        connection.list_length = list_length
        return connection
예제 #7
0
    def connection_resolver(cls, resolver, connection, document, root, info,
                            **args):
        """ Returns a Graphql Connection object """

        iterable = resolver(root, info, **args)

        if iterable is None:
            iterable = cls.get_query(document, info, **args)

        if isinstance(iterable, QuerySet):
            length = iterable.count()
        else:
            length = len(iterable)

        connection = connection_from_list_slice(
            iterable,
            args,
            slice_start=0,
            list_length=length,
            list_slice_length=length,
            connection_type=connection,
            pageinfo_type=PageInfo,
            edge_type=connection.Edge,
        )
        connection.iterable = iterable
        connection.length = length

        return connection
예제 #8
0
    def resolve_connection(cls, connection, args, iterable, max_limit=None):
        iterable = maybe_queryset(iterable)

        if isinstance(iterable, QuerySet):
            list_length = iterable.count()
            list_slice_length = (min(max_limit, list_length)
                                 if max_limit is not None else list_length)
        else:
            list_length = len(iterable)
            list_slice_length = (min(max_limit, list_length)
                                 if max_limit is not None else list_length)

        after = get_offset_with_default(args.get("after"), -1) + 1

        if max_limit is not None and "first" not in args:
            args["first"] = max_limit

        connection = connection_from_list_slice(
            iterable[after:],
            args,
            slice_start=after,
            list_length=list_length,
            list_slice_length=list_slice_length,
            connection_type=connection,
            edge_type=connection.Edge,
            pageinfo_type=PageInfo,
        )
        connection.iterable = iterable
        connection.length = list_length
        return connection
예제 #9
0
 def resolve_connection(cls, connection, default_manager, args, iterable):
     if iterable is None:
         iterable = default_manager
     iterable = maybe_queryset(iterable)
     if isinstance(iterable, QuerySet):
         if iterable is not default_manager:
             default_queryset = maybe_queryset(default_manager)
             iterable = cls.merge_querysets(default_queryset, iterable)
         _len = iterable.count()
     else:
         _len = len(iterable)
     connection = connection_from_list_slice(
         iterable,
         args,
         slice_start=0,
         list_length=_len,
         list_slice_length=_len,
         connection_type=connection,
         edge_type=connection.Edge,
         pageinfo_type=PageInfo,
     )
     connection.iterable = iterable
     connection.length = _len
     connection.total_count = _len
     return connection
예제 #10
0
파일: schemas.py 프로젝트: gill876/pattoo
    def connection_resolver(self, resolver, connection, model, root, info,
                            **args):
        query = resolver(root, info, **args) or self.get_query(
            model, info, **args)

        if type(query) == AuthInfoField:
            message = query.message

            if query.message == "Invalid header padding":
                message = "Invalid Token Provided"

            raise GraphQLError(message)

        count = query.count()

        connection = connection_from_list_slice(
            query,
            args,
            slice_start=0,
            list_length=count,
            list_slice_length=count,
            connection_type=connection,
            pageinfo_type=PageInfo,
            edge_type=connection.Edge,
        )
        connection.iterable = query
        connection.length = count
        return connection
예제 #11
0
    def resolve_connection(cls, connection, args, iterable, max_limit=None):
        iterable = maybe_queryset(iterable)

        if isinstance(iterable, QuerySet):
            list_length = iterable.count()
            list_slice_length = (min(max_limit, list_length)
                                 if max_limit is not None else list_length)
        else:
            list_length = len(iterable)
            list_slice_length = (min(max_limit, list_length)
                                 if max_limit is not None else list_length)

        # If after is higher than list_length, connection_from_list_slice
        # would try to do a negative slicing which makes django throw an
        # AssertionError
        after = min(
            get_offset_with_default(args.get("after"), -1) + 1, list_length)

        if max_limit is not None and "first" not in args:
            args["first"] = max_limit

        connection = connection_from_list_slice(
            iterable[after:],
            args,
            slice_start=after,
            list_length=list_length,
            list_slice_length=list_slice_length,
            connection_type=connection,
            edge_type=connection.Edge,
            pageinfo_type=PageInfo,
        )
        connection.iterable = iterable
        connection.length = list_length
        return connection
예제 #12
0
    def default_resolver(self, _root, info, **args):
        # Copy-Paste of the parent code to intercept and filter.
        args = args or {}

        for arg, value in args.items():
            if arg.startswith("id"):
                if type(value) is str:
                    args[arg] = from_global_id(value)[1]
                if type(value) is list:
                    args[arg] = [from_global_id(id)[1] for id in value]

        if _root is not None:
            args["pk__in"] = [
                r.pk for r in getattr(_root, info.field_name, [])
            ]

        connection_args = {
            "first": args.pop("first", None),
            "last": args.pop("last", None),
            "before": args.pop("before", None),
            "after": args.pop("after", None),
        }

        _id = args.pop("id", None)

        if _id is not None:
            iterables = [get_node_from_global_id(self.node_type, info, _id)]
            list_length = 1
        elif callable(getattr(self.model, "objects", None)):
            iterables = self.get_queryset(self.model, info, **args)
            list_length = iterables.count()
        else:
            iterables = []
            list_length = 0

        # Custom Code
        iterables = list(filter(self.filter_item, iterables))

        connection = connection_from_list_slice(
            list_slice=iterables,
            args=connection_args,
            list_length=list_length,
            list_slice_length=list_length,
            connection_type=self.type,
            edge_type=self.type.Edge,
            pageinfo_type=graphene.PageInfo,
        )
        connection.iterable = iterables
        connection.list_length = list_length
        return connection
예제 #13
0
    def connection_resolver(cls, resolver, connection, default_manager,
                            max_limit, enforce_first_or_last, root, args,
                            context, info):
        first = args.get('first')
        last = args.get('last')

        if enforce_first_or_last:
            assert first or last, (
                'You must provide a `first` or `last` value to properly paginate the `{}` connection.'
            ).format(info.field_name)

        if max_limit:
            if first:
                assert first <= max_limit, (
                    'Requesting {} records on the `{}` connection exceeds the `first` limit of {} records.'
                ).format(first, info.field_name, max_limit)
                args['first'] = min(first, max_limit)

            if last:
                assert last <= max_limit, (
                    'Requesting {} records on the `{}` connection exceeds the `last` limit of {} records.'
                ).format(first, info.field_name, max_limit)
                args['last'] = min(last, max_limit)

        iterable = resolver(root, args, context, info)
        if iterable is None:
            iterable = default_manager
        iterable = maybe_queryset(iterable)
        if isinstance(iterable, QuerySet):
            if iterable is not default_manager:
                default_queryset = maybe_queryset(default_manager)
                iterable = cls.merge_querysets(default_queryset, iterable)
            _len = iterable.count()
        else:
            _len = len(iterable)
        connection = connection_from_list_slice(
            iterable,
            args,
            slice_start=0,
            list_length=_len,
            list_slice_length=_len,
            connection_type=connection,
            edge_type=connection.Edge,
            pageinfo_type=PageInfo,
        )
        connection.iterable = iterable
        connection.length = _len
        return connection
예제 #14
0
 def resolve_connection(cls, connection_type, model, info, args, resolved):
     if resolved is None:
         resolved = cls.get_query(model, info, **args)
     _len = cls.get_count(resolved, model, info, **args)
     connection = connection_from_list_slice(
         resolved,
         args,
         slice_start=0,
         list_length=_len,
         list_slice_length=_len,
         connection_type=connection_type,
         pageinfo_type=PageInfo,
         edge_type=connection_type.Edge,
     )
     connection.iterable = resolved
     connection.length = _len
     return connection
예제 #15
0
    def resolve_connection(cls, connection, args, iterable, max_limit=None):
        # Remove the offset parameter and convert it to an after cursor.
        offset = args.pop("offset", None)
        after = args.get("after")
        if offset:
            if after:
                offset += cursor_to_offset(after) + 1
            # input offset starts at 1 while the graphene offset starts at 0
            args["after"] = offset_to_cursor(offset - 1)

        iterable = maybe_queryset(iterable)

        if isinstance(iterable, QuerySet):
            list_length = iterable.count()
        else:
            list_length = len(iterable)
        list_slice_length = (min(max_limit, list_length)
                             if max_limit is not None else list_length)

        # If after is higher than list_length, connection_from_list_slice
        # would try to do a negative slicing which makes django throw an
        # AssertionError
        after = min(
            get_offset_with_default(args.get("after"), -1) + 1, list_length)

        if max_limit is not None and "first" not in args:
            if "last" in args:
                args["first"] = list_length
                list_slice_length = list_length
            else:
                args["first"] = max_limit

        connection = connection_from_list_slice(
            iterable[after:],
            args,
            slice_start=after,
            list_length=list_length,
            list_slice_length=list_slice_length,
            connection_type=connection,
            edge_type=connection.Edge,
            pageinfo_type=PageInfo,
        )
        connection.iterable = iterable
        connection.length = list_length
        return connection
예제 #16
0
 def connection_from_list(cls,
                          _list,
                          args=None,
                          connection_type=None,
                          edge_type=None,
                          pageinfo_type=None):
     _len = len(_list)
     connection = connection_from_list_slice(
         _list,
         args,
         connection_type=connection_type,
         edge_type=edge_type,
         pageinfo_type=pageinfo_type,
         slice_start=0,
         list_length=_len,
         list_slice_length=_len)
     connection.length = _len
     return connection
예제 #17
0
 def connection_resolver(resolver, connection, model, root, args, context, info):
     iterable = resolver(root, args, context, info)
     if iterable is None:
         iterable = get_query(model, context)
     if isinstance(iterable, Query):
         _len = iterable.count()
     else:
         _len = len(iterable)
     return connection_from_list_slice(
         iterable,
         args,
         slice_start=0,
         list_length=_len,
         list_slice_length=_len,
         connection_type=connection,
         pageinfo_type=PageInfo,
         edge_type=connection.Edge,
     )
예제 #18
0
 def connection_resolver(cls, resolver, connection, model, root, info, **args):
     iterable = resolver(root, info, **args)
     if not iterable:
         iterable = cls.get_query(model, info, **args)
     _len = len(iterable)
     connection = connection_from_list_slice(
         iterable,
         args,
         slice_start=0,
         list_length=_len,
         list_slice_length=_len,
         connection_type=connection,
         pageinfo_type=PageInfo,
         edge_type=connection.Edge,
     )
     connection.iterable = iterable
     connection.length = _len
     return connection
예제 #19
0
 def connection_resolver(self, resolver, connection, model, root, info,
                         **args):
     query = resolver(root, info, **args) or self.get_query(
         model, info, **args)
     count = query.count()
     connection = connection_from_list_slice(
         query,
         args,
         slice_start=0,
         list_length=count,
         list_slice_length=count,
         connection_type=connection,
         pageinfo_type=PageInfo,
         edge_type=connection.Edge,
     )
     connection.iterable = query
     connection.length = count
     return connection
예제 #20
0
    def default_resolver(self, _root, info, only_fields=list(), **args):
        args = args or {}

        if _root is not None:
            field_name = to_snake_case(info.field_name)
            if getattr(_root, field_name, []) is not None:
                args["pk__in"] = [r.id for r in getattr(_root, field_name, [])]

        connection_args = {
            "first": args.pop("first", None),
            "last": args.pop("last", None),
            "before": args.pop("before", None),
            "after": args.pop("after", None),
        }

        _id = args.pop('id', None)

        if _id is not None:
            args['pk'] = from_global_id(_id)[-1]

        if callable(getattr(self.model, "objects", None)):
            iterables = self.get_queryset(self.model, info, only_fields,
                                          **args)
            if isinstance(info, ResolveInfo):
                if not info.context:
                    info.context = Context()
                info.context.queryset = iterables
            list_length = iterables.count()
        else:
            iterables = []
            list_length = 0

        connection = connection_from_list_slice(
            list_slice=iterables,
            args=connection_args,
            list_length=list_length,
            list_slice_length=list_length,
            connection_type=self.type,
            edge_type=self.type.Edge,
            pageinfo_type=graphene.PageInfo,
        )
        connection.iterable = iterables
        connection.list_length = list_length
        return connection
예제 #21
0
    def resolve_connection(cls, connection, args, iterable):
        if isinstance(iterable, QuerySet):
            _len = iterable.count()
        else:
            _len = len(iterable)

        connection = connection_from_list_slice(
            iterable,
            args,
            slice_start=0,
            list_length=_len,
            list_slice_length=_len,
            connection_type=connection,
            edge_type=connection.Edge,
            pageinfo_type=PageInfo,
        )
        connection.iterable = iterable
        connection.length = _len
        return connection
예제 #22
0
파일: fields.py 프로젝트: insolite/graphene
 def connection_resolver(resolver, connection, model, root, args, context,
                         info):
     iterable = resolver(root, args, context, info)
     if iterable is None:
         iterable = get_query(model, context)
     if isinstance(iterable, Query):
         _len = iterable.count()
     else:
         _len = len(iterable)
     return connection_from_list_slice(
         iterable,
         args,
         slice_start=0,
         list_length=_len,
         list_slice_length=_len,
         connection_type=connection,
         pageinfo_type=PageInfo,
         edge_type=connection.Edge,
     )
예제 #23
0
파일: gql.py 프로젝트: gitzart/sns
    def connection_resolver(cls, resolver, connection, root, info, **kwargs):
        iterable = resolver(root, info, **kwargs)

        if isinstance(iterable, SQLAlchemyQuery):
            _len = iterable.count()
        else:
            _len = len(iterable)

        connection = connection_from_list_slice(
            iterable,
            kwargs,
            slice_start=0,
            list_length=_len,
            list_slice_length=_len,
            connection_type=connection,
            pageinfo_type=relay.connection.PageInfo,
            edge_type=connection.Edge)
        connection.iterable = iterable
        connection.length = _len
        return connection
예제 #24
0
 def connection_resolver(resolver, connection, default_manager, root, args, context, info):
     iterable = resolver(root, args, context, info)
     if iterable is None:
         iterable = default_manager
     iterable = maybe_queryset(iterable)
     if isinstance(iterable, QuerySet):
         _len = iterable.count()
     else:
         _len = len(iterable)
     connection = connection_from_list_slice(
         iterable,
         args,
         slice_start=0,
         list_length=_len,
         list_slice_length=_len,
         connection_type=connection,
         edge_type=connection.Edge,
         pageinfo_type=PageInfo,
     )
     connection.iterable = iterable
     connection.length = _len
     return connection
예제 #25
0
    def default_resolver(self, _root, info, **args):
        args = args or {}

        if _root is not None:
            args["pk__in"] = [
                r.pk for r in getattr(_root, info.field_name, [])
            ]

        connection_args = {
            "first": args.pop("first", None),
            "last": args.pop("last", None),
            "before": args.pop("before", None),
            "after": args.pop("after", None),
        }

        _id = args.pop("id", None)

        if _id is not None:
            iterables = [get_node_from_global_id(self.node_type, info, _id)]
            list_length = 1
        elif callable(getattr(self.model, "objects", None)):
            iterables = self.get_queryset(self.model, info, **args)
            list_length = iterables.count()
        else:
            iterables = []
            list_length = 0

        connection = connection_from_list_slice(
            list_slice=iterables,
            args=connection_args,
            list_length=list_length,
            list_slice_length=list_length,
            connection_type=self.type,
            edge_type=self.type.Edge,
            pageinfo_type=graphene.PageInfo,
        )
        connection.iterable = iterables
        connection.list_length = list_length
        return connection
예제 #26
0
 def resolve_connection(cls, connection, args, iterable, max_limit=None):
     iterable = maybe_queryset(iterable)
     # When slicing from the end, need to retrieve the iterable length.
     if args.get("last"):
         max_limit = None
     if isinstance(iterable, QuerySet):
         _len = max_limit or iterable.count()
     else:
         _len = max_limit or len(iterable)
     connection = connection_from_list_slice(
         iterable,
         args,
         slice_start=0,
         list_length=_len,
         list_slice_length=_len,
         connection_type=connection,
         edge_type=connection.Edge,
         pageinfo_type=PageInfo,
     )
     connection.iterable = iterable
     connection.length = _len
     return connection
예제 #27
0
파일: fields.py 프로젝트: mirumee/saleor
    def resolve_connection(cls, connection, default_manager, args, iterable):
        if iterable is None:
            iterable = default_manager

        if isinstance(iterable, QuerySet):
            _len = iterable.count()
        else:
            _len = len(iterable)

        connection = connection_from_list_slice(
            iterable,
            args,
            slice_start=0,
            list_length=_len,
            list_slice_length=_len,
            connection_type=connection,
            edge_type=connection.Edge,
            pageinfo_type=PageInfo,
        )
        connection.iterable = iterable
        connection.length = _len
        return connection
예제 #28
0
    def resolve_connection(cls, connection_type, args, resolved):
        if isinstance(resolved, connection_type):
            return resolved

        if not isinstance(resolved, Iterable):
            raise AssertionError(
                'Resolved value from the connection field have to be '
                'iterable or instance of {}. Received "{}"').format(
                    connection_type, type(resolved))

        if getattr(connection_type, 'max_items', None):
            total_size = args.get('first') or args.get('last')
            if not total_size or total_size > connection_type.max_items:
                raise ValueError(
                    "must specify a 'first' or 'last' parameter <= {}".format(
                        connection_type.max_items))

        if isinstance(resolved, list):
            _len = len(resolved)
        else:
            # Django QuerySet
            _len = resolved.count()

        connection = connection_from_list_slice(
            list_slice=resolved,
            args=args,
            connection_type=connection_type,
            edge_type=connection_type.Edge,
            pageinfo_type=graphene.relay.PageInfo,
            slice_start=0,
            list_length=_len,
            list_slice_length=_len,
        )
        connection.iterable = resolved
        connection._len = _len
        return connection
예제 #29
0
    def resolve_connection(cls, connection_type, model, info, args, resolved):
        if resolved is None:
            resolved = cls.get_query(model, info, **args)
        if isinstance(resolved, Query):
            _len = resolved.count()
        else:
            _len = len(resolved)

        if isinstance(resolved, set):
            resolved = list(resolved)

        connection = connection_from_list_slice(
            resolved,
            args,
            slice_start=0,
            list_length=_len,
            list_slice_length=_len,
            connection_type=connection_type,
            pageinfo_type=PageInfo,
            edge_type=connection_type.Edge,
        )
        connection.iterable = resolved
        connection.length = _len
        return connection
    def resolve_connection(cls, connection_type, args, resolved):
        if isinstance(resolved, connection_type):
            return resolved

        assert isinstance(resolved, Iterable), (
            "Resolved value from the connection field have to be iterable or instance of {}. "
            'Received "{}"').format(connection_type, resolved)

        if isinstance(resolved, QuerySet):
            _len = resolved.count()
        else:
            _len = len(resolved)
        connection = connection_from_list_slice(
            resolved,
            args,
            slice_start=0,
            list_length=_len,
            list_slice_length=_len,
            connection_type=connection_type,
            edge_type=connection_type.Edge,
            pageinfo_type=PageInfo,
        )
        connection.iterable = resolved
        return connection
예제 #31
0
def slice_connection_iterable(
    iterable,
    args,
    connection_type,
    edge_type=None,
    pageinfo_type=None,
):
    _len = len(iterable)

    slice = connection_from_list_slice(
        iterable,
        args,
        slice_start=0,
        list_length=_len,
        list_slice_length=_len,
        connection_type=connection_type,
        edge_type=edge_type or connection_type.Edge,
        pageinfo_type=pageinfo_type or graphene.relay.PageInfo,
    )

    if "total_count" in connection_type._meta.fields:
        slice.total_count = _len

    return slice