Skip to content

Commit

Permalink
Merge branch 'relay-id' into 0.159.0-plus-relay
Browse files Browse the repository at this point in the history
  • Loading branch information
jthorniley committed Mar 14, 2023
2 parents dfde19a + 4bd9121 commit f4773c7
Show file tree
Hide file tree
Showing 32 changed files with 4,808 additions and 21 deletions.
3 changes: 3 additions & 0 deletions .coveragerc
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ exclude_lines =

@overload

# Those are not supposed to be hit
assert_never\(\w+\)

ignore_errors = True

omit =
Expand Down
4 changes: 4 additions & 0 deletions RELEASE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Release type: minor

Initial relay spec implementation. For information on how to use
it, check out the docs in here: https://strawberry.rocks/docs/guides/relay
1 change: 1 addition & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
- [Dealing with errors](./guides/errors.md)
- [Federation](./guides/federation.md)
- [Federation V1](./guides/federation-v1.md)
- [Relay](./guides/relay.md)
- [Custom extensions](./guides/custom-extensions.md)
- [File upload](./guides/file-upload.md)
- [Pagination](./guides/pagination/overview.md)
Expand Down
70 changes: 70 additions & 0 deletions docs/errors/relay-wrong-annotation.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
---
title: Relay wrong annotation Error
---

# Relay wrong annotation error

## Description

This error is thrown when a field on a relay connection has a wrong
type annotation. For example, the following code will throw this error:

```python
from typing import List

import strawberry


@strawberry.type
class NonNodeSubclassType:
...


@strawberry.type
class Query:
@strawberry.relay.connection
def some_connection(self) -> int:
...

@strawberry.relay.connection
def some_other_connection(self) -> List[NonNodeSubclassType]:
...
```

This happens because when defining a custom resolver for the connection,
it expects the type annotation to be one of: `Iterable[<NodeType>]`,
`Iterator[<NodeType>]`, `AsyncIterable[<NodeType>]` or `AsyncIterator[<NodeType]`

## How to fix this error

You can fix this error by annotating the connection custom resolver with
one of the following possibilities:

- `Iterable[<NodeType>]`
- `Iterator[<NodeType>]`
- `AsyncIterable[<NodeType>]`
- `AsyncIterator[<NodeType]`

For example:

```python
from typing import Iterable, List

import strawberry


@strawberry.type
class NodeSubclassType(strawberry.relay.Node):
...


@strawberry.type
class Query:
@strawberry.relay.connection
def some_connection(self) -> List[NodeSubclassType]:
...

@strawberry.relay.connection
def some_other_connection(self) -> Iterable[NodeSubclassType]:
...
```
66 changes: 66 additions & 0 deletions docs/errors/relay-wrong-node-resolver-annotation.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
---
title: Relay wrong annotation Error
---

# Relay wrong annotation error

## Description

This error is thrown when a field on a relay connection was defined with
a `node_converter` that has the wrong type type annotation. For example,
the following code will throw this error:

```python
from typing import Iterable

import strawberry


@strawberry.type
class NonNodeSubclassType:
...


def node_converter(node_id: str) -> NonNodeSubclassType:
...


@strawberry.type
class Query:
@strawberry.relay.connection(NonNodeSubclassType)
def some_connection(self) -> Iterable[str]:
...
```

This happens because when defining a `node_converter`, it is expected
to be a function that receives the iterable element as its single argument,
and should return the correct strawberry `Node` implemented type.

## How to fix this error

You can fix this error by annotating the `node_converter` function to
return the correct strawberry `Node` implemented type.

For example:

```python
from typing import Iterable

import strawberry


@strawberry.type
class NodeSubclassType(strawberry.relay.Node):
...


def node_converter(node_id: str) -> NodeSubclassType:
...


@strawberry.type
class Query:
@strawberry.relay.connection(node_converter=node_converter)
def some_connection(self) -> Iterable[str]:
...
```
8 changes: 8 additions & 0 deletions docs/guides/pagination/overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,14 @@ need a reliable and consistent way to handle pagination.

### Cursor based pagination

<Note>

Strawberry provides a cursor based pagination implementing the
[relay spec](https://relay.dev/docs/guides/graphql-server-specification/).
You can read more about it in the [relay](./input-types) page.

<Note/>

Cursor based pagination, also known as keyset pagination, works by returning a pointer to a specific item in the dataset. On subsequent requests,
the server returns results after the given pointer. This method addresses the drawbacks of using offset pagination, but does so by making certain trade offs:

Expand Down
Loading

0 comments on commit f4773c7

Please sign in to comment.