Example #1
0
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),
    }
Example #2
0
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)
    }
Example #3
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)
    }
Example #4
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)}
Example #5
0
class IntList(Object):
    fields = {
        "count": IntegerType(),
        "all_records": PlainListType(IntegerType()),
        "records": ObjectType(
            "self",
            parameters={
                "limit": IntegerType(nullable=True, default=20),
                "offset": IntegerType(nullable=True, default=0),
            },
            resolver=resolve
        )
    }
Example #6
0
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)
Example #7
0
def create_associated_list_type(cls):
    def resolve_pagination(request, parent_val, params, **kwargs):
        return parent_val[params["offset"]:(params["offset"] +
                                            params["limit"])]

    attrs = {
        "fields": {
            "count":
            IntegerType(),
            "data":
            PlainListType(ObjectType(cls),
                          parameters={
                              "limit":
                              IntegerType(nullable=True,
                                          default=DEFAULT_LIMIT),
                              "offset":
                              IntegerType(nullable=True, default=0),
                          },
                          resolver=resolve_pagination)
        },
        "hidden": True,
    }
    ObjectMeta(cls.__name__ + "List", (Object, ), attrs, module=cls.__module__)
Example #8
0
class Person(Object):
    fields = {
        "name": StringType(),
        "age": IntegerType(),
    }
Example #9
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)
Example #10
0
class TestObject(Object):
    fields = {
        "int1": IntegerType(),
        "int2": IntegerType(),
    }
Example #11
0
class Coordinates(Object):
    fields = {"lat": IntegerType(), "lng": IntegerType()}
Example #12
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)
Example #13
0
def resolve(request, parent_val, params, **kwargs):
    res = parent_val[params["offset"]:(params["offset"]+params["limit"])]
    return AttrDict(count=len(res), all_records=res, records=res)


class IntList(Object):
    fields = {
        "count": IntegerType(),
        "all_records": PlainListType(IntegerType()),
        "records": ObjectType(
            "self",
            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"]), all_records=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)
Example #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)