Research
Security News
Quasar RAT Disguised as an npm Package for Detecting Vulnerabilities in Ethereum Smart Contracts
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
cypartagraphqlsubscriptionstools
Advanced tools
A CypartaGraphqlSubscriptionsTools implementation for Graphene + Django built using Django Channels +reactive programming in python (RxPY) . Provides support for model creation, mutation and deletion,and get data with websocket or path list of events name for subscriptions .
A CypartaGraphqlSubscriptionsTools implementation for Graphene + Django built using Django Channels +reactive programming in python (RxPY) . Provides support for model creation, mutation and deletion,and get data with websocket or path list of events name for subscriptions .
libirary support Both WebSocket Protocol graphql-transport-ws and graphql-ws
for more info use full link : https://wundergraph.com/blog/quirks_of_graphql_subscriptions_sse_websockets_hasura_apollo_federation_supergraph#graphql-subscriptions-over-websockets:-subscription-transport-ws-vs-graphql-ws
libirary use async for more beast performance
Real-time GraphQL Subscriptions:
Support for Django Models:
Django Subscription Model Mixin:
Dynamic Subscription Management:
subscripe
and id
.WebSocket Protocol Support:
graphql-transport-ws
and graphql-ws
for compatibility with various GraphQL clients.Async Implementation:
Custom Event Support:
Observable and Reactive Programming:
rxpy
for handling observables and reactive programming, enabling application of various operations on subscription streams.Multi-Subscription Support:
Easy Integration with Graphene:
Ping Mechanism:
WebSocket Group Management:
In-memory Channel Layer Support:
Customizable Routing:
Compatibility with Django Lifecycle:
django_lifecycle
library to leverage Django model lifecycle events.Install CypartaGraphqlSubscriptionsTools
$ pip install django_lifecycle
$ pip install CypartaGraphqlSubscriptionsTools
Add CypartaGraphqlSubscriptionsTools
to INSTALLED_APPS
:
# your_project/settings.py
INSTALLED_APPS = [
# ...
'CypartaGraphqlSubscriptionsTools'
]
Add Django Channels to your project (see: Django Channels installation docs) and set up Channel Layers. If you don't want to set up a Redis instance in your dev environment yet, you can use the in-memory Channel Layer:
# your_project/settings.py
CHANNEL_LAYERS = {
"default": {
"BACKEND": "channels.layers.InMemoryChannelLayer"
}
}
Add CypartaGraphqlSubscriptionsConsumer
to your routing.py
file.
# your_project/routing.py
from channels.routing import ProtocolTypeRouter, URLRouter
from django.urls import path
from CypartaGraphqlSubscriptionsTools.consumers import CypartaGraphqlSubscriptionsConsumer
application = ProtocolTypeRouter({
"websocket": URLRouter([
path('graphql/', CypartaGraphqlSubscriptionsConsumer)
]),
})
Define your subscriptions and connect them to your project schema
#your_project/schema.py
import graphene
from asgiref.sync import async_to_sync
from your_app.graphql.subscriptions import YourSubscription
class Query(graphene.ObjectType):
base = graphene.String()
class Subscription(YourSubscription):
pass
schema = graphene.Schema(
query=Query,
subscription=Subscription
)
The Django Subscription Model Mixin is a powerful tool that seamlessly integrates with Django models to enable real-time GraphQL subscriptions when model instances are created, updated, or deleted. This mixin leverages the django_lifecycle
library for managing model lifecycle events and the CypartaGraphqlSubscriptionsTools
library for triggering GraphQL subscriptions.
Inherit from the CypartaSubscriptionModelMixin
in Your Django Model:
# your_project/models.py
from CypartaGraphqlSubscriptionsTools.mixins import CypartaSubscriptionModelMixin
class YourModel(CypartaSubscriptionModelMixin, models.Model):
# Your model fields and methods go here
Replace YourModel
with the actual name of your model. This mixin provides hooks for triggering subscriptions on model lifecycle events, such as creation, update, and deletion.
Subscriptions in Graphene are defined as normal ObjectType
's. Each subscription field resolver must return an observable which emits values matching the field's type.
A simple hello world subscription (which returns the value "hello world!"
every 3 seconds) could be defined as follows:
import graphene
from rx import Observable
class Subscription(graphene.ObjectType):
hello = graphene.String()
def resolve_hello(root, info):
return Observable.interval(3000) \
.map(lambda i: "hello world!")
Each subscription that you define will receive a an Consumer
of CypartaGraphqlSubscriptionsConsumer
's as the root
parameter, which will subscripe or cancel by detect_register_group_status
function .
This code snippet demonstrates how to implement GraphQL subscriptions for Django models using the CypartaGraphqlSubscriptionsTools library. In this example, a subscription named mymodelcreated
is defined to trigger events whenever a new instance of the MyModel
Django model is created. The subscripe
parameter is introduced, allowing for dynamic subscription management by providing a boolean value (True
to subscribe, False
to cancel subscription).
-GraphQL Type Definition: The code defines a GraphQL type YourModelType using graphene and graphene_django.types.DjangoObjectType for the MyModel Django model.
-Subscription Definition: The Subscription class extends graphene.ObjectType and includes a subscription field named get_my_model
. This field is associated with the MyModelType and includes the subscripe parameter to manage subscription status.
-Subscription Resolver Logic: The resolve_my_model_created function handles the resolution logic for the get_my_model
subscription. It dynamically extracts requested fields from the GraphQL query using info.field_nodes and filters for the relevant fields. The event name is constructed based on the model name and the event type ({model_name}Created
). The resolution logic is then delegated to the detect_register_group_status
function.
Event Triggering: The event is triggered whenever a new instance of MyModel
is created, leveraging the signals provided by django_lifecycle
.
import graphene
from graphene_django.types import DjangoObjectType
from CypartaGraphqlSubscriptionsTools.events import CREATED
from your_app.models import MyModel
class YourModelType(DjangoObjectType)
class Meta:
model = MyModel
class Subscription(graphene.ObjectType):
get_my_model = graphene.Field(MyModelType, subscripe=graphene.Boolean())
# Resolve function for handling 'get_my_model' based on 'subscripe'
def resolve_get_my_model(root, info, subscripe):
requested_fields = [field.name.value for field in info.field_nodes[0].selection_set.selections]
model_name = get_model_name_instance(MyModelType)
return async_to_sync(root.detect_register_group_status)([f'{model_name}Created'], subscripe, requested_fields)
You can also filter events based on a subscription's arguments. For example, here's a subscription that fires whenever a model is updated:
import graphene
from graphene_django.types import DjangoObjectType
from CypartaGraphqlSubscriptionsTools.events import UPDATED
from your_app.models import MyModel
class YourModelType(DjangoObjectType)
class Meta:
model = MyModel
class Subscription(graphene.ObjectType):
my_model_updated = graphene.Field(MyModelType, id=graphene.String(), subscripe=graphene.Boolean())
# Resolve function for handling 'my_model_updated' based on 'subscripe' and 'id'
def resolve_my_model_updated(root, info, subscripe, id):
model_name = get_model_name_instance(MyModelType)
return async_to_sync(root.detect_register_group_status)([f'{model_name}Updated.{id}'], subscripe)
You can also filter events based on a subscription's arguments. For example, here's a subscription that fires whenever a model is updated:
import graphene
from graphene_django.types import DjangoObjectType
from CypartaGraphqlSubscriptionsTools.events import UPDATED
from your_app.models import MyModel
class YourModelType(DjangoObjectType)
class Meta:
model = MyModel
class Subscription(graphene.ObjectType):
my_model_created_update_delete = graphene.Field(MyModelType, subscripe=graphene.Boolean(), id=graphene.String())
# Resolve function for handling create, update, delete operations based on 'subscripe' and 'id'
def resolve_my_model_created_update_delete(root, info, subscripe, id):
requested_fields = [field.name.value for field in info.field_nodes[0].selection_set.selections]
model_name = get_model_name_instance(MyModelType)
groups = [f'{model_name}Created', f'{model_name}Updated.{id}', f'{model_name}Deleted.{id}']
if id == "":
groups = [f'{model_name}Created']
return async_to_sync(root.detect_register_group_status)(groups, subscripe, requested_fields)
Defining a subscription that is fired whenever a given model instance is deleted can be accomplished like so
import graphene
from graphene_django.types import DjangoObjectType
from CypartaGraphqlSubscriptionsTools.events import DELETED
from your_app.models import MyModel
class YourModelType(DjangoObjectType)
class Meta:
model = MyModel
class Subscription(graphene.ObjectType):
my_model_deleted = graphene.Field(MyModelType, id=graphene.String(), subscripe=graphene.Boolean())
# Resolve function for handling 'my_model_deleted' based on 'subscripe' and 'id'
def resolve_my_model_deleted(root, info, subscripe, id):
model_name = get_model_name_instance(MyModelType)
return async_to_sync(root.detect_register_group_status)([f'{model_name}Deleted.{id}'], subscripe)
To create a custom type in GraphQL using Graphene, you need to define a new class for your custom type by extending graphene.ObjectType
. Let's say you want to create a custom type named CustomType
. Here's an example:
import graphene
class CustomType(graphene.ObjectType):
# Define fields for your custom type
field1 = graphene.String()
field2 = graphene.Int()
# Add more fields as needed
In this example, CustomType
has two fields: field1
of type graphene.String()
and field2
of type graphene.Int()
. You can customize the fields based on the data you want to include in your custom type.
Now, you can use this CustomType
in your my_custom_event
subscription:
import graphene
my_custom_event = graphene.Field(CustomType)
def resolve_my_custom_event(root, info, subscripe):
return async_to_sync(root.detect_register_group_status)(['custom_event'], subscripe)
# elsewhere in your app:
from CypartaGraphqlSubscriptionsTools.events import trigger_subscription
async_to_sync(trigger_subscription)(f"{model_name}Created", self)
Custom middleware, such as TokenAuthMiddleware
, can be added to provide additional functionality, such as authentication or permission checks, to WebSocket connections.
from CypartaGraphqlSubscriptionsTools.middleware import TokenAuthMiddleware
application = ProtocolTypeRouter({
"http": django_asgi_app,
"websocket":
AuthMiddlewareStack(
TokenAuthMiddleware(
URLRouter(
CypartaGraphqlSubscriptionsTools.routing.websocket_urlpatterns
)
))
})
FAQs
A CypartaGraphqlSubscriptionsTools implementation for Graphene + Django built using Django Channels +reactive programming in python (RxPY) . Provides support for model creation, mutation and deletion,and get data with websocket or path list of events name for subscriptions .
We found that cypartagraphqlsubscriptionstools demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
Security News
Research
A supply chain attack on Rspack's npm packages injected cryptomining malware, potentially impacting thousands of developers.
Research
Security News
Socket researchers discovered a malware campaign on npm delivering the Skuld infostealer via typosquatted packages, exposing sensitive data.