Skip to content

Commit

Permalink
Extend query documentation (#3390)
Browse files Browse the repository at this point in the history
* Extend query documentation
Add examples with using decorator, arguments and `strawberry.argument`

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Update docs/general/queries.md

Co-authored-by: Jonathan Ehwald <github@ehwald.info>

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Jonathan Ehwald <github@ehwald.info>
  • Loading branch information
3 people committed Feb 25, 2024
1 parent be9a2d5 commit 3709858
Showing 1 changed file with 54 additions and 0 deletions.
54 changes: 54 additions & 0 deletions docs/general/queries.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,57 @@ schema = strawberry.Schema(query=Query)
```

So now, when requesting the name field, the `get_name` function will be called.

Alternatively a field can be declared using a decorator:

```python
@strawberry.type
class Query:
@strawberry.field
def name(self) -> str:
return "Strawberry"
```

## Arguments

GraphQL fields can accept arguments, usually to filter out or retrieve specific
objects:

```python
FRUITS = [
"Strawberry",
"Apple",
"Orange",
]


@strawberry.type
class Query:
@strawberry.field
def fruit(self, startswith: str) -> str | None:
for fruit in FRUITS:
if fruit.startswith(startswith):
return fruit
return None
```

Additional metadata can be added to arguments, for example a custom name and
description using `strawberry.argument` with
[typing.Annotated](https://docs.python.org/3/library/typing.html#typing.Annotated):

```python
@strawberry.type
class Query:
@strawberry.field
def fruits(
self,
is_tasty: Annotated[
bool | None,
strawberry.argument(
description="Filters out fruits by whenever they're tasty or not",
deprecation_reason="isTasty argument is deprecated, "
"use fruits(taste:SWEET) instead",
),
] = None,
) -> list[str]: ...
```

0 comments on commit 3709858

Please sign in to comment.