Пример #1
0
def create_app(connection_str, sqla_models) -> Starlette:
    """Create the Starlette app"""

    async_connection = connection_str.replace("postgresql://",
                                              "postgresql+asyncpg://")
    engine = create_async_engine(async_connection)

    # Convert sqla models to graphql schema
    gql_schema = sqla_models_to_graphql_schema(sqla_models)

    # Build the Starlette GraphQL Route
    graphql_route = get_graphql_route(gql_schema=gql_schema, engine=engine)

    # Build the Starlette GraphiQL Route and StaticFiles
    graphiql_route = get_graphiql_route()

    # Instantiate the Starlette app
    _app = Starlette(
        routes=[graphql_route, graphiql_route],
        exception_handlers={HTTPException: http_exception},
        on_startup=[engine.connect],
        on_shutdown=[engine.dispose],
    )

    return _app
Пример #2
0
def dump_schema(connection, schema, out_file):
    """Dump the GraphQL Schema to stdout or file"""
    from nebulo.gql.sqla_to_gql import sqla_models_to_graphql_schema
    from nebulo.sql.reflection.manager import reflect_sqla_models

    engine = create_engine(connection)
    sqla_models, sql_functions = reflect_sqla_models(engine, schema=schema)
    schema = sqla_models_to_graphql_schema(sqla_models, sql_functions)
    schema_str = print_schema(schema)
    click.echo(schema_str, file=out_file)
Пример #3
0
def create_app(
    connection: str,
    schema: str = "public",
    jwt_identifier: Optional[str] = None,
    jwt_secret: Optional[str] = None,
    default_role: Optional[str] = None,
) -> Starlette:
    """Instantiate the Starlette app"""

    if not (jwt_identifier is not None) == (jwt_secret is not None):
        raise Exception(
            "jwt_token_identifier and jwt_secret must be provided together")

    database = Database(connection)
    # Reflect database to sqla models
    sqla_engine = create_engine(connection)
    sqla_models, sql_functions = reflect_sqla_models(engine=sqla_engine,
                                                     schema=schema)

    # Convert sqla models to graphql schema
    gql_schema = sqla_models_to_graphql_schema(
        sqla_models,
        sql_functions,
        jwt_identifier=jwt_identifier,
        jwt_secret=jwt_secret,
    )

    graphql_path = "/"

    graphql_route = get_graphql_route(
        gql_schema=gql_schema,
        database=database,
        jwt_secret=jwt_secret,
        default_role=default_role,
        path=graphql_path,
        name="graphql",
    )

    graphiql_route = get_graphiql_route(graphiql_path="/graphiql",
                                        graphql_path=graphql_path,
                                        name="graphiql")

    _app = Starlette(
        routes=[graphql_route, graphiql_route],
        middleware=[Middleware(CORSMiddleware, allow_origins=["*"])],
        exception_handlers={HTTPException: http_exception},
        on_startup=[database.connect],
        on_shutdown=[database.disconnect],
    )

    return _app
Пример #4
0
def create_app(connection_str, sqla_models) -> Starlette:
    """Create the Starlette app"""

    database = Database(connection_str)

    # Convert sqla models to graphql schema
    gql_schema = sqla_models_to_graphql_schema(sqla_models)

    # Build the Starlette GraphQL Route
    graphql_route = get_graphql_route(gql_schema=gql_schema, database=database)

    # Build the Starlette GraphiQL Route and StaticFiles
    graphiql_route = get_graphiql_route()

    # Instantiate the Starlette app
    _app = Starlette(
        routes=[graphql_route, graphiql_route],
        exception_handlers={HTTPException: http_exception},
        on_startup=[database.connect],
        on_shutdown=[database.disconnect],
    )

    return _app
Пример #5
0
 def build(sql: str):
     session.execute(sql)
     session.commit()
     tables, functions = reflect_sqla_models(engine, schema="public")
     schema = sqla_models_to_graphql_schema(tables, functions)
     return schema