|
| 1 | +from rath.rath import Rath, current_rath |
| 2 | +from typing import AsyncIterator, Iterator, Protocol, Type, TypeVar, Any, Dict, Optional |
| 3 | +from pydantic import BaseModel |
| 4 | + |
| 5 | +from rath.scalars import IDCoercible |
| 6 | + |
| 7 | + |
| 8 | +# --- Base meta description --- |
| 9 | +class TurmsMeta(Protocol): |
| 10 | + """Meta class for Turms operations""" |
| 11 | + |
| 12 | + document: str |
| 13 | + name: str |
| 14 | + type: str |
| 15 | + |
| 16 | + |
| 17 | +TMeta = TypeVar("TMeta", bound=TurmsMeta) |
| 18 | + |
| 19 | + |
| 20 | +class TurmsFragment(Protocol[TMeta]): |
| 21 | + """Represents a Turms operation that is both callable and its own return type.""" |
| 22 | + |
| 23 | + Meta: Type[TMeta] |
| 24 | + |
| 25 | + def __init__(self, *args: Any, **kwargs: Any) -> None: |
| 26 | + """A pydantic constructor""" |
| 27 | + ... |
| 28 | + |
| 29 | + |
| 30 | +TFragment = TypeVar("TFragment", bound=TurmsFragment[Any]) |
| 31 | + |
| 32 | + |
| 33 | +def fetch_fragment_via( |
| 34 | + fragment: Type[TFragment], |
| 35 | + id: IDCoercible, |
| 36 | + rath: Optional[Rath] = None, |
| 37 | +) -> TFragment: |
| 38 | + """Synchronously Executes an a query or mutation using rath |
| 39 | +
|
| 40 | +
|
| 41 | + This function will execute an operation using rath, retrieving |
| 42 | + the currentliy active rath client from the current_rath context |
| 43 | + if no rath client is provided. |
| 44 | +
|
| 45 | + Parameters |
| 46 | + ---------- |
| 47 | + operation : TurmsOperation |
| 48 | + The turms operation to execute |
| 49 | + variables : Dict[str, Any] |
| 50 | + The variables to use |
| 51 | + rath : Optional[Rath], optional |
| 52 | + The rath client, by default the current rath client |
| 53 | +
|
| 54 | + Returns |
| 55 | + ------- |
| 56 | + BaseModel |
| 57 | + The result of the operation |
| 58 | + """ |
| 59 | + rath = rath or current_rath.get() |
| 60 | + assert rath is not None, ( |
| 61 | + "No rath client provided and no rath client in current_rath context" |
| 62 | + ) |
| 63 | + return fragment( |
| 64 | + **rath.query( |
| 65 | + """ |
| 66 | + %s |
| 67 | + query Entities($representations: [_Any!]!) { |
| 68 | + entities: _entities(representations: $representations) { |
| 69 | + ...%s |
| 70 | + } |
| 71 | + } |
| 72 | + """ |
| 73 | + % ( |
| 74 | + fragment.Meta.document, |
| 75 | + fragment.Meta.name, |
| 76 | + ), |
| 77 | + {"representations": [{"__typename": fragment.Meta.type, "id": id}]}, |
| 78 | + ).data["entities"][0], |
| 79 | + ) |
| 80 | + |
| 81 | + |
| 82 | +async def afetch_fragment_via( |
| 83 | + fragment: Type[TFragment], |
| 84 | + id: IDCoercible, |
| 85 | + rath: Optional[Rath] = None, |
| 86 | +) -> TFragment: |
| 87 | + """Synchronously Executes an a query or mutation using rath |
| 88 | +
|
| 89 | +
|
| 90 | + This function will execute an operation using rath, retrieving |
| 91 | + the currentliy active rath client from the current_rath context |
| 92 | + if no rath client is provided. |
| 93 | +
|
| 94 | + Parameters |
| 95 | + ---------- |
| 96 | + operation : TurmsOperation |
| 97 | + The turms operation to execute |
| 98 | + variables : Dict[str, Any] |
| 99 | + The variables to use |
| 100 | + rath : Optional[Rath], optional |
| 101 | + The rath client, by default the current rath client |
| 102 | +
|
| 103 | + Returns |
| 104 | + ------- |
| 105 | + BaseModel |
| 106 | + The result of the operation |
| 107 | + """ |
| 108 | + rath = rath or current_rath.get() |
| 109 | + assert rath is not None, ( |
| 110 | + "No rath client provided and no rath client in current_rath context" |
| 111 | + ) |
| 112 | + return fragment( |
| 113 | + **( |
| 114 | + await rath.aquery( |
| 115 | + """ |
| 116 | + %s |
| 117 | + query Entities($representations: [_Any!]!) { |
| 118 | + entities: _entities(representations: $representations) { |
| 119 | + ...%s |
| 120 | + } |
| 121 | + } |
| 122 | + """ |
| 123 | + % ( |
| 124 | + fragment.Meta.document, |
| 125 | + fragment.Meta.name, |
| 126 | + ), |
| 127 | + {"representations": [{"__typename": fragment.Meta.name, "id": id}]}, |
| 128 | + ) |
| 129 | + ).data["entities"][0], |
| 130 | + ) |
0 commit comments