@@ -6,42 +6,36 @@ title: Federation
66
77Strawberry Django works seamlessly with
88[ Strawberry's Federation support] ( https://strawberry.rocks/docs/guides/federation ) .
9- Since federation is handled at the Strawberry level, you can use all federation
10- features directly with your Django types.
9+ You can use either Strawberry's federation decorators directly or the Django-specific
10+ ` strawberry_django.federation ` module which provides auto-generated ` resolve_reference `
11+ methods.
1112
12- ## Basic Usage
13+ ## Using ` strawberry_django.federation ` (Recommended)
1314
14- Use Strawberry's federation decorators alongside ` strawberry_django ` :
15+ The ` strawberry_django.federation ` module provides Django-aware federation decorators
16+ that automatically generate ` resolve_reference ` methods for your entity types:
1517
1618``` python
1719import strawberry
1820import strawberry_django
19- from strawberry.federation.schema_directives import Key
21+ from strawberry.federation import Schema as FederationSchema
2022
2123from . import models
2224
2325
24- @strawberry_django.type (models.Product, directives = [Key( fields = " upc" ) ])
26+ @strawberry_django.federation. type (models.Product, keys = [ " upc" ])
2527class Product :
2628 upc: strawberry.auto
2729 name: strawberry.auto
2830 price: strawberry.auto
31+ # resolve_reference is automatically generated!
2932
3033
31- @strawberry_django.type (models.Review, directives = [Key( fields = " id" ) ])
34+ @strawberry_django.federation. type (models.Review, keys = [ " id" ])
3235class Review :
3336 id : strawberry.auto
3437 body: strawberry.auto
3538 product: Product
36- ```
37-
38- ## Creating a Federated Schema
39-
40- Use ` strawberry.federation.Schema ` instead of the regular ` strawberry.Schema ` :
41-
42- ``` python
43- import strawberry
44- from strawberry.federation import Schema
4539
4640
4741@strawberry.type
@@ -51,14 +45,117 @@ class Query:
5145 return models.Product.objects.all()
5246
5347
54- schema = Schema(query = Query, enable_federation_2 = True )
48+ schema = FederationSchema(query = Query)
49+ ```
50+
51+ ### Federation Parameters
52+
53+ The ` @strawberry_django.federation.type ` decorator accepts all standard
54+ ` @strawberry_django.type ` parameters plus federation-specific ones:
55+
56+ | Parameter | Type | Description |
57+ | ----------------- | ----------------- | ------------------------------------------------------------------------- |
58+ | ` keys ` | ` list[str] ` | Key fields for entity resolution (e.g., ` ["id"] ` or ` ["sku", "package"] ` ) |
59+ | ` extend ` | ` bool ` | Whether this type extends a type from another subgraph |
60+ | ` shareable ` | ` bool ` | Whether this type can be resolved by multiple subgraphs |
61+ | ` inaccessible ` | ` bool ` | Whether this type is hidden from the public API |
62+ | ` authenticated ` | ` bool ` | Whether this type requires authentication |
63+ | ` policy ` | ` list[list[str]] ` | Access policy for this type |
64+ | ` requires_scopes ` | ` list[list[str]] ` | Required OAuth scopes for this type |
65+ | ` tags ` | ` list[str] ` | Metadata tags for this type |
66+
67+ ### Multiple Keys
68+
69+ You can define multiple key fields:
70+
71+ ``` python
72+ @strawberry_django.federation.type (models.Product, keys = [" id" , " upc" ])
73+ class Product :
74+ id : strawberry.auto
75+ upc: strawberry.auto
76+ name: strawberry.auto
77+ ```
78+
79+ ### Composite Keys
80+
81+ For composite keys (multiple fields that together form a key), use a space-separated
82+ string:
83+
84+ ``` python
85+ @strawberry_django.federation.type (models.ProductVariant, keys = [" sku package" ])
86+ class ProductVariant :
87+ sku: strawberry.auto
88+ package: strawberry.auto
89+ price: strawberry.auto
90+ ```
91+
92+ ### Custom ` resolve_reference `
93+
94+ If you need custom logic, you can still define your own ` resolve_reference ` :
95+
96+ ``` python
97+ @strawberry_django.federation.type (models.Product, keys = [" upc" ])
98+ class Product :
99+ upc: strawberry.auto
100+ name: strawberry.auto
101+
102+ @ classmethod
103+ def resolve_reference (cls , upc : str , info : Info) -> " Product" :
104+ # Custom implementation with select_related
105+ return models.Product.objects.select_related(" category" ).get(upc = upc)
106+ ```
107+
108+ ### Federation Fields
109+
110+ Use ` strawberry_django.federation.field ` for federation-specific field directives:
111+
112+ ``` python
113+ @strawberry_django.federation.type (models.Product, keys = [" id" ])
114+ class Product :
115+ id : strawberry.auto
116+ name: strawberry.auto = strawberry_django.federation.field(external = True )
117+ price: strawberry.auto = strawberry_django.federation.field(shareable = True )
118+ display_name: str = strawberry_django.federation.field(requires = [" name" ])
119+ ```
120+
121+ Field parameters:
122+
123+ | Parameter | Type | Description |
124+ | ----------------- | ----------------- | ------------------------------------------------ |
125+ | ` authenticated ` | ` bool ` | Whether this field requires authentication |
126+ | ` external ` | ` bool ` | Field is defined in another subgraph |
127+ | ` requires ` | ` list[str] ` | Fields required from other subgraphs |
128+ | ` provides ` | ` list[str] ` | Fields this resolver provides to other subgraphs |
129+ | ` override ` | ` str ` | Override field from another subgraph |
130+ | ` policy ` | ` list[list[str]] ` | Access policy for this field |
131+ | ` requires_scopes ` | ` list[list[str]] ` | Required OAuth scopes for this field |
132+ | ` shareable ` | ` bool ` | Field can be resolved by multiple subgraphs |
133+ | ` tags ` | ` list[str] ` | Metadata tags for this field |
134+ | ` inaccessible ` | ` bool ` | Field is hidden from the public API |
135+
136+ ### Interfaces
137+
138+ Federation interfaces are also supported:
139+
140+ ``` python
141+ @strawberry_django.federation.interface (models.Product, keys = [" id" ])
142+ class ProductInterface :
143+ id : strawberry.auto
144+ name: strawberry.auto
55145```
56146
57- ## Reference Resolvers
147+ ## Using Strawberry's Federation Directly
58148
59- When other services need to resolve your Django entities, define ` resolve_reference ` :
149+ You can also use Strawberry's federation decorators alongside ` strawberry_django ` :
60150
61151``` python
152+ import strawberry
153+ import strawberry_django
154+ from strawberry.federation.schema_directives import Key
155+
156+ from . import models
157+
158+
62159@strawberry_django.type (models.Product, directives = [Key(fields = " upc" )])
63160class Product :
64161 upc: strawberry.auto
@@ -70,6 +167,24 @@ class Product:
70167 return models.Product.objects.get(upc = upc)
71168```
72169
170+ ## Creating a Federated Schema
171+
172+ Use ` strawberry.federation.Schema ` instead of the regular ` strawberry.Schema ` :
173+
174+ ``` python
175+ from strawberry.federation import Schema
176+
177+
178+ @strawberry.type
179+ class Query :
180+ @strawberry_django.field
181+ def products (self ) -> list[Product]:
182+ return models.Product.objects.all()
183+
184+
185+ schema = Schema(query = Query)
186+ ```
187+
73188## Django-Specific Considerations
74189
75190### Query Optimizer
@@ -82,11 +197,13 @@ from strawberry_django.optimizer import DjangoOptimizerExtension
82197
83198schema = Schema(
84199 query = Query,
85- enable_federation_2 = True ,
86200 extensions = [DjangoOptimizerExtension],
87201)
88202```
89203
204+ The auto-generated ` resolve_reference ` methods integrate with the query optimizer
205+ when using ` strawberry_django.federation ` .
206+
90207### Authentication
91208
92209When using federation with Django authentication, ensure your gateway forwards
0 commit comments