{"id":28028595,"url":"https://github.com/syfun/gql-subscriptions","last_synced_at":"2025-07-07T13:36:00.676Z","repository":{"id":57435664,"uuid":"257606083","full_name":"syfun/gql-subscriptions","owner":"syfun","description":"A Python3.7+ port of Apollo Graphql Subscriptions.","archived":false,"fork":false,"pushed_at":"2020-09-03T06:28:42.000Z","size":25,"stargazers_count":6,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-05-18T13:49:30.267Z","etag":null,"topics":["graphql","pubsub","python","subscription"],"latest_commit_sha":null,"homepage":null,"language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/syfun.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-04-21T13:33:23.000Z","updated_at":"2023-09-16T11:15:16.000Z","dependencies_parsed_at":"2022-08-27T23:11:23.039Z","dependency_job_id":null,"html_url":"https://github.com/syfun/gql-subscriptions","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"purl":"pkg:github/syfun/gql-subscriptions","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/syfun%2Fgql-subscriptions","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/syfun%2Fgql-subscriptions/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/syfun%2Fgql-subscriptions/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/syfun%2Fgql-subscriptions/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/syfun","download_url":"https://codeload.github.com/syfun/gql-subscriptions/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/syfun%2Fgql-subscriptions/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":264086665,"owners_count":23555375,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["graphql","pubsub","python","subscription"],"created_at":"2025-05-11T07:19:14.442Z","updated_at":"2025-07-07T13:36:00.619Z","avatar_url":"https://github.com/syfun.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# gql-subscriptions\n\nA Python3.7+ port of [Apollo Graphql Subscriptions](https://github.com/apollographql/graphql-subscriptions).\n\nThis package contains a basic asyncio pubsub system which should be used only in demo, and other pubsub system(like Redis).\n\n## Requirements\n\nPython 3.7+\n\n## Installation\n\n`pip install gql-subscriptions`\n\n\u003e This package should be used with a network transport, for example [starlette-graphql](https://github.com/syfun/starlette-graphql)\n\n## Getting started with your first subscription\n\nTo begin with GraphQL subscriptions, start by defining a GraphQL Subscription type in your schema:\n\n```\ntype Subscription {\n    somethingChanged: Result\n}\n\ntype Result {\n    id: String\n}\n```\n\nNext, add the Subscription type to your schema definition:\n\n```\nschema {\n  query: Query\n  mutation: Mutation\n  subscription: Subscription\n}\n```\n\nNow, let's create a simple `PubSub` instance - it is simple pubsub implementation, based on `asyncio.Queue`.\n\n```python\nfrom gql_subscriptions import PubSub\n\npubsub = PubSub()\n```\n\nNow, implement your Subscriptions type resolver, using the `pubsub.async_iterator` to map the event you need(use [python-gql](https://github.com/syfun/python-gql)):\n\n```python\nfrom gql_subscriptions import PubSub, subscribe\n\n\npubsub = PubSub()\n\nSOMETHING_CHANGED_TOPIC = 'something_changed'\n\n\n@subscribe\nasync def something_changed(parent, info):\n    return pubsub.async_iterator(SOMETHING_CHANGED_TOPIC)\n```\n\nNow, the GraphQL engine knows that `somethingChanged` is a subscription, and every time we use pubsub.publish over this topic - it will publish it using the transport we use:\n\n```\npubsub.publish(SOMETHING_CHANGED_TOPIC, {'somethingChanged': {'id': \"123\" }})\n```\n\n\u003eNote that the default PubSub implementation is intended for demo purposes. It only works if you have a single instance of your server and doesn't scale beyond a couple of connections. For production usage you'll want to use one of the [PubSub implementations](#pubsub-implementations) backed by an external store. (e.g. Redis).\n\n## Filters\n\nWhen publishing data to subscribers, we need to make sure that each subscriber gets only the data it needs.\n\nTo do so, we can use `with_filter` decorator, which wraps the `subscription resolver` with a filter function, and lets you control each publication for each user.\n\n```\nResolverFn = Callable[[Any, Any, Dict[str, Any]], Awaitable[AsyncIterator]]\nFilterFn = Callable[[Any, Any, Dict[str, Any]], bool]\n\ndef with_filter(filter_fn: FilterFn) -\u003e Callable[[ResolverFn], ResolverFn]\n    ...\n```\n\n`ResolverFn` is a async function which returned a `typing.AsyncIterator`.\n```\nasync def something_changed(parent, info) -\u003e typing.AsyncIterator\n```\n\n`FilterFn` is a filter function, executed with the payload(published value), operation info, arugments, and must return bool.\n\nFor example, if `something_changed` would also accept a argument with the ID that is relevant, we can use the following code to filter according to it:\n\n```python\nfrom gql_subscriptions import PubSub, subscribe, with_filter\n\n\npubsub = PubSub()\n\nSOMETHING_CHANGED_TOPIC = 'something_changed'\n\n\ndef filter_thing(payload, info, relevant_id):\n    return payload['somethingChanged'].get('id') == relevant_id\n\n\n@subscribe\n@with_filter(filter_thing)\nasync def something_changed(parent, info, relevant_id):\n    return pubsub.async_iterator(SOMETHING_CHANGED_TOPIC)\n```\n\n## Channels Mapping\n\nYou can map multiple channels into the same subscription, for example when there are multiple events that trigger the same subscription in the GraphQL engine.\n\n```python\nfrom gql_subscriptions import PubSub, subscribe, with_filter\n\npubsub = PubSub()\n\nSOMETHING_UPDATED = 'something_updated'\nSOMETHING_CREATED = 'something_created'\nSOMETHING_REMOVED = 'something_removed'\n\n\n@subscribe\nasync def something_changed(parent, info):\n    return pubsub.async_iterator([SOMETHING_UPDATED, SOMETHING_CREATED, SOMETHING_REMOVED])\n```\n\n## PubSub Implementations\n\nIt can be easily replaced with some other implements of [PubSubEngine abstract class](https://github.com/syfun/gql-subscriptions/blob/master/gql_subscriptions/engine.py).\n\nThis package contains a `Redis` implements.\n\n```python\nfrom gql import subscribe\nfrom gql_subscriptions.pubsubs.redis import RedisPubSub\n\n\npubsub = RedisPubSub()\n\nSOMETHING_CHANGED_TOPIC = 'something_changed'\n\n\n@subscribe\nasync def something_changed(parent, info):\n    return pubsub.async_iterator(SOMETHING_CHANGED_TOPIC)\n```\n\nYou can also implement a `PubSub` of your own, by using the inherit `PubSubEngine` from this package, this is a [Reids example](https://github.com/syfun/gql-subscriptions/blob/master/gql_subscriptions/pubsubs/redis.py).","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsyfun%2Fgql-subscriptions","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsyfun%2Fgql-subscriptions","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsyfun%2Fgql-subscriptions/lists"}