diff --git a/docs/custom_code.md b/docs/custom_code.md index edf224a..c8713ff 100644 --- a/docs/custom_code.md +++ b/docs/custom_code.md @@ -4,13 +4,13 @@ Platformics will generate a GraphQL API that exposes CRUD operations for each en ## Adding root-level queries and mutations Platformics will generate basic read and aggregate queries in `api/queries.py`. These are inherited by a query class in `custom_queries.py`, which will gain access to all the codegenned queries as well as allow you to implement your own queries. To add a new root level field (query), add the field to the query class in `custom_queries.py`: -``` +```python @strawberry.type class Query(BaseQuery): foo: str = my_custom_field ``` The resolver for the custom field should be decorated with `@strawberry.field`: -``` +```python @strawberry.field def my_custom_field(self) -> str: return "foo" @@ -18,19 +18,19 @@ def my_custom_field(self) -> str: Similarly, for custom mutations, root level mutations can be added to the `Mutation` class in `custom_mutations.py`, which inherits from the codegenned `Mutation` class in `api/mutations.py` -``` +```python @strawberry.type class Mutation(BaseMutation): bar: str = my_custom_mutation ``` The resolver for the custom mutation should be decorated with `@strawberry.mutation`: -``` +```python @strawberry.mutation def my_custom_mutation(self) -> str: return "bar" ``` Both the custom query and mutation classes are imported into `main.py` and used by Strawberry to generate an API at runtime: -``` +```python from custom_mutations import Mutation from custom_queries import Query @@ -39,7 +39,7 @@ schema = strawberry.Schema(query=Query, mutation=Mutation, config=get_strawberry ### Overriding queries and mutations By creating a field with the same name as an existing codegenned query or mutation, you can override the codegenned resolver. -``` +```python @strawberry.mutation def my_override_mutation(self) -> str: return "Sorry, I overrode the mutation"