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
class ObjectInfo(Object): fields = { "name": StringType(), "pk_field": StringType(nullable=True), "actions": PlainListType(ObjectType(ActionInfo)), } 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)
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 generate_filters(cls): filters = OrderedDict() for name, field in cls.out_fields.items(): all_filters = determine_filters_for_type(field, name) determined_filters = determine_items( all_filters, field.kwargs.get("only_filters", None), field.kwargs.get("exclude_filters", None), field.kwargs.get("custom_filters", None), all_on_none=False) filters.update(determined_filters) filters["ordering"] = PlainListType(StringType(), nullable=True) return filters
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 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 inject_references(mcs, cls): for field in { **cls.fields, **cls.input_fields, **cls.output_fields }.values(): field.set_parent_class(cls) for action_name, action in cls.actions.items(): action.set_parent_class(cls) action.set_name(action_name) if not getattr(cls, "hidden", False): cls.output_fields["__actions"] = PlainListType( ObjectType("simple_api.object.meta_types.ActionInfo"), resolver=build_actions_resolver(cls))
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__)
} 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)
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)
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)
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)
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)
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)
def build_action_info(actions): return Action(return_value=PlainListType(ObjectType(ActionInfo)), exec_fn=build_action_info_fn(actions))
def build_type_info(): return Action(return_value=PlainListType(ObjectType(TypeInfo)), exec_fn=type_info)
def build_object_info(): return Action(return_value=PlainListType(ObjectType(ObjectInfo)), exec_fn=object_info)
class TypeInfo(Object): fields = { "typename": StringType(), "fields": PlainListType(ObjectType(FieldInfo)) } hidden = True