Skip to main content

Dishka integration for Strawberry

Project description

dishka-strawberry

dishka-strawberry will help you use Dishka with Strawberry. It features:

  • automatic injection of dependencies into GraphQL resolver functions
  • integration with FastAPI through strawberry.fastapi.GraphQLRouter

Installation

Install using pip:

pip install dishka-strawberry

Or using uv:

uv add dishka-strawberry

How to use

1. Import

from dishka_strawberry.fastapi import (
    FromDishka,
    inject,
)
from dishka.integrations.fastapi import (
    FastapiProvider,
    setup_dishka,
)
from dishka import make_async_container, Provider, provide, Scope

2. Create provider

You can use FastapiProvider as a base class if you need to access fastapi.Request in your providers

class AppProvider(FastapiProvider):
    @provide(scope=Scope.REQUEST)
    def create_message(self) -> Message:
         return Message("42")

3. Mark resolver parameters

Mark resolver parameters which are to be injected with FromDishka[] and decorate them using @inject

@strawberry.type
class Query:
    @strawberry.field
    @inject
    def answer(self, message: FromDishka[Message]) -> MessageGQL:
        return MessageGQL(message=message)

4. Create Strawberry schema

Create Strawberry schema and integrate it with FastAPI using GraphQLRouter

schema = strawberry.Schema(query=Query)
graphql_router = GraphQLRouter(schema)

app = FastAPI()
app.include_router(graphql_router, prefix="/graphql")

5. Setup dishka integration

Setup dishka integration for FastAPI (which will handle the container lifecycle for GraphQL requests as well)

container = make_async_container(AppProvider())
setup_dishka(container=container, app=app)

Contributing

Contributors are welcome! If you'd like to contribute to this project, please feel free to open an issue or submit a pull request.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

dishka_strawberry-0.2.0.tar.gz (64.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

dishka_strawberry-0.2.0-py3-none-any.whl (7.6 kB view details)

Uploaded Python 3

File details

Details for the file dishka_strawberry-0.2.0.tar.gz.

File metadata

  • Download URL: dishka_strawberry-0.2.0.tar.gz
  • Upload date:
  • Size: 64.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.9.9 {"installer":{"name":"uv","version":"0.9.9"},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for dishka_strawberry-0.2.0.tar.gz
Algorithm Hash digest
SHA256 1616ab1578f67df28355d57c64e31b665e010f0add961f18ce34a4e7aefe9553
MD5 6586558aca33ca12ba13f60b9c09a98e
BLAKE2b-256 9cb76e10e88bf4ae6fdd0a5ebca33455eb24fb31d6dfb1fb4b25d989360ef99e

See more details on using hashes here.

File details

Details for the file dishka_strawberry-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: dishka_strawberry-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 7.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.9.9 {"installer":{"name":"uv","version":"0.9.9"},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for dishka_strawberry-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 fbc96145e10bbebf1ca90698f79293ef96fd019bf90fa5604dd6226ae4fcc19a
MD5 2074bb6578635460d836b087e003bebc
BLAKE2b-256 3c3d59ec153a5963a1a6d8f5e68eedac5a56a9bb0dead86a670ce18b4de7974e

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page