-
-
Notifications
You must be signed in to change notification settings - Fork 128
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: add tests to reproduce mptt nested issues
- Loading branch information
1 parent
015621c
commit e318ad2
Showing
9 changed files
with
373 additions
and
2 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
from typing import TYPE_CHECKING | ||
|
||
import strawberry | ||
from strawberry import relay | ||
from typing_extensions import Annotated, TypeAlias | ||
|
||
import strawberry_django | ||
from strawberry_django.relay import ListConnectionWithTotalCount | ||
|
||
from .models import MPTTAuthor | ||
|
||
if TYPE_CHECKING: | ||
from .b import MPTTBookConnection | ||
|
||
|
||
@strawberry_django.type(MPTTAuthor) | ||
class MPTTAuthorType(relay.Node): | ||
name: str | ||
books: Annotated["MPTTBookConnection", strawberry.lazy("tests.relay.mptt.b")] = ( | ||
strawberry_django.connection() | ||
) | ||
children: "MPTTAuthorConnection" = strawberry_django.connection() | ||
|
||
|
||
MPTTAuthorConnection: TypeAlias = ListConnectionWithTotalCount[MPTTAuthorType] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
from typing import TYPE_CHECKING | ||
|
||
import strawberry | ||
from strawberry import relay | ||
from typing_extensions import Annotated, TypeAlias | ||
|
||
import strawberry_django | ||
from strawberry_django.relay import ListConnectionWithTotalCount | ||
|
||
from .models import MPTTBook | ||
|
||
if TYPE_CHECKING: | ||
from .a import MPTTAuthorType | ||
|
||
|
||
@strawberry_django.filter(MPTTBook) | ||
class MPTTBookFilter: | ||
name: str | ||
|
||
|
||
@strawberry_django.order(MPTTBook) | ||
class MPTTBookOrder: | ||
name: str | ||
|
||
|
||
@strawberry_django.type(MPTTBook, filters=MPTTBookFilter, order=MPTTBookOrder) | ||
class MPTTBookType(relay.Node): | ||
name: str | ||
author: Annotated["MPTTAuthorType", strawberry.lazy("tests.relay.mptt.a")] | ||
|
||
|
||
MPTTBookConnection: TypeAlias = ListConnectionWithTotalCount[MPTTBookType] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
from django.db import models | ||
from mptt.fields import TreeForeignKey | ||
from mptt.models import MPTTModel | ||
|
||
|
||
class MPTTAuthor(MPTTModel): | ||
name = models.CharField(max_length=100) | ||
parent = TreeForeignKey( | ||
to="self", | ||
on_delete=models.CASCADE, | ||
null=True, | ||
blank=True, | ||
related_name="children", | ||
) | ||
|
||
|
||
class MPTTBook(models.Model): | ||
title = models.CharField(max_length=100) | ||
author = models.ForeignKey( | ||
MPTTAuthor, | ||
on_delete=models.CASCADE, | ||
related_name="books", | ||
) |
182 changes: 182 additions & 0 deletions
182
...s/test_lazy_annotations/test_lazy_type_annotations_in_schema/authors_and_books_schema.gql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,182 @@ | ||
""" | ||
The `ID` scalar type represents a unique identifier, often used to refetch an object or as key for a cache. The ID type appears in a JSON response as a String; however, it is not intended to be human-readable. When expected as an input type, any string (such as `"4"`) or integer (such as `4`) input value will be accepted as an ID. | ||
""" | ||
scalar GlobalID @specifiedBy(url: "https://relay.dev/graphql/objectidentification.htm") | ||
|
||
type MPTTAuthorType implements Node { | ||
"""The Globally Unique ID of this object""" | ||
id: GlobalID! | ||
name: String! | ||
books( | ||
filters: MPTTBookFilter | ||
order: MPTTBookOrder | ||
|
||
"""Returns the items in the list that come before the specified cursor.""" | ||
before: String = null | ||
|
||
"""Returns the items in the list that come after the specified cursor.""" | ||
after: String = null | ||
|
||
"""Returns the first n items from the list.""" | ||
first: Int = null | ||
|
||
"""Returns the items in the list that come after the specified cursor.""" | ||
last: Int = null | ||
): MPTTBookTypeConnection! | ||
children( | ||
"""Returns the items in the list that come before the specified cursor.""" | ||
before: String = null | ||
|
||
"""Returns the items in the list that come after the specified cursor.""" | ||
after: String = null | ||
|
||
"""Returns the first n items from the list.""" | ||
first: Int = null | ||
|
||
"""Returns the items in the list that come after the specified cursor.""" | ||
last: Int = null | ||
): MPTTAuthorTypeConnection! | ||
} | ||
|
||
"""A connection to a list of items.""" | ||
type MPTTAuthorTypeConnection { | ||
"""Pagination data for this connection""" | ||
pageInfo: PageInfo! | ||
|
||
"""Contains the nodes in this connection""" | ||
edges: [MPTTAuthorTypeEdge!]! | ||
|
||
"""Total quantity of existing nodes.""" | ||
totalCount: Int | ||
} | ||
|
||
"""An edge in a connection.""" | ||
type MPTTAuthorTypeEdge { | ||
"""A cursor for use in pagination""" | ||
cursor: String! | ||
|
||
"""The item at the end of the edge""" | ||
node: MPTTAuthorType! | ||
} | ||
|
||
input MPTTBookFilter { | ||
name: String! | ||
AND: MPTTBookFilter | ||
OR: MPTTBookFilter | ||
NOT: MPTTBookFilter | ||
DISTINCT: Boolean | ||
} | ||
|
||
input MPTTBookOrder { | ||
name: String | ||
} | ||
|
||
type MPTTBookType implements Node { | ||
"""The Globally Unique ID of this object""" | ||
id: GlobalID! | ||
name: String! | ||
author: MPTTAuthorType! | ||
} | ||
|
||
"""A connection to a list of items.""" | ||
type MPTTBookTypeConnection { | ||
"""Pagination data for this connection""" | ||
pageInfo: PageInfo! | ||
|
||
"""Contains the nodes in this connection""" | ||
edges: [MPTTBookTypeEdge!]! | ||
|
||
"""Total quantity of existing nodes.""" | ||
totalCount: Int | ||
} | ||
|
||
"""An edge in a connection.""" | ||
type MPTTBookTypeEdge { | ||
"""A cursor for use in pagination""" | ||
cursor: String! | ||
|
||
"""The item at the end of the edge""" | ||
node: MPTTBookType! | ||
} | ||
|
||
"""An object with a Globally Unique ID""" | ||
interface Node { | ||
"""The Globally Unique ID of this object""" | ||
id: GlobalID! | ||
} | ||
|
||
"""Information to aid in pagination.""" | ||
type PageInfo { | ||
"""When paginating forwards, are there more items?""" | ||
hasNextPage: Boolean! | ||
|
||
"""When paginating backwards, are there more items?""" | ||
hasPreviousPage: Boolean! | ||
|
||
"""When paginating backwards, the cursor to continue.""" | ||
startCursor: String | ||
|
||
"""When paginating forwards, the cursor to continue.""" | ||
endCursor: String | ||
} | ||
|
||
type Query { | ||
booksConn( | ||
filters: MPTTBookFilter | ||
order: MPTTBookOrder | ||
|
||
"""Returns the items in the list that come before the specified cursor.""" | ||
before: String = null | ||
|
||
"""Returns the items in the list that come after the specified cursor.""" | ||
after: String = null | ||
|
||
"""Returns the first n items from the list.""" | ||
first: Int = null | ||
|
||
"""Returns the items in the list that come after the specified cursor.""" | ||
last: Int = null | ||
): MPTTBookTypeConnection! | ||
booksConn2( | ||
filters: MPTTBookFilter | ||
order: MPTTBookOrder | ||
|
||
"""Returns the items in the list that come before the specified cursor.""" | ||
before: String = null | ||
|
||
"""Returns the items in the list that come after the specified cursor.""" | ||
after: String = null | ||
|
||
"""Returns the first n items from the list.""" | ||
first: Int = null | ||
|
||
"""Returns the items in the list that come after the specified cursor.""" | ||
last: Int = null | ||
): MPTTBookTypeConnection! | ||
authorsConn( | ||
"""Returns the items in the list that come before the specified cursor.""" | ||
before: String = null | ||
|
||
"""Returns the items in the list that come after the specified cursor.""" | ||
after: String = null | ||
|
||
"""Returns the first n items from the list.""" | ||
first: Int = null | ||
|
||
"""Returns the items in the list that come after the specified cursor.""" | ||
last: Int = null | ||
): MPTTAuthorTypeConnection! | ||
authorsConn2( | ||
"""Returns the items in the list that come before the specified cursor.""" | ||
before: String = null | ||
|
||
"""Returns the items in the list that come after the specified cursor.""" | ||
after: String = null | ||
|
||
"""Returns the first n items from the list.""" | ||
first: Int = null | ||
|
||
"""Returns the items in the list that come after the specified cursor.""" | ||
last: Int = null | ||
): MPTTAuthorTypeConnection! | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import pathlib | ||
|
||
import strawberry | ||
from pytest_snapshot.plugin import Snapshot | ||
|
||
import strawberry_django | ||
from strawberry_django.relay import ListConnectionWithTotalCount | ||
|
||
from .a import MPTTAuthorConnection, MPTTAuthorType | ||
from .b import MPTTBookConnection, MPTTBookType | ||
|
||
SNAPSHOTS_DIR = pathlib.Path(__file__).parent / "snapshots" | ||
|
||
|
||
def test_lazy_type_annotations_in_schema(snapshot: Snapshot): | ||
@strawberry.type | ||
class Query: | ||
books_conn: MPTTBookConnection = strawberry_django.connection() | ||
books_conn2: ListConnectionWithTotalCount[MPTTBookType] = ( | ||
strawberry_django.connection() | ||
) | ||
authors_conn: MPTTAuthorConnection = strawberry_django.connection() | ||
authors_conn2: ListConnectionWithTotalCount[MPTTAuthorType] = ( | ||
strawberry_django.connection() | ||
) | ||
|
||
schema = strawberry.Schema(query=Query) | ||
snapshot.assert_match(str(schema), "authors_and_books_schema.gql") |
Oops, something went wrong.