Last active
August 16, 2020 13:19
-
-
Save ProjectCheshire/277c5ff1468460a05c2a44556e270eff to your computer and use it in GitHub Desktop.
Autoschemer.py
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
import os | |
import importlib | |
from inspect import getmembers, isclass | |
from graphene import ObjectType | |
from logzero import logger | |
def schema_operations_builder(operationName, operationModule, operationBase, clsName): | |
op_base_classes = build_base_classes(operationName, operationModule, operationBase, clsName) | |
properties = {} | |
# filter on scopes before this | |
for base_class in op_base_classes: | |
properties.update(base_class.__dict__['_meta'].fields) | |
ALL = type(operationName, tuple(op_base_classes), properties) | |
return ALL | |
def build_base_classes(operationName, operationModule, operationBase, clsName): | |
class OperationAbstract(ObjectType): | |
scopes = ['unauthorized'] | |
pass | |
current_directory = os.path.dirname(os.path.abspath(__file__)) | |
# current_module = current_directory.split('/')[-1] | |
subdirectories = [x for x in os.listdir(current_directory) if | |
os.path.isdir(os.path.join(current_directory, x)) and x != '__pycache__' and x != 'root'] | |
op_base_classes = [OperationAbstract] | |
for directory in subdirectories: | |
try: | |
module = importlib.import_module(f'api.models.{directory}.{operationModule}') | |
if module: | |
classes = [x for x in getmembers(module, isclass)] | |
opers = [x[1] for x in classes if clsName in x[0] and x[0] != operationBase] | |
op_base_classes += opers | |
else: | |
logger.info('wat?') | |
logger.debug(current_directory) | |
except ModuleNotFoundError: | |
pass | |
op_base_classes = op_base_classes[::-1] | |
return op_base_classes |
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
from graphene.types.objecttype import ObjectType, ObjectTypeOptions | |
class BaseQuery(ObjectType): | |
pass | |
class BaseMutation(ObjectType): | |
pass | |
class BaseSubscription(ObjectType): | |
pass |
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
from .schemer import schema_operations_builder | |
GEN = schema_operations_builder('GenQueries', 'query', 'BaseQuery', 'Query') | |
ALL_QUERIES = schema_operations_builder('Query', 'query', 'BaseQuery', 'Query') | |
MUT = schema_operations_builder('GenMutations', 'mutation', 'BaseMutation', 'Mutation') | |
SCHEMA = Schema(query=GEN, mutation=MUT, subscription=SUBS) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Why is there a
ALL_QUERIES
that is not used inSchemer.py
? Also noSUBS
definition.... guess this code does not work?