def __init__(self, type, *args, **kwargs): # SQLAlchemyConnectionField applies type checking to `type` # before a potential module string is resolved. Do that # first here. type = get_type(type) super().__init__(type, *args, **kwargs)
def get_resolver(self, parent_resolver): return partial( self.node_type.node_resolver, get_type(self.field_type), self.permission_classes, self.throttle_classes, )
def input_type(self): input_type = get_type(self._input_type) if input_type is not None: if not callable(getattr(input_type, "get_type", None)): raise ValueError( "Wrong `input_type` for {}: it only accepts graphene types, got {}" .format(self.__class__.__name__, input_type)) return input_type
class CountryType(DjangoObjectType): class Meta: model = Country last_summary = graphene.Field(SummaryType) last_contextual_update = graphene.Field(ContextualUpdateType) contacts = DjangoPaginatedListObjectField( ContactListType, pagination=PageGraphqlPagination(page_size_query_param='pageSize'), accessor='contacts') operating_contacts = DjangoPaginatedListObjectField( ContactListType, pagination=PageGraphqlPagination(page_size_query_param='pageSize'), accessor='operating_contacts') contextual_updates = DjangoPaginatedListObjectField( ContextualUpdateListType, pagination=PageGraphqlPagination(page_size_query_param='pageSize'), accessor='contextual_updates') summaries = DjangoPaginatedListObjectField( SummaryListType, pagination=PageGraphqlPagination(page_size_query_param='pageSize'), accessor='summaries') crises = graphene.Dynamic(lambda: DjangoPaginatedListObjectField( get_type('apps.crisis.schema.CrisisListType'), pagination=PageGraphqlPagination(page_size_query_param='pageSize'), accessor='crises')) entries = graphene.Dynamic(lambda: DjangoPaginatedListObjectField( get_type('apps.entry.schema.EntryListType'), pagination=PageGraphqlPagination(page_size_query_param='pageSize'), accessor='entries')) @staticmethod def get_queryset(queryset, info): # graphene_django/fields.py:57 demands we implement this method # so that we can filter based on request, but we do not need return queryset
def type(self): _type = get_type(self._type) type_name = str(_type) stored_type = _all_node_connection_types.get(type_name, None) if stored_type: return stored_type else: class NodeConnection(Connection): total_count = Int() class Meta: node = _type name = '{}FilterConnection'.format(_type._meta.name) def resolve_total_count(self, info, **kwargs): return self.iterable.count() _all_node_connection_types[type_name] = NodeConnection return NodeConnection
def is_filterable(k): """ Remove complex columns from input args at this moment. Args: k (str): field name. Returns: bool """ if hasattr(self.fields[k].type, '_sdl'): return False if not hasattr(self.model, k): return False if isinstance(getattr(self.model, k), property): return False try: converted = convert_mongoengine_field(getattr(self.model, k), self.registry) except MongoEngineConversionError: return False if isinstance(converted, (ConnectionField, Dynamic)): return False if callable(getattr(converted, "type", None)) and isinstance( converted.type(), ( FileFieldType, PointFieldType, MultiPolygonFieldType, graphene.Union, PolygonFieldType, ), ): return False if getattr(converted, "type", None) and getattr( converted.type, "_of_type", None) and issubclass( (get_type(converted.type.of_type)), graphene.Union): return False if isinstance(converted, (graphene.List)) and issubclass( getattr(converted, "_of_type", None), graphene.Union): return False return True
def type(self): return get_type(self._type)
def get_id(self, root, info, **kwargs): only_type = get_type(self.field_type) return self.node_type.node_resolver(only_type, root, info, **kwargs)
def fields(self): self._type = get_type(self._type) return self._type._meta.fields
from django.contrib.auth import get_user_model import graphene from graphene import Field from graphene.types.utils import get_type # from graphene_django import DjangoObjectType from graphene_django_extras import PageGraphqlPagination, DjangoObjectType from utils.fields import DjangoPaginatedListObjectField, CustomDjangoListObjectType from apps.users.filters import UserFilter User = get_user_model() EntryListType = get_type('apps.entry.schema.EntryListType') class UserType(DjangoObjectType): class Meta: model = User exclude_fields = ('password',) review_entries = DjangoPaginatedListObjectField(EntryListType, pagination=PageGraphqlPagination( page_size_query_param='pageSize' ), accessor='review_entries') created_entry = DjangoPaginatedListObjectField(EntryListType, pagination=PageGraphqlPagination( page_size_query_param='pageSize' ), accessor='created_entry') role = Field(graphene.String)