예제 #1
0
class Book(DjangoObject):
    model = BookModel
    custom_actions = {
        "getById": Action(parameters={"id": IntegerType(validators=And(NotNegative, NotZero))},
                          return_value=ObjectType("self"),
                          validators=NotRestrictedBook,
                          exec_fn=get_by_id),
        "getById2": Action(parameters={"id": IntegerType(validators=NotNegative)},
                           data={"Title": StringType(validators=LongerThen3Characters)},
                           return_value=ObjectType("self"),
                           validators=NotRestrictedBook,
                           exec_fn=get_by_id)}
예제 #2
0
 def to_action(self):
     if self._action is None:
         self.determine_exec_fn()
         self.determine_permissions()
         self.determine_parameters()
         self.determine_data()
         self._action = Action(parameters=self._determined_parameters,
                               data=self._determined_data,
                               return_value=self.return_value,
                               exec_fn=self._determined_exec_fn,
                               permissions=self._determined_permissions,
                               **self.kwargs)
     return self._action
예제 #3
0
    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)
예제 #4
0
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, Not, AllowAll, Or, And

from simple_api.adapters.graphql.utils import build_patterns

actions = {
    "allow1":
    Action(return_value=BooleanType(),
           exec_fn=lambda **kwargs: True,
           permissions=Not(AllowNone)),
    "allow2":
    Action(return_value=BooleanType(),
           exec_fn=lambda **kwargs: True,
           permissions=Or(AllowAll, AllowNone)),
    "allow3":
    Action(return_value=BooleanType(),
           exec_fn=lambda **kwargs: True,
           permissions=And(AllowAll, AllowAll)),
    "allow4":
    Action(return_value=BooleanType(),
           exec_fn=lambda **kwargs: True,
           permissions=(AllowAll, AllowAll)),
    "allow5":
    Action(return_value=BooleanType(),
           exec_fn=lambda **kwargs: True,
           permissions=Or(Not(AllowAll), Not(And(AllowAll, AllowNone)))),
    "deny1":
    Action(return_value=BooleanType(),
예제 #5
0
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 ObjectType, StringType, IntegerType
from simple_api.object.object import Object

from simple_api.adapters.graphql.utils import build_patterns


class Coordinates(Object):
    fields = {"lat": IntegerType(), "lng": IntegerType()}


class Location(Object):
    fields = {"name": StringType(), "coords": ObjectType(Coordinates)}


def echo(request, params, **kwargs):
    return params["loc"]


actions = {
    "echo":
    Action(parameters={"loc": ObjectType(Location)},
           return_value=ObjectType(Location),
           exec_fn=echo)
}

schema = generate(GraphQLAdapter, actions)
patterns = build_patterns("api/", schema)
예제 #6
0
def non_null(request, params, **kwargs):
    return [i for i in range(10)]


def null(request, params, **kwargs):
    return None


def list_non_null_elem_null(request, params, **kwargs):
    return [1, 2, 3, None, None, None, 7, 8, 9]


actions = {
    "getNonNull":
    Action(return_value=PlainListType(IntegerType()), exec_fn=non_null),
    "getNull":
    Action(return_value=PlainListType(IntegerType(nullable=True),
                                      nullable=True),
           exec_fn=null),
    "getListNullElemNonNull":
    Action(return_value=PlainListType(IntegerType(), nullable=True),
           exec_fn=null),
    "getListNonNullElemNull":
    Action(return_value=PlainListType(IntegerType(nullable=True)),
           exec_fn=list_non_null_elem_null),
}

schema = generate(GraphQLAdapter, actions)
patterns = build_patterns("api/", schema)
예제 #7
0
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)
예제 #8
0
    }


class PersonList(Object):
    fields = {
        "count":
        IntegerType(),
        "records":
        PlainListType(ObjectType(Person),
                      parameters={
                          "limit": IntegerType(nullable=True, default=20),
                          "offset": IntegerType(nullable=True, default=0),
                      },
                      resolver=resolve)
    }


def get(request, params, **kwargs):
    return AttrDict(count=len(params["input"]), records=params["input"])


actions = {
    "get":
    Action(parameters={"input": PlainListType(ObjectType(Person))},
           return_value=ObjectType(PersonList),
           exec_fn=get)
}

schema = generate(GraphQLAdapter, actions)
patterns = build_patterns("api/", schema)
예제 #9
0
        "time": params.get("time"),
        "datetime": params.get("datetime")
    }


class TestObject(Object):
    fields = {
        "date": DateType(),
        "time": TimeType(),
        "datetime": DateTimeType(),
    }


actions = {
    "getObject":
    Action(return_value=ObjectType(TestObject), exec_fn=get_object),
    "getDate":
    Action(return_value=DateType(), exec_fn=get_date),
    "getTime":
    Action(return_value=TimeType(), exec_fn=get_time),
    "getDatetime":
    Action(return_value=DateTimeType(), exec_fn=get_datetime),
    "echo":
    Action(parameters={
        "date": DateType(),
        "time": TimeType(),
        "datetime": DateTimeType()
    },
           return_value=ObjectType(TestObject),
           exec_fn=echo),
}
예제 #10
0
    if "id" in params:
        return "{}.{}".format(params["id"]["int1"], params["id"]["int2"])
    return "no params passed"


class TestObject(Object):
    fields = {
        "int1": IntegerType(),
        "int2": IntegerType(),
    }


actions = {
    "get":
    Action(parameters={"id": ObjectType(TestObject)},
           return_value=StringType(),
           exec_fn=get),
    "getNull":
    Action(parameters={"id": ObjectType(TestObject, nullable=True)},
           return_value=StringType(),
           exec_fn=get),
    "getNullDefault":
    Action(parameters={
        "id":
        ObjectType(TestObject, nullable=True, default={
            "int1": 10,
            "int2": 20
        })
    },
           return_value=StringType(),
           exec_fn=get)
예제 #11
0
def build_action_info(actions):
    return Action(return_value=PlainListType(ObjectType(ActionInfo)),
                  exec_fn=build_action_info_fn(actions))
예제 #12
0
def build_type_info():
    return Action(return_value=PlainListType(ObjectType(TypeInfo)),
                  exec_fn=type_info)
예제 #13
0
def build_object_info():
    return Action(return_value=PlainListType(ObjectType(ObjectInfo)),
                  exec_fn=object_info)
예제 #14
0
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 IntegerType, PlainListType

from simple_api.adapters.graphql.utils import build_patterns


def plus_one(request, params, **kwargs):
    return [i + 1 for i in params["list"]]


actions = {
    "plusOne":
    Action(parameters={"list": PlainListType(IntegerType())},
           return_value=PlainListType(IntegerType()),
           exec_fn=plus_one),
}

schema = generate(GraphQLAdapter, actions)
patterns = build_patterns("api/", schema)
예제 #15
0
    }


class PersonList(Object):
    output_fields = {
        "count": IntegerType(),
    }
    fields = {
        "records": PlainListType(
            ObjectType(Person),
            parameters={
                "limit": IntegerType(nullable=True, default=20),
                "offset": IntegerType(nullable=True, default=0),
            },
            resolver=resolve
        )
    }


def get(request, params, **kwargs):
    return AttrDict(count=len(params["input"]), records=params["input"])


actions = {
    "get": Action(parameters={"input": ObjectType(PersonList)}, return_value=ObjectType(PersonList), exec_fn=get)
}


schema = generate(GraphQLAdapter, actions)
patterns = build_patterns("api/", schema)
예제 #16
0
from simple_api.object.datatypes import IntegerType, ObjectType
from simple_api.object.object import Object
from simple_api.utils import AttrDict

from simple_api.adapters.graphql.utils import build_patterns


def get(request, params, **kwargs):
    return AttrDict(number=20, number_def=5)


def get_number(request, parent_val, params, **kwargs):
    return params.get("num") or parent_val


class TestObject(Object):
    fields = {
        "number":
        IntegerType(parameters={"num": IntegerType(nullable=True)},
                    resolver=get_number),
        "number_def":
        IntegerType(parameters={"num": IntegerType(nullable=True, default=5)},
                    resolver=get_number),
    }


actions = {"get": Action(return_value=ObjectType(TestObject), exec_fn=get)}

schema = generate(GraphQLAdapter, actions)
patterns = build_patterns("api/", schema)
예제 #17
0
    return [
        {
            "int1": 0,
            "int2": 10
        },
        None,
        {
            "int1": 2,
            "int2": 12
        },
    ]


actions = {
    "getNonNull":
    Action(return_value=PlainListType(ObjectType(TestObject)),
           exec_fn=non_null),
    "getNull":
    Action(return_value=PlainListType(ObjectType(TestObject, nullable=True),
                                      nullable=True),
           exec_fn=null),
    "getListNullElemNonNull":
    Action(return_value=PlainListType(ObjectType(TestObject), nullable=True),
           exec_fn=null),
    "getListNonNullElemNull":
    Action(return_value=PlainListType(ObjectType(TestObject, nullable=True)),
           exec_fn=list_non_null_elem_null),
}

schema = generate(GraphQLAdapter, actions)
patterns = build_patterns("api/", schema)
예제 #18
0
from simple_api.django_object.django_object import DjangoObject
from simple_api.object.actions import Action
from simple_api.object.datatypes import StringType

from simple_api.adapters.graphql.utils import build_patterns_w

from .models import Book as BookModel, Bookmark as BookmarkModel


class Book(DjangoObject):
    model = BookModel


class Bookmark(DjangoObject):
    model = BookmarkModel


def ping(**kwargs):
    return "Action run"


actions = {
    "Heavy_Action":
    Action(return_value=StringType(), exec_fn=ping, action_weight=100001),
    "Light_Action":
    Action(return_value=StringType(), exec_fn=ping, action_weight=1)
}

schema, weight_schema = generate_w(GraphQLAdapter, actions)
patterns = build_patterns_w("api/", schema, weight_schema)
예제 #19
0
    return parent_val[params["offset"]:(params["offset"] + params["limit"])]


class IntList(Object):
    fields = {
        "count":
        IntegerType(),
        "records":
        PlainListType(IntegerType(),
                      parameters={
                          "limit": IntegerType(nullable=True, default=20),
                          "offset": IntegerType(nullable=True, default=0),
                      },
                      resolver=resolve)
    }


def get(request, params, **kwargs):
    return AttrDict(count=len(params["input"]), records=params["input"])


actions = {
    "get":
    Action(parameters={"input": PlainListType(IntegerType())},
           return_value=ObjectType(IntList),
           exec_fn=get)
}

schema = generate(GraphQLAdapter, actions)
patterns = build_patterns("api/", schema)
예제 #20
0
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 ObjectType, DurationType
from simple_api.object.object import Object

from simple_api.adapters.graphql.utils import build_patterns


def echo(request, params, **kwargs):
    return params["in"]


class TestObject(Object):
    fields = {"duration": DurationType()}


actions = {
    "get":
    Action(return_value=DurationType(),
           parameters={"in": DurationType()},
           exec_fn=echo),
    "getObject":
    Action(return_value=ObjectType(TestObject),
           parameters={"in": ObjectType(TestObject)},
           exec_fn=echo)
}

schema = generate(GraphQLAdapter, actions)
patterns = build_patterns("api/", schema)
예제 #21
0
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),
    "deny":
    Action(return_value=BooleanType(),
           exec_fn=lambda **kwargs: True,
           permissions=AllowNone),
    "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)
예제 #22
0
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 ObjectType
from simple_api.object.object import Object

from simple_api.adapters.graphql.utils import build_patterns


def get(request, params, **kwargs):
    return None


class TestObject(Object):
    fields = {
        "self": ObjectType("self", nullable=True)
    }


actions = {
    "get": Action(return_value=ObjectType(TestObject, nullable=True), exec_fn=get)
}

schema = generate(GraphQLAdapter, actions)
patterns = build_patterns("api/", schema)
예제 #23
0
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 ObjectType

from simple_api.adapters.graphql.utils import build_patterns

from .b import get


actions = {
    "get": Action(return_value=ObjectType("C"), exec_fn=get)
}


schema = generate(GraphQLAdapter, actions)
patterns = build_patterns("api/", schema)
예제 #24
0
from simple_api.adapters.graphql.utils import build_patterns


class TestObject(Object):
    fields = {
        "field": IntegerType(),
        "nullable_if_input_field": IntegerType(nullable_if_input=True),
    }
    input_fields = {
        "only_input_field": IntegerType()
    }
    output_fields = {
        "only_output_field": IntegerType(nullable=True)
    }


def get(request, params, **kwargs):
    return {
        "field": 1,
        "nullable_if_input_field": 2
    }


actions = {
    "get": Action(parameters={"in": ObjectType(TestObject)}, return_value=ObjectType(TestObject), exec_fn=get)
}


schema = generate(GraphQLAdapter, actions)
patterns = build_patterns("api/", schema)