Starlite

Strawberry comes with an integration for Starlite by providing a make_graphql_controller function that can be used to create a GraphQL controller.

See the example below for integrating Starlite with Strawberry:

import strawberry
from starlite import Starlite
from strawberry.starlite import make_graphql_controller
 
 
@strawberry.type
class Query:
    @strawberry.field
    def hello(self) -> str:
        return "Hello World"
 
 
schema = strawberry.Schema(Query)
 
 
GraphQLController = make_graphql_controller(
    schema,
    path="/graphql",
)
 
app = Starlite(
    route_handlers=[GraphQLController],
)

Options

The make_graphql_controller function accepts the following options:

context_getter

The context_getter option allows you to provide a custom context object that can be used in your resolver. It receives a request object that can be used to extract information from the request.

import strawberry
from starlite import Request, Starlite
from strawberry.starlite import make_graphql_controller
from strawberry.types.info import Info
 
 
def custom_context_getter(request: Request):
    return {"custom": "context"}
 
 
@strawberry.type
class Query:
    @strawberry.field
    def hello(self, info: Info[object, None]) -> str:
        return info.context["custom"]
 
 
schema = strawberry.Schema(Query)
 
 
GraphQLController = make_graphql_controller(
    schema,
    path="/graphql",
    context_getter=custom_context_getter,
)
 
app = Starlite(
    route_handlers=[GraphQLController],
)

root_value_getter

The root_value_getter option allows you to provide a custom root value that can be used in your resolver. It receives a request object that can be used to extract information from the request.

import strawberry
from starlite import Request, Starlite
from strawberry.starlite import make_graphql_controller
 
 
@strawberry.type
class Query:
    example: str = "Hello World"
 
    @strawberry.field
    def hello(self) -> str:
        return self.example
 
 
def custom_get_root_value(request: Request):
    return Query()
 
 
schema = strawberry.Schema(Query)
 
 
GraphQLController = make_graphql_controller(
    schema,
    path="/graphql",
    root_value_getter=custom_get_root_value,
)
 
app = Starlite(
    route_handlers=[GraphQLController],
)
Edit this page on GitHub