def server(schema, host, port, app_dir): sys.path.insert(0, app_dir) try: import starlette # noqa: F401 import uvicorn except ImportError: message = ( "The debug server requires additional packages, install them by running:\n" "pip install strawberry-graphql[debug-server]") raise click.ClickException(message) try: schema_symbol = import_module_symbol(schema, default_symbol_name="schema") except (ImportError, AttributeError) as exc: message = str(exc) raise click.BadArgumentUsage(message) if not isinstance(schema_symbol, Schema): message = "The `schema` must be an instance of strawberry.Schema" raise click.BadArgumentUsage(message) os.environ[DEBUG_SERVER_SCHEMA_ENV_VAR_KEY] = schema app = "strawberry.cli.debug_server:app" print(f"Running strawberry on http://{host}:{port}/graphql 🍓") uvicorn.run(app, host=host, port=port, log_level="error", reload=True, reload_dirs=[app_dir])
def server(schema, host, port, app_dir): sys.path.insert(0, app_dir) try: schema_symbol = import_module_symbol(schema, default_symbol_name="schema") except (ImportError, AttributeError) as exc: message = str(exc) raise click.BadArgumentUsage(message) if not isinstance(schema_symbol, Schema): message = "The `schema` must be an instance of strawberry.Schema" raise click.BadArgumentUsage(message) reloader = hupper.start_reloader("strawberry.cli.run", verbose=False) schema_module = importlib.import_module(schema_symbol.__module__) reloader.watch_files([schema_module.__file__]) app = Starlette(debug=True) app.add_middleware(CORSMiddleware, allow_headers=["*"], allow_origins=["*"], allow_methods=["*"]) graphql_app = GraphQL(schema_symbol, debug=True) paths = ["/", "/graphql"] for path in paths: app.add_route(path, graphql_app) app.add_websocket_route(path, graphql_app) print(f"Running strawberry on http://{host}:{port}/ 🍓") uvicorn.run(app, host=host, port=port, log_level="error")
def export_schema(schema: str): try: schema_symbol = import_module_symbol(schema, default_symbol_name="schema") except (ImportError, AttributeError) as exc: message = str(exc) raise click.BadArgumentUsage(message) if not isinstance(schema_symbol, Schema): message = "The `schema` must be an instance of strawberry.Schema" raise click.BadArgumentUsage(message) print(print_schema(schema_symbol))
def test_symbol_import(): selector = "tests.fixtures.sample_package.sample_module:schema" schema_symbol = import_module_symbol(selector) assert schema_symbol == schema
def test_invalid_symbol_import(): selector = "tests.fixtures.sample_package.sample_module:not.existing.symbol" with pytest.raises(AttributeError): import_module_symbol(selector)
def test_invalid_module_import(): selector = "not.existing.module:schema" with pytest.raises(ImportError): import_module_symbol(selector)
def test_not_specifying_a_symbol(): selector = "tests.fixtures.sample_package.sample_module" with pytest.raises(ValueError) as exc: import_module_symbol(selector) assert "Selector does not include a symbol name" in str(exc.value)
def test_default_symbol_import(): selector = "tests.fixtures.sample_package.sample_module" schema_symbol = import_module_symbol(selector, default_symbol_name="schema") assert schema_symbol == schema
import os from starlette.applications import Starlette from starlette.middleware.cors import CORSMiddleware from strawberry import Schema from strawberry.asgi import GraphQL from strawberry.cli.constants import DEBUG_SERVER_SCHEMA_ENV_VAR_KEY from strawberry.utils.importer import import_module_symbol app = Starlette(debug=True) app.add_middleware( CORSMiddleware, allow_headers=["*"], allow_origins=["*"], allow_methods=["*"] ) schema_import_string = os.environ[DEBUG_SERVER_SCHEMA_ENV_VAR_KEY] schema_symbol = import_module_symbol(schema_import_string, default_symbol_name="schema") assert isinstance(schema_symbol, Schema) graphql_app = GraphQL(schema_symbol, debug=True) paths = ["/", "/graphql"] for path in paths: app.add_route(path, graphql_app) app.add_websocket_route(path, graphql_app)