Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor resolvers in order to accept query args
- Loading branch information
1 parent
f890ac5
commit 87395a1
Showing
10 changed files
with
111 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,44 @@ | ||
import inspect | ||
import functools | ||
from typing import Callable, Dict, Optional, Any | ||
|
||
import graphql | ||
from graphql.language.ast import Value | ||
|
||
from slothql.utils.functional import is_method, get_function_signature | ||
|
||
ResolveArgs = Dict[str, Value] | ||
PartialResolver = Callable[[Optional[Any], Optional[graphql.ResolveInfo], Optional[ResolveArgs]], Callable] | ||
Resolver = Callable[[Any, graphql.ResolveInfo, ResolveArgs], Callable] | ||
|
||
|
||
def _get_function(field, resolver: PartialResolver = None) -> Optional[Resolver]: | ||
if resolver is None: | ||
return None | ||
if isinstance(resolver, staticmethod): | ||
return resolver.__func__ | ||
if isinstance(resolver, classmethod): | ||
return functools.partial(resolver.__func__, type(field)) | ||
if is_method(resolver): | ||
return functools.partial(resolver, field) | ||
return resolver | ||
|
||
|
||
def _inject_missing_args(func: PartialResolver) -> Resolver: | ||
signature, arg_count = get_function_signature(func) | ||
assert arg_count <= 3, f'{func} expected arguments to be of signature (parent, info, args), received {signature}' | ||
if arg_count < 3: | ||
@functools.wraps(func) | ||
def resolver(parent, info, args): | ||
if arg_count == 0: | ||
return func() | ||
elif arg_count == 1: | ||
return func(parent) | ||
return func(parent, info) | ||
|
||
class Resolver: | ||
@staticmethod | ||
def is_method(func): | ||
return func.__code__.co_varnames and 'self' == func.__code__.co_varnames[0] | ||
|
||
def __init__(self, field, resolver): | ||
func = self.get_function(field, resolver) | ||
self.func = func and self.inject_missing_args(func) | ||
|
||
@classmethod | ||
def get_function(cls, field, resolver): | ||
if resolver is None: | ||
return None | ||
if isinstance(resolver, staticmethod): | ||
return resolver.__func__ | ||
if isinstance(resolver, classmethod): | ||
return functools.partial(resolver.__func__, type(field)) | ||
if cls.is_method(resolver): | ||
return functools.partial(resolver, field) | ||
return resolver | ||
return func | ||
|
||
|
||
@classmethod | ||
def inject_missing_args(cls, func): | ||
if isinstance(func, functools.partial): | ||
signature = inspect.signature(func.func) | ||
arg_count = len(signature.parameters) - len(func.args) | ||
else: | ||
assert callable(func), f'Expected callable, got {func}' | ||
signature = inspect.signature(func) | ||
arg_count = len(signature.parameters) | ||
assert arg_count <= 2, \ | ||
f'{func} expected arguments to be of signature (obj, info), received {signature}' | ||
if arg_count < 2: | ||
@functools.wraps(func) | ||
def resolver(obj, info): | ||
return func() if arg_count == 0 else func(obj) | ||
|
||
return resolver | ||
return func | ||
def get_resolver(field, resolver: PartialResolver) -> Resolver: | ||
func = _get_function(field, resolver) | ||
return func and _inject_missing_args(func) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import inspect | ||
import functools | ||
from typing import Callable, Tuple | ||
|
||
|
||
def is_method(func): | ||
return func.__code__.co_varnames and 'self' == func.__code__.co_varnames[0] # FIXME: noqa | ||
|
||
|
||
def get_function_signature(func: Callable) -> Tuple[inspect.Signature, int]: | ||
if isinstance(func, functools.partial): | ||
signature = inspect.signature(func.func) | ||
return signature, len(signature.parameters) - len(func.args) | ||
assert callable(func), f'Expected callable, got {func}' | ||
signature = inspect.signature(func) | ||
return signature, len(signature.parameters) |