You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Add graphql documentation for the model auto registration (#324)
* Add graphql documentation for the model auto registration
* Add graphql docs for federating the autoregistered types
* Fix graphql docs with incorrect issue for self reference product blocks
- also fix typo/formatting.
* Fix typo of trawberry to strawberry
Co-authored-by: Daniel Marjenburgh <[email protected]>
* Update docs/architecture/application/graphql.md
Co-authored-by: Daniel Marjenburgh <[email protected]>
---------
Co-authored-by: Tjeerd.Verschragen <[email protected]>
Co-authored-by: Daniel Marjenburgh <[email protected]>
When using the `register_graphql()` function, all products in the `SUBSCRIPTION_MODEL_REGISTRY` will be automatically converted into GraphQL types.
45
+
The registration process iterates through the list, starting from the deepest product block and working its way back up to the product level.
46
+
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 is an example of the expected error with a self referenced `ProductBlock`:
50
+
51
+
```
52
+
strawberry.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.
56
+
57
+
Here's an example of how to do it:
58
+
59
+
```python
60
+
# product_block_file.py
61
+
import strawberry
62
+
from typing import Annotated
63
+
from app.product_blocks import ProductBlock
64
+
from orchestrator.graphql importGRAPHQL_MODELS
65
+
66
+
67
+
# It is necessary to use pydantic type, so that other product blocks can recognize it when typing to GraphQL.
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.
82
+
83
+
### Scalars for Auto Registration
84
+
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()`.
87
+
88
+
Here's an example of how to add a new scalar:
89
+
90
+
```python
91
+
import strawberry
92
+
from typing import NewType
93
+
from orchestrator.graphql importSCALAR_OVERRIDES
94
+
95
+
VlanRangesType = strawberry.scalar(
96
+
NewType("VlanRangesType", str),
97
+
description="Represent the Orchestrator VlanRanges data type",
98
+
serialize=lambdav: v.to_list_of_tuples(),
99
+
parse_value=lambdav: v,
100
+
)
101
+
102
+
# Add the scalar to the SCALAR_OVERRIDES dictionary, with the type in the product block as the key and the scalar as the value
103
+
SCALAR_OVERRIDES= {
104
+
VlanRanges: VlanRangesType,
105
+
}
106
+
```
107
+
108
+
You can find more examples of scalar usage in the `orchestrator/graphql/types.py` file.
109
+
For additional information on Scalars, please refer to the Strawberry documentation on Scalars: https://strawberry.rocks/docs/types/scalars.
110
+
111
+
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.
112
+
113
+
### Federating with Autogenerated Types
114
+
115
+
To enable federation, set the `FEDERATION_ENABLED` environment variable to `True`.
116
+
117
+
Federation allows you to federate with subscriptions using the `subscriptionId` and with product blocks inside the subscription by utilizing any property that includes `_id` in its name.
118
+
119
+
Below is an example of a GraphQL app that extends the `SubscriptionInterface`:
By following these examples, you can effectively federate autogenerated types (`subscriptions` and `product blocks`) enabling seamless integration across multiple GraphQL endpoints.
0 commit comments