Skip to content

Commit 6f4a01a

Browse files
committed
Fix graphql docs with incorrect issue for self reference product blocks
- also fix typo/formatting.
1 parent d0f27ae commit 6f4a01a

File tree

1 file changed

+17
-9
lines changed

1 file changed

+17
-9
lines changed

docs/architecture/application/graphql.md

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,23 @@ app.register_graphql(query=NewQuery)
4141

4242
## Domain Models Auto Registration for GraphQL
4343

44-
When using the register_graphql function, all products in the SUBSCRIPTION_MODEL_REGISTRY will be automatically converted into GraphQL types.
44+
When using the `register_graphql()` function, all products in the `SUBSCRIPTION_MODEL_REGISTRY` will be automatically converted into GraphQL types.
4545
The registration process iterates through the list, starting from the deepest product block and working its way back up to the product level.
4646

47-
However, there is a potential issue when dealing with a ProductBlock that references itself, as it can lead to an infinite loop.
48-
To handle this situation, you must manually create the GraphQL type for that ProductBlock and add it to the GRAPHQL_MODELS list.
47+
However, there is a potential issue when dealing with a `ProductBlock` that references itself, as it leads to an error expecting the `ProductBlock` type to exist.
48+
49+
Here an example of the expected error with self referenced `ProductBlock`:
50+
51+
```
52+
trawberry.experimental.pydantic.exceptions.UnregisteredTypeException: Cannot find a Strawberry Type for <class 'products.product_blocks.product_block_file.ProductBlock'> did you forget to register it?
53+
```
54+
55+
To handle this situation, you must manually create the GraphQL type for that `ProductBlock` and add it to the `GRAPHQL_MODELS` list.
4956

5057
Here's an example of how to do it:
5158

5259
```python
60+
# product_block_file.py
5361
import strawberry
5462
from typing import Annotated
5563
from app.product_blocks import ProductBlock
@@ -60,7 +68,7 @@ from orchestrator.graphql import GRAPHQL_MODELS
6068
@strawberry.experimental.pydantic.type(model=ProductBlock)
6169
class ProductBlockGraphql:
6270
name: strawberry.auto
63-
self_refrence_block: Annotated[
71+
self_reference_block: Annotated[
6472
"ProductBlockGraphql", strawberry.lazy(".product_block_file")
6573
] | None = None
6674
...
@@ -70,19 +78,19 @@ class ProductBlockGraphql:
7078
GRAPHQL_MODELS.update({"ProductBlockGraphql": ProductBlockGraphql})
7179
```
7280

73-
By following this example, you can effectively create the necessary GraphQL type for ProductBlock and ensure proper registration with register_graphql. This will help you avoid any infinite loop scenarios and enable smooth integration of domain models with GraphQL.
81+
By following this example, you can effectively create the necessary GraphQL type for `ProductBlock` and ensure proper registration with `register_graphql()`. This will help you avoid any `Cannot find a Strawberry Type` scenarios and enable smooth integration of domain models with GraphQL.
7482

7583
### Scalars for Auto Registration
7684

77-
When working with special types such as VlanRanges or IPv4Interface in the core module, scalar types are essential for the auto registration process.
78-
Scalar types enable smooth integration of these special types into the GraphQL schema.
85+
When working with special types such as `VlanRanges` or `IPv4Interface` in the core module, scalar types are essential for the auto registration process.
86+
Scalar types enable smooth integration of these special types into the GraphQL schema, They need to be initialized before `register_graphql()`.
7987

8088
Here's an example of how to add a new scalar:
8189

8290
```python
8391
import strawberry
8492
from typing import NewType
85-
from orchestrator.graphql import SCALAR_OVERRIDESs
93+
from orchestrator.graphql import SCALAR_OVERRIDES
8694

8795
VlanRangesType = strawberry.scalar(
8896
NewType("VlanRangesType", str),
@@ -97,7 +105,7 @@ SCALAR_OVERRIDES = {
97105
}
98106
```
99107

100-
You can find more examples of scalar usage in the `orchestrator-core/orchestrator/graphql/types.py` file.
108+
You can find more examples of scalar usage in the `orchestrator/graphql/types.py` file.
101109
For additional information on Scalars, please refer to the Strawberry documentation on Scalars: https://strawberry.rocks/docs/types/scalars.
102110

103111
By using scalar types for auto registration, you can seamlessly incorporate special types into your GraphQL schema, making it easier to work with complex data in the Orchestrator application.

0 commit comments

Comments
 (0)