class ActionInfo(Object): fields = { "name": StringType(), "parameters": PlainListType(ObjectType(FieldInfo)), "data": PlainListType(ObjectType(FieldInfo)), "return_type": StringType(), "permitted": BooleanType(), "deny_reason": StringType(nullable=True), "retry_in": DurationType(nullable=True), "mutation": BooleanType() } hidden = True
def integer_filters(field_name): return build_filters_for_field( OrderedDict({ "": IntegerType(nullable=True), "exact": IntegerType(nullable=True), "gt": IntegerType(nullable=True), "gte": IntegerType(nullable=True), "in": PlainListType(IntegerType(), nullable=True), "isnull": BooleanType(nullable=True), "lt": IntegerType(nullable=True), "lte": IntegerType(nullable=True), }), field_name)
def generate(self): query_classes = [] mutation_classes = {} at_least_one_query_action_exists = False for obj in self.objects: for name, field in self.convert_output_fields_for_object( obj).items(): get_class(obj)._meta.fields[name] = field for name, field in self.convert_input_fields_for_object( obj).items(): get_class(obj, input=True)._meta.fields[name] = field query_actions, mutation_actions = self.convert_actions( obj.actions, obj.__name__) if query_actions: at_least_one_query_action_exists = True query_class = type("Query", (graphene.ObjectType, ), query_actions) query_classes.append(query_class) self.update_mutation_classes(mutation_actions, mutation_classes) query_actions, mutation_actions = self.convert_actions( self.extra_actions) # if there are no query actions, create a dummy one, since graphene-python needs that if not query_actions and not at_least_one_query_action_exists: query_actions = { "dummy": Action(return_value=BooleanType(), exec_fn=Function( lambda request, params: False)).convert(self) } self.update_mutation_classes(mutation_actions, mutation_classes) query_class = type("Query", tuple(query_classes) + (graphene.ObjectType, ), query_actions) mutation_class = None if mutation_classes: mutation_class = type("Mutation", (graphene.ObjectType, ), mutation_classes) check_classes_for_fields() return graphene.Schema(query=query_class, mutation=mutation_class, auto_camelcase=False)
def string_filters(field_name): return build_filters_for_field( OrderedDict({ "": StringType(nullable=True), "contains": StringType(nullable=True), "endswith": StringType(nullable=True), "exact": StringType(nullable=True), "icontains": StringType(nullable=True), "in": PlainListType(StringType(), nullable=True), "iregex": StringType(nullable=True), "isnull": BooleanType(nullable=True), "regex": StringType(nullable=True), "startswith": StringType(nullable=True), }), field_name)
def __init__(self, custom_parameters=None, data=None, return_value=None, get_fn=None, exec_fn=None, permissions=None, **kwargs): return_value = return_value or BooleanType() super().__init__(custom_parameters=custom_parameters, data=data, return_value=return_value, get_fn=get_fn, exec_fn=exec_fn, permissions=permissions, **kwargs)
from datetime import timedelta from simple_api.adapters.graphql.graphql import GraphQLAdapter from simple_api.adapters.utils import generate from simple_api.object.actions import Action from simple_api.object.datatypes import BooleanType from simple_api.object.permissions import AllowNone, AllowAll from simple_api.adapters.graphql.utils import build_patterns actions = { "allow": Action(return_value=BooleanType(), exec_fn=lambda **kwargs: True, permissions=AllowAll, retry_in=timedelta(hours=1)), "deny": Action(return_value=BooleanType(), exec_fn=lambda **kwargs: True, permissions=AllowNone, retry_in=timedelta(days=3, seconds=10)), "hide": Action(return_value=BooleanType(), exec_fn=lambda **kwargs: True, permissions=AllowNone, hide_if_denied=True), } schema = generate(GraphQLAdapter, actions) patterns = build_patterns("api/", schema)