A quick a dirty example of how a dictionary could be used modify Graphene ObjectType
field definitions without restarting the engine.
import graphene modeled = [{ "type": "String", "name": "hello" }] class Query(graphene.ObjectType): hello = graphene.String(argument=graphene.String(default_value="stranger")) def resolve_hello(self, info, argument): return 'Hello ' + argument class Query2(graphene.ObjectType): empty = graphene.String() def resolve_hello(self, info, argument): return 'Hello ' + argument for m in modeled: t = getattr(graphene, m.get('type')) n = m.get('name') d = graphene.Dynamic(lambda: t) f = graphene.Field( graphene.String, argument=graphene.String(default_value="stranger") ) Query2._meta.fields.update({n: f}) setattr(Query2, n, t(argument=graphene.String(default_value="stranger"))) schema = graphene.Schema(query=Query) schema2 = graphene.Schema(query=Query2) result = schema.execute('{ hello }') print(result.data['hello']) # "Hello stranger" # or passing the argument in the query result = schema2.execute('{ hello (argument: "graph") }') print(result.data['hello']) # "Hello graph"
Leave a Reply