Giter Club home page Giter Club logo

Comments (3)

fierysolid avatar fierysolid commented on July 26, 2024 2

+1

from graphql-ws.

zerlok avatar zerlok commented on July 26, 2024 2

Our team also have the same problem when clients try to subscribe on multiple levels of graphql objects. We're using aiohttp and we made a simple workaround to solve the problem.

import asyncio
import inspect

from typing import Union, Awaitable, Any, List, Tuple, Dict
from graphql_ws.aiohttp import AiohttpSubscriptionServer


class SubscriptionServer(AiohttpSubscriptionServer):

    async def send_execution_result(self, connection_context, op_id, execution_result) -> None:
        resolving_items: List[Awaitable[Any]] = []

        queue: List[Tuple[Union[List[Any], Dict[Union[int, str], Any]], Union[int, str], Any]] = [
            (
                None,
                None,
                execution_result.data,
            ),
        ]
        while queue:
            container, key, item = queue.pop(0)

            if isinstance(item, list):
                self.__extend_list_item(queue, item)

            elif isinstance(item, dict):
                self.__extend_dict_item(queue, item)

            elif inspect.isawaitable(item):
                container = container if container is not None else queue
                key = key if key is not None else 0

                resolving_items.append(self.__resolve_container_item(container, key, item))

        await asyncio.gather(*resolving_items)

        await super().send_execution_result(connection_context, op_id, execution_result)

        return None

    async def __resolve_container_item(
            self,
            container: Union[List[Any], Dict[Union[int, str], Any]],
            key: Union[int, str],
            item: Awaitable[Any],
    ) -> None:
        container[key] = await item

    def __extend_list_item(
            self,
            queue: List[Tuple[Union[List[Any], Dict[Union[int, str], Any]], Union[int, str], Any]],
            item: List[Any],
    ) -> None:
        queue.extend(
            (
                item,
                index,
                value,
            )
            for index, value in enumerate(item)
        )

    def __extend_dict_item(
            self,
            queue: List[Tuple[Union[List[Any], Dict[Union[int, str], Any]], Union[int, str], Any]],
            item: Dict[Union[int, str], Any],
    ) -> None:
        queue.extend(
            (
                item,
                key,
                value,
            )
            for key, value in item.items()
        )

The main idea is to walk through the containers' objects and await for awaitable objects

from graphql-ws.

SmileyChris avatar SmileyChris commented on July 26, 2024

Hopefully fixed. Reopen if you can still reproduce using code on master

from graphql-ws.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.