Skip to content

Instantly share code, notes, and snippets.

@ProjectCheshire
Last active August 16, 2020 13:19
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ProjectCheshire/277c5ff1468460a05c2a44556e270eff to your computer and use it in GitHub Desktop.
Save ProjectCheshire/277c5ff1468460a05c2a44556e270eff to your computer and use it in GitHub Desktop.
Autoschemer.py
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
from graphene.types.objecttype import ObjectType, ObjectTypeOptions
class BaseQuery(ObjectType):
pass
class BaseMutation(ObjectType):
pass
class BaseSubscription(ObjectType):
pass
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)
@coler-j
Copy link

coler-j commented Aug 16, 2020

Why is there a ALL_QUERIES that is not used in Schemer.py? Also no SUBS definition.... guess this code does not work?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment