Skip to content

Commit

Permalink
Minor fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
patrick91 committed Jun 6, 2024
1 parent 8057155 commit 8b30ec4
Show file tree
Hide file tree
Showing 21 changed files with 426 additions and 159 deletions.
2 changes: 1 addition & 1 deletion docs/editors/vscode.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,4 @@ checking mode. At the moment strict mode is not supported.
Once you have configured the settings, you can restart VS Code and you should be
getting type checking errors in vscode.

<img src="./pylance.png" alt="Pylance showing a type error" width="1990" height="522" />
<!-- ![Pylance showing a type error](./pylance.png) -->
18 changes: 11 additions & 7 deletions docs/general/mutations.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,18 +69,23 @@ It is also possible to write a mutation that doesn't return anything.

This is mapped to a `Void` GraphQL scalar, and always returns `null`

```python+schema
<CodeGrid>
```python
@strawberry.type
class Mutation:
@strawberry.mutation
def restart() -> None:
print(f'Restarting the server')
---
print(f"Restarting the server")
```

```graphql
type Mutation {
restart: Void
}
```

</CodeGrid>

<Note>

Mutations with void-result go against
Expand Down Expand Up @@ -166,12 +171,10 @@ import strawberry
@strawberry.type
class FruitMutations:
@strawberry.mutation
def add(self, info, input: AddFruitInput) -> Fruit:
# ...
def add(self, info, input: AddFruitInput) -> Fruit: ...

@strawberry.mutation
def update_weight(self, info, input: UpdateFruitWeightInput) -> Fruit:
# ...
def update_weight(self, info, input: UpdateFruitWeightInput) -> Fruit: ...


@strawberry.type
Expand Down Expand Up @@ -211,4 +214,5 @@ For more details, see
[Apollo's guide on Namespaces for serial mutations](https://www.apollographql.com/docs/technotes/TN0012-namespacing-by-separation-of-concern/#namespaces-for-serial-mutations)
and
[Rapid API's Interactive Guide to GraphQL Queries: Aliases and Variables](https://rapidapi.com/guides/graphql-aliases-variables).

</Note>
2 changes: 1 addition & 1 deletion docs/general/subscriptions.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ subscription {

In this example, the data looks like this as it passes over the websocket:

<img src="../images/subscriptions-count-websocket.png" alt="A view of the data that's been passed via websocket" width="1013" height="267" />
![A view of the data that's been passed via websocket](../images/subscriptions-count-websocket.png)

This is a very short example of what is possible. Like with queries and
mutations the subscription can return any GraphQL type, not only scalars as
Expand Down
10 changes: 8 additions & 2 deletions docs/general/why.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,24 @@ GraphQL APIs while also helping finding bugs when using type checkers like MyPy.
Here's a basic example of a type and how it compares to the equivalent type in
GraphQL:

```python+schema
<CodeGrid>

```python
@strawberry.type
class User:
id: strawberry.ID
name: str
---
```

```graphql
type User {
id: ID!
name: String!
}
```

</CodeGrid>

As you can see the code is very similar to what you would write using the
GraphQL SDL. Thanks to this, we think Strawberry hits a perfect middle ground
between code first and schema first.
Expand Down
16 changes: 11 additions & 5 deletions docs/guides/accessing-parent-data.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ It is quite common to want to be able to access the data from the field's parent
in a resolver. For example let's say that we want to define a `fullName` field
on our `User`. This would be our code:

```python+schema
<CodeGrid>

```python
import strawberry


Expand All @@ -17,14 +19,18 @@ class User:
first_name: str
last_name: str
full_name: str
---
```

```graphql
type User {
firstName: String!
lastName: String!
fullName: String!
firstName: String!
lastName: String!
fullName: String!
}
```

</CodeGrid>

In this case `full_name` will need to access the `first_name` and `last_name`
fields, and depending on whether we define the resolver as a function or as a
method, we'll have a few options! Let's start with the defining a resolver as a
Expand Down
23 changes: 18 additions & 5 deletions docs/guides/dataloaders.md
Original file line number Diff line number Diff line change
Expand Up @@ -197,15 +197,18 @@ the dataloader, in order to avoid reloading data afterwards.

For example:

```python+graphql
<CodeGrid>

```python
@strawberry.type
class Person:
id: strawberry.ID
friends_ids: strawberry.Private[List[strawberry.ID]]

@strawberry.field
async def friends(self) -> List[Person]:
return await loader.load_many(self.friends_ids)
return await loader.load_many(self.friends_ids)


@strawberry.type
class Query:
Expand All @@ -220,7 +223,9 @@ class Query:
loader.prime_many({person.id: person for person in people})

return people
---
```

```graphql
{
getAllPeople {
id
Expand All @@ -231,6 +236,8 @@ class Query:
}
```

</CodeGrid>

### Custom Cache

DataLoader's default cache is per-request and it caches data in memory. This
Expand Down Expand Up @@ -341,7 +348,9 @@ that allows to fetch a single user by id.

We can use this query by doing the following request:

```graphql+response
<CodeGrid>

```graphql
{
first: getUser(id: 1) {
id
Expand All @@ -350,7 +359,9 @@ We can use this query by doing the following request:
id
}
}
---
```

```json
{
"data": {
"first": {
Expand All @@ -363,6 +374,8 @@ We can use this query by doing the following request:
}
```

</CodeGrid>

Even if this query is fetching two users, it still results in one call to
`load_users`.

Expand Down
38 changes: 26 additions & 12 deletions docs/guides/errors.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,15 @@ GraphQL is strongly typed and so Strawberry validates all queries before
executing them. If a query is invalid it isn’t executed and instead the response
contains an `errors` list:

```graphql+response
<CodeGrid>

```graphql
{
hi
}
---
```

```json
{
"data": null,
"errors": [
Expand All @@ -42,6 +46,8 @@ contains an `errors` list:
}
```

</CodeGrid>

Each error has a message, line, column and path to help you identify what part
of the query caused the error.

Expand Down Expand Up @@ -69,11 +75,15 @@ class Query:
schema = strawberry.Schema(query=Query)
```

```graphql+response
<CodeGrid>

```graphql
{
hello
}
---
```

```json
{
"data": null,
"errors": [
Expand All @@ -85,14 +95,14 @@ schema = strawberry.Schema(query=Query)
"column": 3
}
],
"path": [
"hello"
]
"path": ["hello"]
}
]
}
```

</CodeGrid>

Each error has a message, line, column and path to help you identify what part
of the query caused the error.

Expand Down Expand Up @@ -121,13 +131,17 @@ class Query:
schema = strawberry.Schema(query=Query)
```

```graphql+response
<CodeGrid>

```graphql
{
user {
name
}
}
---
```

```json
{
"data": null,
"errors": [
Expand All @@ -139,14 +153,14 @@ schema = strawberry.Schema(query=Query)
"column": 2
}
],
"path": [
"user"
]
"path": ["user"]
}
]
}
```

</CodeGrid>

## Expected errors

If an error is expected then it is often best to express it in the schema. This
Expand Down
22 changes: 17 additions & 5 deletions docs/guides/field-extensions.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,22 @@ will be called instead of the resolver and receives the resolver function as the
`next` argument. Therefore, it is important to not modify any arguments that are
passed to `next` in an incompatible way.

```graphql+response
<CodeGrid>

```graphql
query {
string
string
}
---
```

```json
{
"string": "THIS IS A TEST!!"
}
```

</CodeGrid>

## Modifying the field

<Warning>
Expand Down Expand Up @@ -100,19 +106,25 @@ class CachingExtension(FieldExtension):
return self.cached_result
```

```python+schema
<CodeGrid>

```python
@strawberry.type
class Client:

@strawberry.field(extensions=[CachingExtensions(caching_time=200)])
def analyzed_hours(self, info) -> int:
return do_expensive_computation()
---
```

```graphql
type Client {
analyzedHours: Int! @Cached(time=200)
}
```

</CodeGrid>

## Combining multiple field extensions

When chaining multiple field extensions, the last extension in the list is
Expand Down
14 changes: 10 additions & 4 deletions docs/guides/pagination/connections.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ building GraphQL APIs which use a cursor based connection pattern.
By the end of this tutorial, we should be able to return a connection of users
when requested.

```graphql+response
<CodeGrid>

```graphql
query getUsers {
getUsers(first: 2) {
users {
Expand All @@ -33,7 +35,9 @@ query getUsers {
}
}
}
---
```

```json
{
"data": {
"getUsers": {
Expand All @@ -60,14 +64,16 @@ query getUsers {
]
},
"pageInfo": {
"endCursor": "dXNlcjoz",
"hasNextPage": true
"endCursor": "dXNlcjoz",
"hasNextPage": true
}
}
}
}
```

</CodeGrid>

## Connections

A Connection represents a paginated relationship between two entities. This
Expand Down
Loading

0 comments on commit 8b30ec4

Please sign in to comment.