def list_resolver(self, resolver, manager, filterset_class, filtering_args, root, info, **kwargs): qs_resolve_override = True qs = maybe_queryset(resolver(root, info, **kwargs)) if qs is None: qs = self.get_queryset(manager) qs_resolve_override = False if not self.skip_filters: filter_kwargs = { k: v for k, v in kwargs.items() if k in filtering_args } qs = filterset_class(data=filter_kwargs, queryset=qs, request=info.context).qs if not qs_resolve_override and root and is_valid_django_model( root._meta.model): extra_filters = get_extra_filters(root, manager.model) qs = qs.filter(**extra_filters) if getattr(self, "pagination", None): qs = self.pagination.paginate_queryset(qs, **kwargs) if not qs_resolve_override: qs = self.refactor_query(qs, info, fragments=info.fragments, **kwargs) return maybe_queryset(qs)
def list_resolver(django_object_type, resolver, root, info, **args): queryset = maybe_queryset(resolver(root, info, **args)) if queryset is None: queryset = QuerySet.none() # FIXME: This will throw error if isinstance(queryset, QuerySet): if hasattr(django_object_type, 'get_queryset'): # Pass queryset to the DjangoObjectType get_queryset method queryset = maybe_queryset( django_object_type.get_queryset(queryset, info)) return queryset
def list_resolver(django_object_type, resolver, default_queryset, root, info, **args): queryset = maybe_queryset(resolver(root, info, **args)) if queryset is None: queryset = default_queryset if isinstance(queryset, QuerySet): if hasattr(django_object_type, 'get_queryset'): # Pass queryset to the DjangoObjectType get_queryset method queryset = maybe_queryset( django_object_type.get_queryset(queryset, info)) return queryset
def resolve_connection(cls, connection, args, iterable): iterable = maybe_queryset(iterable) if isinstance(iterable, QuerySet): # only query count on database when pagination is needed # resolve_connection may be removed again once following issue is fixed: # https://github.com/graphql-python/graphene-django/issues/177 if "before" in args or "after" in args or "first" in args or "last" in args: _len = iterable.count() else: _len = len(iterable) else: # pragma: no cover _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
def list_resolver(self, manager, filterset_class, filtering_args, root, info, **kwargs): if self.fetch_fn is not None: qs = self.fetch_fn(info.context) elif self.property_name is not None and root and is_valid_django_model( root._meta.model): qs = getattr(root, self.property_name) else: qs = queryset_factory(manager, info.field_asts, info.fragments, **kwargs) filter_kwargs = { k: v for k, v in kwargs.items() if k in filtering_args } qs = filterset_class(data=filter_kwargs, queryset=qs, request=info.context).qs if root and is_valid_django_model(root._meta.model): extra_filters = get_extra_filters(root, manager.model) qs = qs.filter(**extra_filters) count = qs.count() results = maybe_queryset(qs) return DjangoListObjectBase( count=count, results=results, results_field_name=self.type._meta.results_field_name, )
def list_resolver(manager, filterset_class, filtering_args, root, info, **kwargs): qs = None available_related_fields = get_related_fields(root._meta.model) field = find_field(info.field_asts[0], available_related_fields) filter_kwargs = { k: v for k, v in kwargs.items() if k in filtering_args } if field: try: if filter_kwargs: qs = operator.attrgetter('{}.filter'.format( getattr(field, 'related_name', None) or field.name))(root)(**filter_kwargs) else: qs = operator.attrgetter('{}.all'.format( getattr(field, 'related_name', None) or field.name))(root)() except AttributeError: qs = None if qs is None: qs = queryset_factory(manager, info.field_asts, info.fragments, **kwargs) qs = filterset_class(data=filter_kwargs, queryset=qs).qs if root and is_valid_django_model(root._meta.model): extra_filters = get_extra_filters(root, manager.model) qs = qs.filter(**extra_filters) return maybe_queryset(qs)
def resolve_connection(cls, connection, args, iterable, max_limit=None): iterable = maybe_queryset(iterable) if isinstance(iterable, QuerySet): # only query count on database when pagination is needed # resolve_connection may be removed again once following issue is fixed: # https://github.com/graphql-python/graphene-django/issues/177 if all( args.get(x) is None for x in ["before", "after", "first", "last"]): _len = len(iterable) else: _len = iterable.count() else: # pragma: no cover _len = len(iterable) # 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, _len) if max_limit is not None and "first" not in args: # pragma: no cover args["first"] = max_limit connection = connection_from_list_slice( iterable[after:], 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
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
def list_resolver(resolver, default_manager, filterset_class, filtering_args, root, info, **args): filter_kwargs = {k: v for k, v in args.items() if k in filtering_args} qs = filterset_class( data=filter_kwargs, queryset=resolver(root, info, **args), request=info.context, ).qs return maybe_queryset(qs)
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): # only query count on database when pagination is needed # resolve_connection may be removed again once following issue is fixed: # https://github.com/graphql-python/graphene-django/issues/177 if all( args.get(pagination_arg) is None for pagination_arg in ["before", "after", "first", "last"] ): list_length = len(iterable) else: list_length = iterable.count() else: # pragma: no cover 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_array_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) # This is commented out because we disable this functionality in our # settings explicitly. # if max_limit is not None and args.get("first", None) is None: # if args.get("last", None) is not None: # after = list_length - args["last"] # else: # args["first"] = max_limit connection = connection_from_array_slice( iterable[after:], args, slice_start=after, array_length=list_length, array_slice_length=list_slice_length, connection_type=connection, edge_type=connection.Edge, page_info_type=PageInfo, ) connection.iterable = iterable connection.length = list_length return connection
def list_resolver(self, resolver, manager, filterset_class, filtering_args, root, info, **kwargs): qs_resolve_override, count = True, 0 qs = maybe_queryset(resolver(root, info, **kwargs)) if qs is None: qs = self.get_queryset(manager) qs_resolve_override = False if not self.skip_filters: filter_kwargs = {k: v for k, v in kwargs.items() if k in filtering_args} qs = filterset_class(data=filter_kwargs, queryset=qs, request=info.context).qs count = qs.count() if not qs_resolve_override: qs = self.refactor_query(qs, info, fragments=info.fragments, **kwargs) return DjangoListObjectBase( count=count, results=maybe_queryset(qs), results_field_name=self.type._meta.results_field_name, )
def list_resolver(self, manager, filterset_class, filtering_args, root, info, **kwargs): qs = queryset_factory(manager, info.field_asts, info.fragments, **kwargs) filter_kwargs = {k: v for k, v in kwargs.items() if k in filtering_args} qs = filterset_class(data=filter_kwargs, queryset=qs, request=info.context).qs count = qs.count() return DjangoListObjectBase( count=count, results=maybe_queryset(qs), results_field_name=self.type._meta.results_field_name )
def resolve_connection(cls, connection, args, iterable): try: iterable = maybe_queryset(iterable) connection = connection_from_queryset(iterable, args, connection_type=connection, edge_type=connection.Edge, pageinfo_type=PageInfo) # TODO: DjangoConnectionFieldでは下記を代入してるけど必要なのか? # connection.iterable = iterable # connection.length = iterable.count() return connection except Exception as e: logger.exception(e)
def list_resolver(self, manager, filterset_class, filtering_args, root, info, **kwargs): filter_kwargs = {k: v for k, v in kwargs.items() if k in filtering_args} qs = self.get_queryset(manager, info, **kwargs) qs = filterset_class(data=filter_kwargs, queryset=qs, request=info.context).qs if root and is_valid_django_model(root._meta.model): extra_filters = get_extra_filters(root, manager.model) qs = qs.filter(**extra_filters) if getattr(self, 'pagination', None): qs = self.pagination.paginate_queryset(qs, **kwargs) return maybe_queryset(qs)
def list_resolver(self, manager, filterset_class, filtering_args, root, info, **kwargs): filter_kwargs = { k: v for k, v in kwargs.items() if k in filtering_args } if self.accessor: qs = getattr(root, self.accessor) if hasattr(qs, 'all'): qs = qs.all() qs = filterset_class(data=filter_kwargs, queryset=qs, request=info.context).qs else: qs = self.get_queryset(manager, root, info, **kwargs) qs = filterset_class(data=filter_kwargs, queryset=qs, request=info.context).qs if root and is_valid_django_model(root._meta.model): extra_filters = get_extra_filters(root, manager.model) qs = qs.filter(**extra_filters) # TODO: Duplicate count (self.pagination.paginate_queryset also calls count) count = qs.count() if getattr(self, "pagination", None): ordering = kwargs.pop(self.pagination.ordering_param, None) or self.pagination.ordering if type(self.pagination) == NoOrderingPageGraphqlPagination: # This is handled in filterset kwargs[self.pagination.ordering_param] = None else: ordering = ','.join([ to_snake_case(each) for each in ordering.strip(',').replace(' ', '').split(',') ]) kwargs[self.pagination.ordering_param] = ordering 'pageSize' in kwargs and kwargs['pageSize'] is None and kwargs.pop( 'pageSize') qs = self.pagination.paginate_queryset(qs, **kwargs) return CustomDjangoListObjectBase( count=count, results=maybe_queryset(qs), results_field_name=self.type._meta.results_field_name, page=kwargs.get('page', 1) if hasattr(self.pagination, 'page_query_param') else None, pageSize=kwargs. get( # TODO: Need to add cutoff to send max page size instead of requested 'pageSize', graphql_api_settings.DEFAULT_PAGE_SIZE) if hasattr( self.pagination, 'page_size_query_param') else None)
def wrap(*args, **kwargs): info = next(i for i in args if i and isinstance(i, graphene.ResolveInfo)) assert info is not None, "graphene.ResolveInfo is required in `permission_required`" wrap_permission_mixin.check_permissions(info.context) result = f(*args, **kwargs) qs = maybe_queryset(result) if isinstance(qs, QuerySet) and qs.exists(): wrap_permission_mixin.check_object_permissions( info.context, qs.first()) elif isinstance(qs, (list, tuple)) and len(qs) > 0: wrap_permission_mixin.check_object_permissions( info.context, qs[0]) return result
def list(cls, manager, filterset_class, filtering_args, root, info, **kwargs): qs = queryset_factory(cls._meta.queryset or manager, info.field_asts, info.fragments, **kwargs) filter_kwargs = {k: v for k, v in kwargs.items() if k in filtering_args} qs = filterset_class(data=filter_kwargs, queryset=qs).qs count = qs.count() return DjangoListObjectBase( count=count, results=maybe_queryset(qs), results_field_name=cls.list_object_type()._meta.results_field_name )
def get_queryset(self, root, field_name, field_asts, fragments, **kwargs): prefetched = get_prefetched_attr(root, field_name) if prefetched: return prefetched filter_kwargs = { k: v for k, v in kwargs.items() if k in self.filtering_args } qs = queryset_factory(self.type.of_type, field_asts, fragments, **kwargs) qs = self.filterset_class(data=filter_kwargs, queryset=qs).qs return maybe_queryset(qs)
def list_resolver(self, manager, filterset_class, filtering_args, root, info, **kwargs): filter_kwargs = {k: v for k, v in kwargs.items() if k in filtering_args} qs = queryset_factory(manager, info.field_asts, info.fragments, **kwargs) qs = filterset_class(data=filter_kwargs, queryset=qs, request=info.context).qs if root and is_valid_django_model(root._meta.model): extra_filters = get_extra_filters(root, manager.model) qs = qs.filter(**extra_filters) if getattr(self, 'pagination', None): qs = self.pagination.paginate_queryset(qs, **kwargs) return maybe_queryset(qs)
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: # pragma: no cover _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
def list_resolver(self, manager, filterset_class, filtering_args, root, info, **kwargs): qs = self.get_queryset(root, info.field_name, info.field_asts, info.fragments, **kwargs) # If we've prefetched the queryset, it will be a list object and we don't need to appy the extra filters if not isinstance(qs, list) and root and is_valid_django_model( root._meta.model): extra_filters = get_extra_filters(root, manager.model) qs = qs.filter(**extra_filters) if getattr(self, 'pagination', None): qs = self.pagination.paginate_queryset(qs, **kwargs) return maybe_queryset(qs)
def list_resolver(self, manager, filterset_class, filtering_args, root, info, **kwargs): qs = self.get_queryset(root, info.field_name, info.field_asts, info.fragments, **kwargs) # If we've prefetched the queryset, it will be a list object and we don't need to appy the extra filters if not isinstance(qs, list) and root and is_valid_django_model( root._meta.model): extra_filters = get_extra_filters(root, manager.model) qs = qs.filter(**extra_filters) count = _get_count(qs) return DjangoListObjectBase( count=count, results=maybe_queryset(qs), results_field_name=self.type._meta.results_field_name)
def get_queryset(self, root, field_name, field_asts, fragments, **kwargs): prefetched = get_prefetched_attr(root, to_snake_case(field_name)) if prefetched: return prefetched filter_kwargs = { k: v for k, v in kwargs.items() if k in self.filtering_args } qs = queryset_factory( registry.get_type_for_model(self.type._meta.model), field_asts, fragments, **kwargs) qs = self.filterset_class(data=filter_kwargs, queryset=qs).qs if self.post_optimize: qs = self.post_optimize(qs, **kwargs) return maybe_queryset(qs)
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 args.get("first", None) is None: if args.get("last", None) is not None: after = list_length - args["last"] 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
def list_resolver(resolver, default_manager, filterset_class, filtering_args, root, info, **args): """ :param resolver: :param default_manager: Beware! this field is actually needed. GraphQl will fail if removed! :param filterset_class: :param filtering_args: :param root: :param info: :param args: :return: """ filter_kwargs = {k: v for k, v in args.items() if k in filtering_args} qs = filterset_class( data=filter_kwargs, queryset=resolver(root, info, **args), request=info.context, ).qs return maybe_queryset(qs)
def list_resolver(self, manager, filterset_class, filtering_args, root, info, **kwargs): filter_kwargs = { k: v for k, v in kwargs.items() if k in filtering_args } if self.accessor: qs = getattr(root, self.accessor).all() qs = filterset_class(data=filter_kwargs, queryset=qs, request=info.context).qs else: qs = self.get_queryset(manager, info, **kwargs) qs = filterset_class(data=filter_kwargs, queryset=qs, request=info.context).qs if root and is_valid_django_model(root._meta.model): extra_filters = get_extra_filters(root, manager.model) qs = qs.filter(**extra_filters) count = qs.count() if getattr(self, "pagination", None): ordering = kwargs.pop(self.pagination.ordering_param, None) or self.pagination.ordering ordering = ','.join([ to_snake_case(each) for each in ordering.strip(',').replace(' ', '').split(',') ]) self.pagination.ordering = ordering qs = self.pagination.paginate_queryset(qs, **kwargs) return CustomDjangoListObjectBase( count=count, results=maybe_queryset(qs), results_field_name=self.type._meta.results_field_name, page=kwargs.get('page', 1) if hasattr(self.pagination, 'page') else None, pageSize=kwargs.get( 'pageSize', graphql_api_settings.DEFAULT_PAGE_SIZE) if hasattr( self.pagination, 'page') else None)
def list_resolver(self, filterset_class, filtering_args, root, info, **kwargs): filter_kwargs = { k: v for k, v in kwargs.items() if k in filtering_args } qs = getattr(root, self.accessor) if hasattr(qs, 'all'): qs = qs.all() qs = filterset_class(data=filter_kwargs, queryset=qs, request=info.context).qs count = qs.count() if getattr(self, "pagination", None): ordering = kwargs.pop(self.pagination.ordering_param, None) or self.pagination.ordering ordering = ','.join([ to_snake_case(each) for each in ordering.strip(',').replace(' ', '').split(',') ]) 'pageSize' in kwargs and kwargs['pageSize'] is None and kwargs.pop( 'pageSize') kwargs[self.pagination.ordering_param] = ordering qs = self.pagination.paginate_queryset(qs, **kwargs) return CustomDjangoListObjectBase( count=count, results=maybe_queryset(qs), results_field_name=self.type._meta.results_field_name, page=kwargs.get('page', 1) if hasattr(self.pagination, 'page') else None, pageSize=kwargs. get( # TODO: Need to add cutoff to send max page size instead of requested 'pageSize', graphql_api_settings.DEFAULT_PAGE_SIZE) if hasattr( self.pagination, 'page') else None)
def list_resolver(manager, filterset_class, filtering_args, root, info, **kwargs): qs = None field = None if root and is_valid_django_model(root._meta.model): available_related_fields = get_related_fields(root._meta.model) field = find_field( info.field_asts[0], available_related_fields ) filter_kwargs = { k: v for k, v in kwargs.items() if k in filtering_args } if field is not None: try: if filter_kwargs: qs = operator.attrgetter( '{}.filter'.format( getattr(field, 'related_name', None) or field.name) )(root)(**filter_kwargs) else: qs = operator.attrgetter( '{}.all'.format( getattr(field, 'related_name', None) or field.name) )(root)() except AttributeError: qs = None if qs is None: qs = queryset_factory(manager, info.field_asts, info.fragments, **kwargs) qs = filterset_class(data=filter_kwargs, queryset=qs).qs if root and is_valid_django_model(root._meta.model): extra_filters = get_extra_filters(root, manager.model) qs = qs.filter(**extra_filters) return maybe_queryset(qs)
def list_resolver(self, resolver, manager, filterset_class, filtering_args, root, info, **kwargs): qs = None field = None if root and is_valid_django_model(root._meta.model): available_related_fields = get_related_fields(root._meta.model) field = find_field(info.field_nodes[0], available_related_fields) filter_kwargs = {k: v for k, v in kwargs.items() if k in filtering_args} if field is not None: try: if filter_kwargs: qs = operator.attrgetter( "{}.filter".format( getattr(field, "related_name", None) or field.name ) )(root)(**filter_kwargs) else: qs = operator.attrgetter( "{}.all".format( getattr(field, "related_name", None) or field.name ) )(root)() except AttributeError: qs = None if qs is None: qs = self.get_queryset(manager) qs = filterset_class( data=filter_kwargs, queryset=qs, request=info.context ).qs if root and is_valid_django_model(root._meta.model): extra_filters = get_extra_filters(root, manager.model) qs = qs.filter(**extra_filters) qs = self.refactor_query(qs, info, fragments=info.fragments, **kwargs) return maybe_queryset(qs)