Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix #3129, simply provide a default #3331

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions RELEASE:md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Release type: minor

Fix #3129 by adding default value
6 changes: 4 additions & 2 deletions strawberry/relay/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -310,9 +310,11 @@ async def resolve_async(
node = field
else:

def node(*args: Any, **kwargs: Any) -> StrawberryField:
def node(*args: Any, default: Any = None, **kwargs: Any) -> StrawberryField:
kwargs["extensions"] = [*kwargs.get("extensions", []), NodeExtension()]
return field(*args, **kwargs)
# the default doesn't matter at all, it is only a stub for dataclasses
# so you can create nodes in sub objects
return field(*args, default=default, **kwargs)


@overload
Expand Down
11 changes: 11 additions & 0 deletions tests/relay/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,12 @@ async def has_permission(self, source: Any, info: Info, **kwargs: Any) -> bool:
return True


@strawberry.type
class SubWithNode:
node: relay.Node = relay.node()
nodes: list[relay.Node] = relay.node()


@strawberry.type
class Query:
node: relay.Node = relay.node()
Expand All @@ -209,6 +215,11 @@ class Query:
resolver=fruits_resolver
)

@strawberry.field
@staticmethod
def sub() -> SubWithNode:
return SubWithNode

@relay.connection(relay.ListConnection[Fruit])
def fruits_concrete_resolver(
self,
Expand Down
11 changes: 11 additions & 0 deletions tests/relay/schema_future_annotations.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,12 @@
return True


@strawberry.type
class SubWithNode:
node: relay.Node = relay.node()
nodes: list[relay.Node] = relay.node()


@strawberry.type
class Query:
node: relay.Node = relay.node()
Expand All @@ -211,6 +217,11 @@
resolver=fruits_resolver
)

@strawberry.field
@staticmethod
def sub() -> SubWithNode:
return SubWithNode

Check warning on line 223 in tests/relay/schema_future_annotations.py

View check run for this annotation

Codecov / codecov/patch

tests/relay/schema_future_annotations.py#L223

Added line #L223 was not covered by tests

@relay.connection(relay.ListConnection[Fruit])
def fruits_concrete_resolver(
self,
Expand Down
12 changes: 12 additions & 0 deletions tests/relay/snapshots/schema.gql
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ type PageInfo {
}

type Query {
sub: SubWithNode!
node(
"""The ID of the object."""
id: GlobalID!
Expand Down Expand Up @@ -335,4 +336,15 @@ type Query {
"""Returns the items in the list that come after the specified cursor."""
last: Int = null
): FruitConnection!
}

type SubWithNode {
node(
"""The ID of the object."""
id: GlobalID!
): Node!
nodes(
"""The IDs of the objects."""
ids: [GlobalID!]!
): [Node!]!
}
12 changes: 12 additions & 0 deletions tests/relay/snapshots/schema_future_annotations.gql
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ type PageInfo {
}

type Query {
sub: SubWithNode!
node(
"""The ID of the object."""
id: GlobalID!
Expand Down Expand Up @@ -335,4 +336,15 @@ type Query {
"""Returns the items in the list that come after the specified cursor."""
last: Int = null
): FruitConnection!
}

type SubWithNode {
node(
"""The ID of the object."""
id: GlobalID!
): Node!
nodes(
"""The IDs of the objects."""
ids: [GlobalID!]!
): [Node!]!
}
73 changes: 73 additions & 0 deletions tests/relay/test_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,39 @@ def test_query_node():
}


def test_query_node_sub():
result = schema.execute_sync(
"""
query TestQuery ($id: GlobalID!) {
sub {
node (id: $id) {
... on Node {
id
}
... on Fruit {
name
color
}
}
}
}
""",
variable_values={
"id": to_base64("Fruit", 2),
},
)
assert result.errors is None
assert result.data == {
"sub": {
"node": {
"id": to_base64("Fruit", 2),
"color": "red",
"name": "Apple",
},
}
}


async def test_query_node_with_async_permissions():
result = await schema.execute(
"""
Expand Down Expand Up @@ -187,6 +220,46 @@ def test_query_nodes():
}


def test_query_nodes_sub():
result = schema.execute_sync(
"""
query TestQuery ($ids: [GlobalID!]!) {
sub {
nodes (ids: $ids) {
... on Node {
id
}
... on Fruit {
name
color
}
}
}
}
""",
variable_values={
"ids": [to_base64("Fruit", 2), to_base64("Fruit", 4)],
},
)
assert result.errors is None
assert result.data == {
"sub": {
"nodes": [
{
"id": to_base64("Fruit", 2),
"name": "Apple",
"color": "red",
},
{
"id": to_base64("Fruit", 4),
"name": "Grape",
"color": "purple",
},
],
}
}


def test_query_nodes_optional():
result = schema.execute_sync(
"""
Expand Down