Ejemplo n.º 1
0
    def relationship_name_mapper(relationship: RelationshipProperty) -> str:
        # Union of Mapper or ORM instance
        referred_cls = relationship.argument
        if hasattr(referred_cls, "class_"):
            referred_cls = referred_cls.class_
        elif callable(referred_cls):
            referred_cls = referred_cls()

        referred_name = get_table_name(referred_cls)
        cardinal_name = to_plural(
            referred_name) if relationship.uselist else referred_name
        camel_name = snake_to_camel(cardinal_name, upper=False)
        relationship_name = (camel_name + "By" + "And".join(
            snake_to_camel(local_col.name, upper=True) + "To" +
            snake_to_camel(remote_col.name, upper=True)
            for local_col, remote_col in relationship.local_remote_pairs))
        return relationship_name
Ejemplo n.º 2
0
def composite_input_factory(sqla_composite: SQLACompositeType) -> InputObjectType:
    name = snake_to_camel(sqla_composite.name, upper=True) + "Input"
    fields = {}

    for column in sqla_composite.columns:
        column_key = str(column.key)
        gql_key = column_key
        gql_field = convert_column_to_input(column)
        fields[gql_key] = gql_field

    return_type = InputObjectType(name, fields)
    return_type.sqla_composite = sqla_composite
    return return_type
Ejemplo n.º 3
0
def composite_factory(sqla_composite: SQLACompositeType) -> CompositeType:
    name = snake_to_camel(sqla_composite.name, upper=True)
    fields = {}

    for column in sqla_composite.columns:
        column_key = str(column.key)
        gql_key = column_key
        gql_type = convert_column(column)
        fields[gql_key] = gql_type

    return_type = CompositeType(name, fields)
    return_type.sqla_composite = sqla_composite
    return return_type
Ejemplo n.º 4
0
def sqla_models_to_graphql_schema(
    sqla_models,
    sql_functions: typing.Optional[typing.List[SQLFunction]] = None,
    jwt_identifier: typing.Optional[str] = None,
    jwt_secret: typing.Optional[str] = None,
) -> Schema:
    """Creates a GraphQL Schema from SQLA Models

    **Parameters**

    * **sqla_models**: _List[Type[SQLAModel]]_ = List of SQLAlchemy models to include in the GraphQL schema
    * **jwt_identifier**: _str_ = qualified path of SQL composite type to use encode as a JWT e.g. 'public.jwt'
    * **jwt_secret**: _str_ = Secret key used to encrypt JWT contents
    * **sql_functions** = **NOT PUBLIC API**
    """

    if sql_functions is None:
        sql_functions = []

    query_fields = {}
    mutation_fields = {}

    # Tables
    for sqla_model in sqla_models:

        if not Config.exclude_read_one(sqla_model):
            # e.g. account(nodeId: NodeID)
            single_name = snake_to_camel(get_table_name(sqla_model),
                                         upper=False)
            query_fields[single_name] = table_field_factory(
                sqla_model, resolver)

        if not Config.exclude_read_all(sqla_model):
            # e.g. allAccounts(first: Int, last: Int ....)
            connection_name = "all" + snake_to_camel(
                to_plural(get_table_name(sqla_model)), upper=True)
            query_fields[connection_name] = connection_field_factory(
                sqla_model, resolver)

        if not Config.exclude_create(sqla_model):
            # e.g. createAccount(input: CreateAccountInput)
            mutation_fields.update(
                create_entrypoint_factory(sqla_model, resolver=resolver))

        if not Config.exclude_update(sqla_model):
            # e.g. updateAccount(input: UpdateAccountInput)
            mutation_fields.update(
                update_entrypoint_factory(sqla_model, resolver=resolver))

        if not Config.exclude_delete(sqla_model):
            # e.g. deleteAccount(input: DeleteAccountInput)
            mutation_fields.update(
                delete_entrypoint_factory(sqla_model, resolver=resolver))
    # Functions
    for sql_function in sql_functions:
        if is_jwt_function(sql_function, jwt_identifier):
            mutation_fields.update(
                mutable_function_entrypoint_factory(sql_function=sql_function,
                                                    resolver=resolver,
                                                    jwt_secret=jwt_secret))
        else:

            # Immutable functions are queries
            if sql_function.is_immutable:
                query_fields.update(
                    immutable_function_entrypoint_factory(
                        sql_function=sql_function, resolver=resolver))

            # Mutable functions are mutations
            else:
                mutation_fields.update(
                    mutable_function_entrypoint_factory(
                        sql_function=sql_function, resolver=resolver))

    schema_kwargs = {
        "query": ObjectType(name="Query", fields=query_fields),
        "mutation": ObjectType(name="Mutation", fields=mutation_fields),
    }
    return Schema(**{k: v for k, v in schema_kwargs.items() if v.fields})
Ejemplo n.º 5
0
def rename_table(base, tablename: str, table: Table) -> str:
    """Produce a 'camelized' class name, e.g. 'words_and_underscores' -> 'WordsAndUnderscores'"""
    return snake_to_camel(tablename, upper=True)
Ejemplo n.º 6
0
 def enum_name_mapper(enum: Type[postgresql.base.ENUM]) -> str:
     return snake_to_camel((enum.name or "") + "_enum", upper=True)
Ejemplo n.º 7
0
 def column_name_mapper(column: Column) -> str:
     return snake_to_camel(column.name, upper=False)
Ejemplo n.º 8
0
 def table_type_name_mapper(sqla_table: TableProtocol) -> str:
     table_name = get_table_name(sqla_table)
     return snake_to_camel(table_name)
Ejemplo n.º 9
0
 def function_type_name_mapper(sql_function: SQLFunction) -> str:
     """to_upper -> ToUpper"""
     return snake_to_camel(sql_function.name, upper=True)
Ejemplo n.º 10
0
 def function_name_mapper(sql_function: SQLFunction) -> str:
     """to_upper -> toUpper"""
     return snake_to_camel(sql_function.name, upper=False)