Skip to content

Commit

Permalink
test: add tests to reproduce mptt nested issues
Browse files Browse the repository at this point in the history
  • Loading branch information
Eraldo authored and bellini666 committed Jun 12, 2024
1 parent 015621c commit e318ad2
Show file tree
Hide file tree
Showing 9 changed files with 373 additions and 2 deletions.
37 changes: 35 additions & 2 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ channels = { version = ">=3.0.5" }
django-choices-field = "^2.2.2"
django-debug-toolbar = "^4.2.0"
django-guardian = "^2.4.0"
django-mptt = "^0.14.0"
django-types = "^0.19.1"
factory-boy = "^3.2.1"
mkdocs = "^1.4.2"
Expand Down
Empty file added tests/relay/mptt/__init__.py
Empty file.
25 changes: 25 additions & 0 deletions tests/relay/mptt/a.py
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]
32 changes: 32 additions & 0 deletions tests/relay/mptt/b.py
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]
23 changes: 23 additions & 0 deletions tests/relay/mptt/models.py
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",
)
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!
}
28 changes: 28 additions & 0 deletions tests/relay/mptt/test_lazy_annotations.py
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")
Loading

0 comments on commit e318ad2

Please sign in to comment.