From 7a20b9775c33ee03dd574dc3f0aac9efc6f35d4f Mon Sep 17 00:00:00 2001 From: Ethan Henderson Date: Wed, 7 Aug 2024 21:11:26 +0100 Subject: [PATCH] Add RELEASE.md --- RELEASE.md | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 RELEASE.md diff --git a/RELEASE.md b/RELEASE.md new file mode 100644 index 0000000000..e2d84438d5 --- /dev/null +++ b/RELEASE.md @@ -0,0 +1,43 @@ +Release type: minor + +You can now configure your schemas to provide a custom subclass of +`strawberry.types.Info` to your types and queries. + +```py +import strawberry +from strawberry.schema.config import StrawberryConfig + +from .models import ProductModel + + +class CustomInfo(strawberry.Info): + def is_selected(field: str) -> bool: + """Check if the field is selected on the top-level of the query.""" + return field in [sel.name for sel in info.selected_fields] + + +@strawberry.type +class Product: + id: strawberry.ID + orders: list[Order] + + +@strawberry.type +class Query: + @strawberry.field + def product(self, id: strawberry.ID, info: CustomInfo) -> Product: + kwargs = {"id": id} + + if info.is_selected("orders"): + # Do some expensive operation here that we wouldn't want to + # do if the field wasn't selected. + kwargs["orders"] = ... + + return Product(**kwargs) + + +schema = strawberry.Schema( + Query, + config=StrawberryConfig(info_class=CustomInfo), +) +```