def filter_argument_factory(type_name: str, filtering_args: Dict): if type_name in _filter_type_registry: return _filter_type_registry[type_name] fields = {} fields.update(filtering_args) fields['__or'] = Dynamic(lambda: List(of_type=FilterType).Argument()) fields['__and'] = Dynamic(lambda: List(of_type=FilterType).Argument()) FilterType = type(type_name, (graphene.InputObjectType, ), fields) filter_type_argument = FilterType().Argument() _filter_type_registry[type_name] = filter_type_argument return filter_type_argument
def convert_field_to_id( field, registry=None, required=None, field_many_to_many_extras=None, field_foreign_key_extras=None, field_one_to_one_extras=None, ): # Call getattr here, as OneToOneRel does not carry the attribute whatsoeever. id_type = ID( description=getattr(field, "help_text", ""), required=is_required(field, required), ) _type_name = (field_foreign_key_extras or {}).get("type", "ID") if _type_name == "ID": return id_type # Use the Input type node from registry in a dynamic type, and create a union with that # and the ID def dynamic_type(): _type = registry.get_converted_field(_type_name) if not _type: raise GraphQLError(f"The type {_type_name} does not exist.") return InputField(_type, description=field.help_text, required=is_required(field, required)) return Dynamic(dynamic_type)
def convert_one_to_one_field( field, registry=None, required=None, field_many_to_many_extras=None, field_foreign_key_extras=None, field_one_to_one_extras=None, ): type_name = (field_one_to_one_extras.get("type", "ID") if field_one_to_one_extras else "ID") if type_name == "ID": return ID( description=getattr(field, "help_text", ""), required=is_required(field, required), ) def dynamic_type(): _type = registry.get_converted_field(type_name) if not _type: raise GraphQLError(f"The type {type_name} does not exist.") return InputField( _type, description=getattr(field, "help_text", ""), required=is_required(field, required), ) return Dynamic(dynamic_type)
def convert_sqlalchemy_relationship(relationship_prop, registry, connection_field_factory, resolver, **field_kwargs): direction = relationship_prop.direction model = relationship_prop.mapper.entity def dynamic_type(): _type = registry.get_type_for_model(model) if not _type: return None if direction == interfaces.MANYTOONE or not relationship_prop.uselist: return Field( _type, resolver=resolver, **field_kwargs ) elif direction in (interfaces.ONETOMANY, interfaces.MANYTOMANY): if _type._meta.connection: # TODO Add a way to override connection_field_factory return connection_field_factory(relationship_prop, registry, **field_kwargs) return Field( List(_type), **field_kwargs ) return Dynamic(dynamic_type)
def convert_sqlalchemy_relationship(relationship_prop, obj_type, connection_field_factory, batching, orm_field_name, **field_kwargs): """ :param sqlalchemy.RelationshipProperty relationship_prop: :param SQLAlchemyObjectType obj_type: :param function|None connection_field_factory: :param bool batching: :param str orm_field_name: :param dict field_kwargs: :rtype: Dynamic """ def dynamic_type(): """:rtype: Field|None""" direction = relationship_prop.direction child_type = obj_type._meta.registry.get_type_for_model(relationship_prop.mapper.entity) batching_ = batching if is_selectin_available else False if not child_type: return None if direction == interfaces.MANYTOONE or not relationship_prop.uselist: return _convert_o2o_or_m2o_relationship(relationship_prop, obj_type, batching_, orm_field_name, **field_kwargs) if direction in (interfaces.ONETOMANY, interfaces.MANYTOMANY): return _convert_o2m_or_m2m_relationship(relationship_prop, obj_type, batching_, connection_field_factory, **field_kwargs) return Dynamic(dynamic_type)
def convert_field_to_list_or_connection(field, registry=None): model = field.related_model def dynamic_type(): _type = registry.get_type_for_model(model) if not _type: return description = (field.help_text if isinstance( field, models.ManyToManyField) else field.field.help_text) # If there is a connection, we should transform the field # into a DjangoConnectionField if _type._meta.connection: # Use a DjangoFilterConnectionField if there are # defined filter_fields or a filterset_class in the # DjangoObjectType Meta if _type._meta.filter_fields or _type._meta.filterset_class: from .filter.fields import DjangoFilterConnectionField return DjangoFilterConnectionField(_type, required=True, description=description) return DjangoConnectionField(_type, required=True, description=description) return DjangoListField( _type, required=True, # A Set is always returned, never None. description=description, ) return Dynamic(dynamic_type)
def convert_field_to_djangomodel( field, registry=None, input_flag=None, nested_field=False ): model = get_related_model(field) def dynamic_type(): # Avoid create field for auto generate OneToOneField product of an inheritance if isinstance(field, models.OneToOneField) and issubclass( field.model, field.related_model ): return if input_flag and not nested_field: return ID( description=field.help_text or field.verbose_name, required=is_required(field) and input_flag == "create", ) _type = registry.get_type_for_model(model, for_input=input_flag) if not _type: return return Field( _type, description=field.help_text or field.verbose_name, required=is_required(field) and input_flag == "create", ) return Dynamic(dynamic_type)
def convert_field_to_id(field, registry=None, required=None, field_many_to_many_extras=None, field_foreign_key_extras=None): id_type = ID(description=field.help_text, required=is_required(field, required)) _type_name = (field_foreign_key_extras or {}).get('type', "ID") if _type_name == "ID": return id_type # Use the Input type node from registry in a dynamic type, and create a union with that # and the ID def dynamic_type(): _type = registry.get_converted_field(_type_name) if not _type: raise GraphQLError(f"The type {_type_name} does not exist.") return InputField(_type, description=field.help_text, required=is_required(field, required)) return Dynamic(dynamic_type)
def convert_many_rel_to_djangomodel( field, registry=None, input_flag=None, nested_field=False ): model = field.related_model def dynamic_type(): if input_flag and not nested_field: return DjangoListField(ID) else: _type = registry.get_type_for_model(model, for_input=input_flag) if not _type: return elif input_flag and nested_field: return DjangoListField(_type) elif _type._meta.filter_fields or _type._meta.filterset_class: return DjangoFilterListField( _type, required=is_required(field) and input_flag == "create", filterset_class=_type._meta.filterset_class, ) else: return DjangoListField( _type, required=is_required(field) and input_flag == "create" ) return Dynamic(dynamic_type)
def convert_field_to_list_or_connection(field, registry=None, input_flag=None, nested_fields=False): model = get_related_model(field) def dynamic_type(): if input_flag and not nested_fields: return DjangoListField(ID, required=is_required(field) and input_flag == 'create') _type = registry.get_type_for_model(model, for_input=input_flag) if not _type: return if _type._meta.filter_fields and input_flag: return DjangoFilterListField(_type, required=is_required(field) and input_flag == 'create') return DjangoListField(_type, required=is_required(field) and input_flag == 'create') return Dynamic(dynamic_type)
def convert_field_to_list_or_connection( field, registry=None, input_flag=None, nested_field=False ): model = get_related_model(field) def dynamic_type(): if input_flag and not nested_field: return DjangoListField( ID, required=is_required(field) and input_flag == "create", description=field.help_text or field.verbose_name, ) else: _type = registry.get_type_for_model(model, for_input=input_flag) if not _type: return elif input_flag and nested_field: return DjangoListField(_type) elif _type._meta.filter_fields or _type._meta.filterset_class: return DjangoFilterListField( _type, required=is_required(field) and input_flag == "create", description=field.help_text or field.verbose_name, filterset_class=_type._meta.filterset_class, ) else: return DjangoListField( _type, required=is_required(field) and input_flag == "create", description=field.help_text or field.verbose_name, ) return Dynamic(dynamic_type)
def convert_many_to_many_field(field, registry=None, required=None, field_many_to_many_extras=None, field_foreign_key_extras=None): # Use getattr on help_text here as ManyToOnRel does not possess this. list_id_type = List(ID, description=getattr(field, 'help_text', ''), required=is_required(field, required, True)) _type_name = (field_many_to_many_extras or {}).get('type', "ID") if _type_name == "ID": return list_id_type # Use the Input type node from registry in a dynamic type, and create a union with that # and the ID def dynamic_type(): _type = registry.get_converted_field(_type_name) if not _type: raise GraphQLError(f"The type {_type_name} does not exist.") return List(_type, description=getattr(field, 'help_text', ''), required=is_required(field, required, True)) return Dynamic(dynamic_type)
def convert_ForeignKey_field_to_djangomodel(field, registry=None, input_flag=None): model = get_related_model(field) def dynamic_type(): _type = registry.get_type_for_model(model, for_input=input_flag) if not _type: return # Avoid create field for auto generate OneToOneField product of an inheritance if isinstance(field, models.OneToOneField) and issubclass( field.model, field.related_model): return if input_flag: if input_flag == "order_by": return graphene.Field( convert_model_to_input_type(model, input_flag="order_by", registry=registry), description=field.help_text or field.verbose_name, ) elif input_flag == "where": return graphene.Field( convert_model_to_input_type(model, input_flag="where", registry=registry), description=field.help_text or field.verbose_name, ) elif input_flag == "create": return graphene.Field( convert_model_to_input_type( model, input_flag="create_nested", registry=registry, exclude=[field.remote_field.name], ), description=field.help_text or field.verbose_name, ) elif input_flag == "update": return graphene.Field( convert_model_to_input_type( model, input_flag="update_nested", registry=registry, exclude=[field.remote_field.name], ), description=field.help_text or field.verbose_name, ) return Field( _type, description=field.help_text or field.verbose_name, ) return Dynamic(dynamic_type)
def convert_field_to_list_or_connection(field, registry=None): model = field.related_model def dynamic_type(): _type = registry.get_query_by_model(model) if not _type: return return List(_type, **get_filters(_type._schema)) return Dynamic(dynamic_type)
def convert_field_to_dynamic(field, registry=None): model = field.document_type def dynamic_type(): _type = registry.get_type_for_model(model) if not _type: return None return Field(_type) return Dynamic(dynamic_type)
def convert_onetoone_field_to_djangomodel(field, registry=None): model = field.related_model def dynamic_type(): _type = registry.get_type_for_model(model) if not _type: return return PermissionedTypeField(_type, required=not null) return Dynamic(dynamic_type)
def convert_many_rel_djangomodel(field, registry=None, input_flag=None): model = field.related_model if input_flag == "order_by": return def dynamic_type(): _type = registry.get_type_for_model(model) if not _type: return if input_flag: if input_flag == "where": return graphene.Field( convert_model_to_input_type(model, input_flag="where", registry=registry), ) elif input_flag == "create": return graphene.Field( convert_model_to_input_type( model, input_flag="create_nested_many", registry=registry), ) elif input_flag == "update": return graphene.Field( convert_model_to_input_type( model, input_flag="update_nested_many", registry=registry), ) else: args = dict() args.update({ "where": graphene.Argument( convert_model_to_input_type(model, input_flag="where", registry=registry)), "order_by": graphene.List( convert_model_to_input_type(model, input_flag="order_by", registry=registry)), }) if _type._meta.connection: return DjangoConnectionField( _type, **args, ) return DjangoListField( _type, **args, ) return Dynamic(dynamic_type)
def convert_onetoone_field_to_djangomodel(field, registry=None): model = get_related_model(field) def dynamic_type(): _type = registry.get_type_for_model(model) if not _type: return return Field(_type) return Dynamic(dynamic_type)
def convert_field_to_djangomodel(field, registry=None): model = field.related_model def dynamic_type(): _type = registry.get_type_for_model(model) if not _type: return return Field(_type, description=field.help_text, required=not field.null) return Dynamic(dynamic_type)
def convert_generic_relation_to_object_list(field, registry=None, input_flag=None, nested_field=False): model = field.related_model def dynamic_type(): if input_flag: return _type = registry.get_type(model) if not _type: return return DjangoListField(_type) return Dynamic(dynamic_type)
def convert_field_to_object(field, registry=None): """ Converts Mongoengine fields to Graphene Object type """ field_data = get_data_from_field(field) def type_factory(): """ Lazy type factory """ doc_type = registry.get_type_for_document(field.document_type) if not doc_type: return None return Field(doc_type, **field_data) return Dynamic(type_factory)
def convert_onetoone_field_to_djangomodel(field, registry=None, input_flag=None, nested_field=False): model = field.related_model def dynamic_type(): if input_flag and not nested_field: return ID() _type = registry.get_type_for_model(model, for_input=input_flag) if not _type: return return Field( _type, required=is_required(field) and input_flag == 'create' ) return Dynamic(dynamic_type)
def convert_field_to_list_or_connection(field, registry=None): model = get_related_model(field) def dynamic_type(): _type = registry.get_type_for_model(model) if not _type: return if is_node(_type): return get_connection_field(_type) return Field(List(_type)) return Dynamic(dynamic_type)
def convert_relationship(relationship_prop, registry, connection_field_factory, resolver, **field_kwargs): def dynamic_type(): _type = registry.get_type_for_model(relationship_prop.other) if relationship_prop.one: return Field(_type, resolver=resolver, **field_kwargs) else: if _type._meta.connection: # TODO Add a way to override connection_field_factory return connection_field_factory(relationship_prop, registry, **field_kwargs) return Field(List(_type), **field_kwargs) return Dynamic(dynamic_type)
def convert_onetoone_field_to_djangomodel(field, registry=None): model = field.related_model def dynamic_type(): _type = registry.get_type_for_model(model) if not _type: return # We do this for a bug in Django 1.8, where null attr # is not available in the OneToOneRel instance null = getattr(field, "null", True) return Field(_type, required=not null) return Dynamic(dynamic_type)
def convert_field_to_list_or_connection(field, registry=None): model = field.related_model def dynamic_type(): _type = registry.get_type_for_model(model) if not _type: return assert hasattr( _type, "permission_class" ), "Attempt to connect to type which does not have a permission_class. Ensure it does by sub-classing `graphene_django_plus.types.PermissionedType`" return create_permissioned_connection_field_for_type(_type) # return DjangoConnectionField(_type) return Dynamic(dynamic_type)
def convert_relationship_to_dynamic(type, attribute, registry=None): def dynamic_type(): _type = registry.get_type_for_model(attribute.model) if not _type: return None if isinstance(attribute, OneToOne): return Field(_type) if isinstance(attribute, OneToMany): if _type._meta.connection: return PynamoConnectionField(_type) return Field(List(_type)) return Dynamic(dynamic_type)
def convert_lazy_field_to_dynamic(field, registry=None): model = field.document_type def lazy_resolver(root, *args, **kwargs): if getattr(root, field.name or field.db_name): return getattr(root, field.name or field.db_name).fetch() def dynamic_type(): _type = registry.get_type_for_model(model) if not _type: return None return Field(_type, resolver=lazy_resolver, description=get_field_description(field, registry)) return Dynamic(dynamic_type)
def convert_sqlalchemy_relationship(relationship, registry): direction = relationship.direction model = relationship.mapper.entity def dynamic_type(): _type = registry.get_type_for_model(model) if not _type: return None if direction == interfaces.MANYTOONE or not relationship.uselist: return Field(_type) elif direction in (interfaces.ONETOMANY, interfaces.MANYTOMANY): if _type._meta.connection: return createConnectionField(_type._meta.connection) return Field(List(_type)) return Dynamic(dynamic_type)
def convert_relatedfield_to_djangomodel(field, registry=None): model = field.model def dynamic_type(): _type = registry.get_type_for_model(model) if not _type: return if isinstance(field.field, models.OneToOneField): return Field(_type) if is_node(_type): return get_connection_field(_type) return DjangoListField(_type) return Dynamic(dynamic_type)