def register_mutation(self, node, graph_name): inp_name = f'{node.__name__}Input' allowed_args = ['_from', '_to'] for name, func in mutators.items(): if input_reg.get(inp_name, None) is None: args = { k: v for k, v in node.__dict__.items() if isinstance(v, Scalar) and ( k in allowed_args or not k.startswith('_')) } input_reg[inp_name] = type(inp_name, (InputObjectType, ), {**args}) inp_type = input_reg[inp_name] arg_cfg = {f'{snake_case(node.__name__)}': inp_type()} if name != 'create': arg_cfg['id'] = String() mutation_class = type( f'{name.title()}{node.__name__}', (Mutation, ), { 'Arguments': type('Arguments', (), {**arg_cfg}), 'Output': node, 'mutate': func(node, graph_name) }) self._mutations[snake_case( mutation_class.__name__)] = mutation_class
def __init_subclass__(cls, **kwargs): cls.Meta = type('Meta', (), {'interfaces': (GQNode,)}) super().__init_subclass__(**kwargs) cls._collname_ = ifl.plural(snake_case(cls.__name__)) node_registry[cls.__name__] = cls connection_registry[cls._collname_] = type( f'{cls.__name__}Connection', (Connection,), {'Meta': type('Meta', (), {'node': cls})} )
async def _create_edge(_, info, **kwargs): data = { k: v for k, v in kwargs[snake_case(node.__name__)].items() if k.lower() not in ['_id', 'id'] } data['_created'] = arrow.utcnow().timestamp new_data = await info.context['db'][graph_name].edge_create( node._collname_, data) return node(**data, **new_data)
async def _update(_, info, **kwargs): q = f'FOR doc IN {node._collname_}' data = { **kwargs[snake_case(node.__name__)], '_updated': arrow.utcnow().timestamp } _id = data.pop('_id', data.pop('id', None)) if _id is None: _from = data.pop('_from', None) _to = data.pop('_to', None) q = (f'{q}' f' FILTER doc._from == \"{_from}\"' f' AND doc._to == \"{_to}\"') else: q = F'{q} FILTER doc._key == \"{_id}\"' q = (f'{q}' f' LIMIT 1' f' UPDATE doc WITH {json.dumps(data)} IN {node._collname_}' f' RETURN NEW') new_data = await info.context['db'].fetch_one(q) return node(**list(new_data)[0])
def __init_subclass__(cls, **kwargs): cls._from = String() cls._to = String() super().__init_subclass__(**kwargs) cls._collname_ = snake_case(cls.__name__) edge_registry[cls.__name__] = cls
def register_subscription(self, subscription): self._subscriptions[snake_case(subscription.__name__)] = subscription
def register_query(self, query): self._queries[snake_case(query.__name__)] = query