async def resolve_user_items(info: ResolveInfo, session: Session, **kwargs) -> ItemNode:
        auth_service = info.context['auth']  # type: AuthService
        user_permissions = await auth_service.user_perms(info)

        # фильтр по родительскому корневому ID и сортировка по цифровому префиксу имени пункта первого уровня
        items_query = session.query(ItemModel).filter_by(
            parent_id=ROOT_ID
        ).order_by(
            await get_paragraph_order(ItemModel)
        )

        # добавляет фильтр по полученным о сервиса авторизации наименования пунктов,
        # если пользователь ограничен в правах
        if user_permissions:
            info.context['permission_filter'] = await get_user_items(session, user_permissions, ROOT_ID)
            items_query = items_query.filter(ItemModel.id.in_(info.context['permission_filter']))

        items = items_query.all()

        filters = kwargs.get('filters', {})
        # добавляет фильтр по префиксу имени пункта первого уровня, если ID корневого пункта не равно 0
        current_tree_item = session.query(RootModel).first()
        filter_item_id = current_tree_item.item_id
        if filter_item_id:
            filters.update(paragraph=filter_item_id)
        # фильтр пунктов первого уровня
        if filters:
            item_filter = ItemFilter(items, filters)
            items = await item_filter.filter_items()

        return items
def create_resolve_info(schema, request_string, variables=None, user=None):
    exe_context = create_execution_context(schema, request_string, variables,
                                           user)
    parent_type = get_operation_root_type(schema, exe_context.operation)
    field_asts = get_field_asts_from_execution_context(exe_context)

    field_ast = field_asts[0]
    field_name = field_ast.name.value

    field_def = get_field_def(schema, parent_type, field_name)
    if not field_def:
        return Undefined
    return_type = field_def.type

    # The resolve function's optional third argument is a context value that
    # is provided to every resolve function within an execution. It is commonly
    # used to represent an authenticated user, or request-specific caches.
    context = exe_context.context_value
    return ResolveInfo(field_name,
                       field_asts,
                       return_type,
                       parent_type,
                       schema=schema,
                       fragments=exe_context.fragments,
                       root_value=exe_context.root_value,
                       operation=exe_context.operation,
                       variable_values=exe_context.variable_values,
                       context=context)
def mock_info(context=None):
    return ResolveInfo(
        None,
        None,
        None,
        None,
        schema=None,
        fragments=None,
        root_value=None,
        operation=None,
        variable_values=None,
        context=context,
    )
Esempio n. 4
0
 def _create_resolve_info(self, field_name, field_asts, return_type, parent_type):
     return ResolveInfo(
         field_name,
         field_asts,
         return_type,
         parent_type,
         schema=self.root_info.schema,
         fragments=self.root_info.fragments,
         root_value=self.root_info.root_value,
         operation=self.root_info.operation,
         variable_values=self.root_info.variable_values,
         context=self.root_info.context,
     )
def info():
    db = create_engine('sqlite://')  # in-memory
    connection = db.engine.connect()
    transaction = connection.begin()
    Base.metadata.create_all(connection)

    session_factory = sessionmaker(bind=connection)
    session = scoped_session(session_factory)

    yield ResolveInfo(*[None] * 9, context={'session': session})

    transaction.rollback()
    connection.close()
    session.remove()