Passed
Push — main ( 5c09fb...a5c77e )
by Eran
01:21
created

graphinate.server.openapi_schema()   A

Complexity

Conditions 1

Size

Total Lines 55
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 32
nop 1
dl 0
loc 55
rs 9.112
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
import contextlib
2
import webbrowser
3
4
import strawberry
5
from starlette.applications import Starlette
6
from starlette.schemas import SchemaGenerator
7
from strawberry.asgi import GraphQL
8
from strawberry.extensions.tracing import OpenTelemetryExtension
9
10
from .starlette import routes
11
12
DEFAULT_PORT: int = 8072
13
14
15
def openapi_schema(request):
16
    schema = SchemaGenerator(
17
        {
18
            "openapi": "3.0.0",
19
            "info": {"title": "Graphinate API", "version": "1.0"},
20
            "paths": {
21
                "/graphql": {
22
                    "get": {
23
                        "responses": {
24
                            200: {
25
                                "description": "GraphQL"
26
                            }
27
                        }
28
                    }
29
                },
30
                "/graphiql": {
31
                    "get": {
32
                        "responses": {
33
                            200: {
34
                                "description": "GraphiQL UI."
35
                            }
36
                        }
37
                    }
38
                },
39
                "/metrics": {
40
                    "get": {
41
                        "responses": {
42
                            200: {
43
                                "description": "Prometheus metrics."
44
                            }
45
                        }
46
                    }
47
                },
48
                "/viewer": {
49
                    "get": {
50
                        "responses": {
51
                            200: {
52
                                "description": "3D Force-Directed Graph Viewer"
53
                            }
54
                        }
55
                    }
56
                },
57
                "/voyager": {
58
                    "get": {
59
                        "responses": {
60
                            200: {
61
                                "description": "Voyager GraphQL Schema Viewer"
62
                            }
63
                        }
64
                    }
65
                }
66
            }
67
        }
68
    )
69
    return schema.OpenAPIResponse(request=request)
70
71
72
def graphql(graphql_schema: strawberry.Schema, port: int = DEFAULT_PORT):
73
    """
74
75
    Args:
76
        graphql_schema:
77
        port:
78
79
    Returns:
80
81
    """
82
    graphql_schema.extensions.append(OpenTelemetryExtension)
83
84
    @contextlib.asynccontextmanager
85
    async def lifespan(app: Starlette):  # pragma: no cover
86
        def open_url():
87
            for app_name in ('voyager', 'graphiql', 'viewer'):
88
                webbrowser.open(f'http://localhost:{port}/{app_name}')
89
90
        open_url()
91
        yield
92
93
    graphql_app: GraphQL = GraphQL(graphql_schema)
94
    app = Starlette(
95
        lifespan=lifespan,
96
        routes=routes()
97
    )
98
    app.add_route("/graphql", graphql_app)
99
    app.add_websocket_route("/graphql", graphql_app)
100
101
    from starlette_prometheus import PrometheusMiddleware, metrics
102
    app.add_middleware(PrometheusMiddleware)
103
    app.add_route("/metrics", metrics)
104
105
    app.add_route("/schema", route=openapi_schema, include_in_schema=False)
106
107
    import uvicorn
108
    uvicorn.run(app, host='0.0.0.0', port=port)
109