No project description provided
Project description
Pleasant python promises
Python promises are a thing, really! But they get ugly quickly. This package lets you handle promises using generator syntax.
Here's what using 3 sequential promises looks like without this package:
def resolve_greatgrandparent(person,info):
parent_prom = person_loader.load(person.parent_id)
def handle_parent(parent):
grandparent_prom = person_loader.load(parent.parent_id)
def handle_grandparent(grandparent):
return person_loader.load(grandparent.parent_id)
return grandparent_prom.then(handle_grandparent)
return parent_prom.then(handle_parent)
And now with this package,
from pleasant_promises import genfunc_to_prom
@genfunc_to_prom
def resolve_greatgrandparent_name(person,info):
parent = await person_loader.load(person.parent_id)
grandparent = await person_loader.load(parent.parent_id)
great_grandparent = await person_loader.load(grandparent.parent_id)
return great_grandparent
Usage with Graphql/Graphene
If you're using promises, you're probably also using graphene or at least graphql. There are a few other helpers in here to help with your graphql schema,
Dataloaders
This package has a useful subclass wrapper of the promise package's Dataloader
. You can overload its batch_load
method with a generator function. This is useful if you want call another dataloader within batch_load. This class will also convert non-promise values to promises, which is required by the API we're wrapping.
from pleasant_promises.dataloader import Dataloader
class GrandparentLoader(DataLoader):
def batch_load(self,keys):
parents = await person_loader.load_many(keys)
grandparents = await person_loader.load_many(parent.parent_id for parent in parents)
# ...
You'll still have to decorate other dataloader methods with @genfunc_to_prom
, though:
from pleasant_promises import genfunc_to_prom
class GrandparentLoader(DataLoader):
@genfunc_to_prom
def get_grandparent_for_single_key(self,key):
parent = await person_loader.load(key)
return await person_loader.load(parent.parent_id)
def batch_load(self,keys):
return Promise.all([self.get_grandparent_for_single_key(key) for key in keys])
Graphene middleware
Without the middleware, you'll have to decorate all your resolvers that want to use the generator syntax.
from pleasant_promises import genfunc_to_prom
class MyPersonType(graphene.ObjectType):
# ...
@genfunc_to_prom
def resolve_grandparent(self,info):
parent = await person_loader.load(person.parent_id)
grandparent = await person_loader.load(parent.parent_id)
return grandparent
To avoid repeating this decorator on all your resolvers, use our pleasant middleware
from pleasant_promises.graphene import promised_generator_middleware
my_graphene_schema.execute('THE QUERY', middleware=[promised_generator_middleware])
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file pleasant_promises-1.0.0.tar.gz
.
File metadata
- Download URL: pleasant_promises-1.0.0.tar.gz
- Upload date:
- Size: 3.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.9.13
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | f29ae58fe5a54c2169e8be1114f87c8d2b1495d7899a2998bcc8e06bb6899244 |
|
MD5 | f5b009052257685b22efa3cf5b6ae06a |
|
BLAKE2b-256 | ef7c9eb9a0839a7ead0d9a0d3624e551b9496d403c4ef46c6f1a232f07a20d11 |
File details
Details for the file pleasant_promises-1.0.0-py3-none-any.whl
.
File metadata
- Download URL: pleasant_promises-1.0.0-py3-none-any.whl
- Upload date:
- Size: 4.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.9.13
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 687f2f78b0f35b08e1cfccffaefd611bad05a5aa02c0dce168483fe205db6341 |
|
MD5 | 44e744bc1c51b2ba3b6508f4a082f503 |
|
BLAKE2b-256 | 6f0efe01f6893aa53a0b3b21026aa29441ec6f9dc84e4a40eedbcc33fc2c10cc |