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.1.tar.gz (64.9 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.1-py3-none-any.whl (7.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: dishka_strawberry-0.2.1.tar.gz
  • Upload date:
  • Size: 64.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.10.4 {"installer":{"name":"uv","version":"0.10.4","subcommand":["publish"]},"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.1.tar.gz
Algorithm Hash digest
SHA256 df3e2b265899ea78309e0eee0bf69aa67cece6eb08349e0a0e0722389df925a6
MD5 6ff5ec90a924f7348405301a95e85785
BLAKE2b-256 d8dc7bd4a527a597767566a6875a2f6d9987aee5b5d6935e8a432ab92f9f7dac

See more details on using hashes here.

File details

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

File metadata

  • Download URL: dishka_strawberry-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 7.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.10.4 {"installer":{"name":"uv","version":"0.10.4","subcommand":["publish"]},"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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 5b01d957c2f47476ba0ec3b6e0d4cb16cffa859e35df1c7e58eb9e267428b25b
MD5 55f248190fff7ddc7e4fa5bfb01092e1
BLAKE2b-256 6b15b98f3192005b0d91bba71e1196889c616a6f851ce0bc2621f22d1e3e8e6e

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