-
-
Notifications
You must be signed in to change notification settings - Fork 561
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
feat(schema): semantic nullability draft #3722
Open
erikwrede
wants to merge
4
commits into
strawberry-graphql:main
Choose a base branch
from
erikwrede:feat/semantic_nullability
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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,8 @@ | ||
from graphql.error.graphql_error import GraphQLError | ||
|
||
|
||
class InvalidNullReturnError(GraphQLError): | ||
def __init__(self) -> None: | ||
super().__init__( | ||
message="Expected non-null return type for semanticNonNull field, but got null", | ||
) |
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,33 @@ | ||
from __future__ import annotations | ||
|
||
from typing import TYPE_CHECKING, Any | ||
|
||
from strawberry.exceptions.semantic_nullability import InvalidNullReturnError | ||
from strawberry.extensions import FieldExtension | ||
|
||
if TYPE_CHECKING: | ||
from strawberry.extensions.field_extension import ( | ||
AsyncExtensionResolver, | ||
SyncExtensionResolver, | ||
) | ||
from strawberry.types import Info | ||
|
||
|
||
class SemanticNonNullExtension(FieldExtension): | ||
def resolve( | ||
self, next_: SyncExtensionResolver, source: Any, info: Info, **kwargs: Any | ||
) -> Any: | ||
resolved = next_(source, info, **kwargs) | ||
if resolved is not None: | ||
return resolved | ||
else: | ||
raise InvalidNullReturnError() | ||
|
||
async def resolve_async( | ||
self, next_: AsyncExtensionResolver, source: Any, info: Info, **kwargs: Any | ||
) -> Any: | ||
resolved = await next_(source, info, **kwargs) | ||
if resolved is not None: | ||
return resolved | ||
else: | ||
raise InvalidNullReturnError() |
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
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
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
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
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,12 @@ | ||
from typing import Optional | ||
|
||
from strawberry.schema_directive import Location, schema_directive | ||
|
||
|
||
@schema_directive( | ||
locations=[Location.FIELD_DEFINITION], | ||
name="semanticNonNull", | ||
print_definition=True, | ||
) | ||
class SemanticNonNull: | ||
level: Optional[int] = None |
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,8 @@ | ||
from typing import TypeVar | ||
from typing_extensions import Annotated | ||
|
||
STRAWBERRY_REQUIRED_TOKEN = "strawberry_required" | ||
|
||
T = TypeVar("T") | ||
|
||
NonNull = Annotated[T, STRAWBERRY_REQUIRED_TOKEN] |
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,60 @@ | ||
import textwrap | ||
from typing import List, Optional | ||
|
||
import pytest | ||
|
||
import strawberry | ||
from strawberry.exceptions.semantic_nullability import InvalidNullReturnError | ||
from strawberry.schema.config import StrawberryConfig | ||
from strawberry.types.strict_non_null import NonNull | ||
|
||
|
||
def test_semantic_nullability_enabled(): | ||
@strawberry.type | ||
class Product: | ||
upc: str | ||
name: str | ||
price: NonNull[int] | ||
weight: Optional[int] | ||
|
||
@strawberry.type | ||
class Query: | ||
@strawberry.field | ||
def top_products(self, first: int) -> List[Product]: | ||
return [] | ||
|
||
schema = strawberry.Schema( | ||
query=Query, config=StrawberryConfig(semantic_nullability_beta=True) | ||
) | ||
|
||
expected_sdl = textwrap.dedent(""" | ||
directive @semanticNonNull(level: Int = null) on FIELD_DEFINITION | OBJECT | INTERFACE | SCALAR | ENUM | ||
|
||
type Product { | ||
upc: String @semanticNonNull(level: null) | ||
name: String @semanticNonNull(level: null) | ||
price: Int! | ||
weight: Int @semanticNonNull(level: null) | ||
} | ||
|
||
type Query { | ||
topProducts(first: Int): [Product] @semanticNonNull(level: null) | ||
} | ||
""").strip() | ||
|
||
assert str(schema) == expected_sdl | ||
|
||
|
||
def test_semantic_nullability_error_on_null(): | ||
@strawberry.type | ||
class Query: | ||
@strawberry.field | ||
def greeting(self) -> str: | ||
return None | ||
|
||
schema = strawberry.Schema( | ||
query=Query, config=StrawberryConfig(semantic_nullability_beta=True) | ||
) | ||
|
||
with pytest.raises(InvalidNullReturnError): | ||
result = schema.execute_sync("{ greeting }") |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
issue (bug_risk): Missing return statement in StrawberryRequired branch
The code sets strawberry_semantic_required_non_null but doesn't return graphql_core_type, causing it to fall through to the else clause. Add 'return graphql_core_type' after setting the attribute.