Ejemplo n.º 1
0
 def build_attrs():
     edge = edge_factory(sqla_model)
     return {
         "edges": Field(NonNull(List(NonNull(edge))), resolve=default_resolver),
         "pageInfo": Field(NonNull(PageInfo), resolve=default_resolver),
         "totalCount": Field(NonNull(Int), resolve=default_resolver),
     }
Ejemplo n.º 2
0
    def build_attrs():
        attrs = {}

        # Override id to relay standard
        attrs["nodeId"] = Field(NonNull(ID), resolve=default_resolver)

        for column in get_columns(sqla_model):
            if not Config.exclude_read(column):
                key = Config.column_name_mapper(column)
                attrs[key] = convert_column(column)

        for relationship in get_relationships(sqla_model):
            direction = relationship.direction
            to_sqla_model = relationship.mapper.class_
            relationship_is_nullable = is_nullable(relationship)

            # Name of the attribute on the model
            attr_key = Config.relationship_name_mapper(relationship)

            # If this model has 1 counterpart, do not use a list
            if direction == interfaces.MANYTOONE:
                _type = table_factory(to_sqla_model)
                _type = NonNull(_type) if not relationship_is_nullable else _type
                attrs[attr_key] = Field(_type, resolve=default_resolver)

            # Otherwise, set it up as a connection
            elif direction in (interfaces.ONETOMANY, interfaces.MANYTOMANY):
                connection_field = connection_field_factory(
                    to_sqla_model, resolver=default_resolver, not_null=relationship_is_nullable
                )
                attrs[attr_key] = connection_field

        return attrs
Ejemplo n.º 3
0
def update_input_type_factory(sqla_model: TableProtocol) -> InputObjectType:
    """UpdateAccountInput!"""
    relevant_type_name = Config.table_type_name_mapper(sqla_model)
    result_name = f"Update{relevant_type_name}Input"

    input_object_name = Config.table_name_mapper(sqla_model)

    attrs = {
        "nodeId": NonNull(ID),
        "clientMutationId": String,
        input_object_name: NonNull(patch_type_factory(sqla_model)),
    }
    return UpdateInputType(result_name, attrs, description=f"All input for the create {relevant_type_name} mutation.")
Ejemplo n.º 4
0
def mutable_function_entrypoint_factory(
        sql_function: SQLFunction,
        resolver: typing.Callable,
        jwt_secret: typing.Optional[str] = None) -> typing.Dict[str, Field]:
    """authenticate"""
    # TODO(OR): need seperate mapper
    function_name = Config.function_name_mapper(sql_function)

    if sql_function.is_immutable:
        raise Exception(
            f"SQLFunction {sql_function.name} is immutable, use immutable_function_entrypoint"
        )

    args = {"input": NonNull(function_input_type_factory(sql_function))}

    if jwt_secret is not None:
        payload = jwt_function_payload_factory(sql_function, jwt_secret)
    else:
        payload = function_payload_factory(sql_function)

    return {
        function_name:
        Field(payload,
              args=args,
              resolve=resolver,
              description=f"Call the function {function_name}.")
    }
Ejemplo n.º 5
0
def immutable_function_entrypoint_factory(
        sql_function: SQLFunction,
        resolver: typing.Callable) -> typing.Dict[str, Field]:
    """authenticate"""
    # TODO(OR): need seperate mapper
    function_name = Config.function_name_mapper(sql_function)

    if not sql_function.is_immutable:
        raise Exception(
            f"SQLFunction {sql_function.name} is not immutable, use mutable_function_entrypoint"
        )

    gql_args = {(arg_name if arg_name else f"param{ix}"):
                Argument(NonNull(convert_type(arg_sqla_type)))
                for ix, (arg_name, arg_sqla_type) in enumerate(
                    zip(sql_function.arg_names, sql_function.arg_sqla_types))}

    return_type = convert_type(sql_function.return_sqla_type)
    return_type.sql_function = sql_function
    return_field = Field(return_type,
                         args=gql_args,
                         resolve=resolver,
                         description="")

    return {function_name: return_field}
Ejemplo n.º 6
0
def create_payload_factory(sqla_model: TableProtocol) -> CreatePayloadType:
    """CreateAccountPayload"""
    from nebulo.gql.convert.table import table_factory

    relevant_type_name = Config.table_type_name_mapper(sqla_model)
    relevant_attr_name = Config.table_name_mapper(sqla_model)
    result_name = f"Create{relevant_type_name}Payload"

    attrs = {
        "clientMutationId":
        Field(String, resolve=default_resolver),
        "nodeId":
        ID,
        relevant_attr_name:
        Field(
            NonNull(table_factory(sqla_model)),
            description=
            f"The {relevant_type_name} that was created by this mutation.",
            resolve=default_resolver,
        ),
    }

    return CreatePayloadType(
        result_name,
        attrs,
        description=f"The output of our create {relevant_type_name} mutation",
        sqla_model=sqla_model)
Ejemplo n.º 7
0
def table_field_factory(sqla_model: TableProtocol, resolver) -> Field:
    relevant_type_name = Config.table_type_name_mapper(sqla_model)
    node = table_factory(sqla_model)
    return Field(
        node,
        args={"nodeId": Argument(NonNull(ID))},
        resolve=resolver,
        description=f"Reads a single {relevant_type_name} using its globally unique ID",
    )
Ejemplo n.º 8
0
def create_entrypoint_factory(sqla_model: TableProtocol, resolver) -> Field:
    """createAccount"""
    relevant_type_name = Config.table_type_name_mapper(sqla_model)
    name = f"create{relevant_type_name}"
    args = {"input": NonNull(create_input_type_factory(sqla_model))}
    payload = create_payload_factory(sqla_model)
    return {
        name:
        Field(payload,
              args=args,
              resolve=resolver,
              description=f"Creates a single {relevant_type_name}.")
    }
Ejemplo n.º 9
0
def update_entrypoint_factory(sqla_model: TableProtocol, resolver) -> t.Dict[str, Field]:
    """updateAccount"""
    relevant_type_name = Config.table_type_name_mapper(sqla_model)
    name = f"update{relevant_type_name}"
    args = {"input": NonNull(update_input_type_factory(sqla_model))}
    payload = update_payload_factory(sqla_model)
    return {
        name: Field(
            payload,
            args=args,
            resolve=resolver,
            description=f"Updates a single {relevant_type_name} using its globally unique id and a patch.",
        )
    }
Ejemplo n.º 10
0
def connection_field_factory(sqla_model: TableProtocol, resolver, not_null=False) -> Field:
    relevant_type_name = Config.table_type_name_mapper(sqla_model)
    connection = connection_factory(sqla_model)
    condition = condition_factory(sqla_model)
    args = {
        "first": Argument(Int, description="", out_name=None),
        "last": Argument(Int),
        "before": Argument(Cursor),
        "after": Argument(Cursor),
        "condition": Argument(condition),
    }
    return Field(
        NonNull(connection) if not_null else connection,
        args=args,
        resolve=resolver,
        description=f"Reads and enables pagination through a set of {relevant_type_name}",
    )
Ejemplo n.º 11
0
def function_input_type_factory(
        sql_function: SQLFunction) -> FunctionInputType:
    """AuthenticateInput!"""
    function_name = Config.function_type_name_mapper(sql_function)
    result_name = f"{function_name}Input"

    function_args = {
        (arg_name if arg_name else f"param{ix}"):
        NonNull(convert_input_type(arg_sqla_type))
        for ix, (arg_name, arg_sqla_type) in enumerate(
            zip(sql_function.arg_names, sql_function.arg_sqla_types))
    }

    attrs = {"clientMutationId": String, **function_args}
    return FunctionInputType(
        result_name,
        attrs,
        description=f"All input for the {function_name} mutation.")
Ejemplo n.º 12
0
        vals.extend([literal_string(col_name), query_elem.c[col_name]])

    return func.jsonb_build_object(
        literal_string("table_name"),
        literal_string(table_name),
        literal_string("values"),
        func.jsonb_build_object(*vals),
    )


ID = ScalarType(
    "ID",
    description="Unique ID for node",
    serialize=serialize,
    parse_value=NodeIdStructure.deserialize,
    parse_literal=lambda x: NodeIdStructure.deserialize(x.value),
)

NodeInterface = InterfaceType(
    "NodeInterface",
    description="An object with a nodeId",
    fields={
        "nodeId":
        Field(NonNull(ID),
              description="The global id of the object.",
              resolve=None)
    },
    # Maybe not necessary
    resolve_type=lambda *args, **kwargs: None,
)
Ejemplo n.º 13
0
def convert_column(column: Column) -> Field:
    """Converts a sqlalchemy column into a graphql field or input field"""
    gql_type = convert_type(column.type)
    notnull = not column.nullable
    return_type = NonNull(gql_type) if notnull else gql_type
    return Field(return_type, resolve=default_resolver)
Ejemplo n.º 14
0
from nebulo.gql.alias import Boolean, Field, NonNull, ObjectType
from nebulo.gql.relay.cursor import Cursor

PageInfo = ObjectType(
    name="PageInfo",
    fields={
        "hasNextPage": Field(NonNull(Boolean)),
        "hasPreviousPage": Field(NonNull(Boolean)),
        "startCursor": Field(Cursor),
        "endCursor": Field(Cursor),
    },
    description="",
)