Skip to content

Commit a064d22

Browse files
update readme with custom operation builder section
1 parent dda41d1 commit a064d22

File tree

1 file changed

+88
-1
lines changed

1 file changed

+88
-1
lines changed

README.md

+88-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ queries_path = "queries.graphql"
5353

5454
Required settings:
5555

56-
- `queries_path` - path to file/directory with queries
56+
- `queries_path` - path to file/directory with queries (Can be optional if `enable_custom_operations` is used)
5757

5858
One of the following 2 parameters is required, in case of providing both of them `schema_path` is prioritized:
5959

@@ -81,6 +81,93 @@ Optional settings:
8181
- `opentelemetry_client` (defaults to `false`) - default base clients don't support any performance tracing. Change this option to `true` to use the base client with Open Telemetry support.
8282
- `files_to_include` (defaults to `[]`) - list of files which will be copied into generated package
8383
- `plugins` (defaults to `[]`) - list of plugins to use during generation
84+
- `enable_custom_operations` (defaults to `false`) - enables building custom operations. Generates additional files that contains all the classes and methods for generation.
85+
86+
87+
## Custom operation builder
88+
89+
The custom operation builder allows you to create complex GraphQL queries in a structured and intuitive way.
90+
91+
### Example Code
92+
93+
```python
94+
import asyncio
95+
from graphql_client import Client
96+
from graphql_client.custom_fields import (
97+
ProductFields,
98+
ProductTranslatableContentFields,
99+
ProductTranslationFields,
100+
TranslatableItemConnectionFields,
101+
TranslatableItemEdgeFields,
102+
)
103+
from graphql_client.custom_queries import Query
104+
from graphql_client.enums import LanguageCodeEnum, TranslatableKinds
105+
106+
107+
async def get_products():
108+
# Create a client instance with the specified URL and headers
109+
client = Client(
110+
url="https://saleor.cloud/graphql/",
111+
headers={"authorization": "bearer ..."},
112+
)
113+
114+
# Build the queries
115+
product_query = Query.product(id="...", channel="channel-uk").fields(
116+
ProductFields.id,
117+
ProductFields.name,
118+
)
119+
120+
translation_query = Query.translations(kind=TranslatableKinds.PRODUCT, first=10).fields(
121+
TranslatableItemConnectionFields.edges().alias("aliased_edges").fields(
122+
TranslatableItemEdgeFields.node.on(
123+
"ProductTranslatableContent",
124+
ProductTranslatableContentFields.id,
125+
ProductTranslatableContentFields.product_id,
126+
ProductTranslatableContentFields.name,
127+
)
128+
)
129+
)
130+
131+
# Execute the queries with an operation name
132+
response = await client.query(
133+
product_query,
134+
translation_query,
135+
operation_name="get_products",
136+
)
137+
138+
print(response)
139+
140+
# Run the async function
141+
asyncio.run(get_products())
142+
```
143+
144+
### Explanation
145+
146+
147+
1. Building the Product Query:
148+
1. The Query.product(id="...", channel="channel-uk") initiates a query for a product with the specified ID and channel.
149+
2. .fields(ProductFields.id, ProductFields.name) specifies the fields to retrieve for the product: id and name.
150+
2. Building the Translation Query:
151+
1. The Query.translations(kind=TranslatableKinds.PRODUCT, first=10) initiates a query for product translations.
152+
2. .fields(...) specifies the fields to retrieve for the translations.
153+
3. .alias("aliased_edges") renames the edges field to aliased_edges.
154+
4. .on("ProductTranslatableContent", ...) specifies the fields to retrieve if the node is of type ProductTranslatableContent: id, product_id, and name.
155+
3. Executing the Queries:
156+
1. The client.query(...) method is called with the built queries and an operation name "get_products".
157+
2. This method sends the queries to the server and retrieves the response.
158+
159+
160+
### Example pyproject.toml configuration.
161+
162+
`Note: queries_path is optional when enable_custom_operations is set to true`
163+
164+
```toml
165+
[tool.ariadne-codegen]
166+
schema_path = "schema.graphql"
167+
include_comments = "none"
168+
target_package_name = "example_client"
169+
enable_custom_operations = true
170+
```
84171

85172

86173
## Plugins

0 commit comments

Comments
 (0)