-
Notifications
You must be signed in to change notification settings - Fork 0
/
__init__.py
38 lines (29 loc) · 1.21 KB
/
__init__.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
__all__ = ['Plugin']
from collections.abc import AsyncIterator, MutableMapping
from apluggy import asynccontextmanager
from nextline import Nextline
from nextlinegraphql.hook import spec
from .cache import CacheStdout
from .example_script import statement
from .schema import Mutation, Query, Subscription
class Plugin:
@spec.hookimpl
def schema(self) -> tuple[type, type | None, type | None]:
return (Query, Mutation, Subscription)
@spec.hookimpl
async def update_lifespan_context(self, context: MutableMapping) -> None:
self._nextline = Nextline(statement)
context['nextline'] = self._nextline
@spec.hookimpl(trylast=True) # trylast so to be the innermost context
@asynccontextmanager
async def lifespan(self) -> AsyncIterator[None]:
'''Yield within the nextline context.'''
self._cache_stdout = CacheStdout()
self._nextline.register(self._cache_stdout)
async with self._cache_stdout, self._nextline:
yield
@spec.hookimpl
def update_strawberry_context(self, context: MutableMapping) -> None:
context['nextline'] = self._nextline
ctrl = {'cache_stdout': self._cache_stdout}
context['ctrl'] = ctrl