-
Notifications
You must be signed in to change notification settings - Fork 765
Feature: Add DjangoInstanceField to allow reuse queryset on DjangoObjectType #1133
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
Open
sebsasto
wants to merge
3
commits into
graphql-python:main
Choose a base branch
from
sebsasto:instance-field
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.
+680
−26
Open
Changes from all commits
Commits
Show all changes
3 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,345 @@ | ||
import pytest | ||
|
||
import graphene | ||
from graphene.relay import Node | ||
|
||
from graphql_relay import to_global_id | ||
|
||
from ..fields import DjangoConnectionField, DjangoInstanceField | ||
from ..types import DjangoObjectType | ||
|
||
from .models import Article, Reporter | ||
|
||
|
||
class TestShouldCallGetQuerySetOnForeignKey: | ||
""" | ||
Check that the get_queryset method is called in both forward and reversed direction | ||
of a foreignkey on types. | ||
(see issue #1111) | ||
""" | ||
|
||
@pytest.fixture(autouse=True) | ||
def setup_schema(self): | ||
class ReporterType(DjangoObjectType): | ||
class Meta: | ||
model = Reporter | ||
|
||
@classmethod | ||
def get_queryset(cls, queryset, info): | ||
if info.context and info.context.get("admin"): | ||
return queryset | ||
raise Exception("Not authorized to access reporters.") | ||
|
||
class ArticleType(DjangoObjectType): | ||
class Meta: | ||
model = Article | ||
|
||
@classmethod | ||
def get_queryset(cls, queryset, info): | ||
return queryset.exclude(headline__startswith="Draft") | ||
|
||
class Query(graphene.ObjectType): | ||
reporter = DjangoInstanceField(ReporterType, id=graphene.ID(required=True)) | ||
article = DjangoInstanceField(ArticleType, id=graphene.ID(required=True)) | ||
|
||
self.schema = graphene.Schema(query=Query) | ||
|
||
self.reporter = Reporter.objects.create( | ||
first_name="Jane", last_name="Doe", pk=2 | ||
) | ||
|
||
self.articles = [ | ||
Article.objects.create( | ||
headline="A fantastic article", | ||
reporter=self.reporter, | ||
editor=self.reporter, | ||
), | ||
Article.objects.create( | ||
headline="Draft: My next best seller", | ||
reporter=self.reporter, | ||
editor=self.reporter, | ||
), | ||
] | ||
|
||
def test_get_queryset_called_on_field(self): | ||
# If a user tries to access an article it is fine as long as it's not a draft one | ||
query = """ | ||
query getArticle($id: ID!) { | ||
article(id: $id) { | ||
headline | ||
} | ||
} | ||
""" | ||
# Non-draft | ||
result = self.schema.execute(query, variables={"id": self.articles[0].id}) | ||
assert not result.errors | ||
assert result.data["article"] == { | ||
"headline": "A fantastic article", | ||
} | ||
# Draft | ||
result = self.schema.execute(query, variables={"id": self.articles[1].id}) | ||
assert not result.errors | ||
assert result.data["article"] is None | ||
|
||
# If a non admin user tries to access a reporter they should get our authorization error | ||
query = """ | ||
query getReporter($id: ID!) { | ||
reporter(id: $id) { | ||
firstName | ||
} | ||
} | ||
""" | ||
|
||
result = self.schema.execute(query, variables={"id": self.reporter.id}) | ||
assert len(result.errors) == 1 | ||
assert result.errors[0].message == "Not authorized to access reporters." | ||
|
||
# An admin user should be able to get reporters | ||
query = """ | ||
query getReporter($id: ID!) { | ||
reporter(id: $id) { | ||
firstName | ||
} | ||
} | ||
""" | ||
|
||
result = self.schema.execute( | ||
query, variables={"id": self.reporter.id}, context_value={"admin": True}, | ||
) | ||
assert not result.errors | ||
assert result.data == {"reporter": {"firstName": "Jane"}} | ||
|
||
def test_get_queryset_called_on_foreignkey(self): | ||
# If a user tries to access a reporter through an article they should get our authorization error | ||
query = """ | ||
query getArticle($id: ID!) { | ||
article(id: $id) { | ||
headline | ||
reporter { | ||
firstName | ||
} | ||
} | ||
} | ||
""" | ||
|
||
result = self.schema.execute(query, variables={"id": self.articles[0].id}) | ||
assert len(result.errors) == 1 | ||
assert result.errors[0].message == "Not authorized to access reporters." | ||
|
||
# An admin user should be able to get reporters through an article | ||
query = """ | ||
query getArticle($id: ID!) { | ||
article(id: $id) { | ||
headline | ||
reporter { | ||
firstName | ||
} | ||
} | ||
} | ||
""" | ||
|
||
result = self.schema.execute( | ||
query, variables={"id": self.articles[0].id}, context_value={"admin": True}, | ||
) | ||
assert not result.errors | ||
assert result.data["article"] == { | ||
"headline": "A fantastic article", | ||
"reporter": {"firstName": "Jane"}, | ||
} | ||
|
||
# An admin user should not be able to access draft article through a reporter | ||
query = """ | ||
query getReporter($id: ID!) { | ||
reporter(id: $id) { | ||
firstName | ||
articles { | ||
headline | ||
} | ||
} | ||
} | ||
""" | ||
|
||
result = self.schema.execute( | ||
query, variables={"id": self.reporter.id}, context_value={"admin": True}, | ||
) | ||
assert not result.errors | ||
assert result.data["reporter"] == { | ||
"firstName": "Jane", | ||
"articles": [{"headline": "A fantastic article"}], | ||
} | ||
|
||
|
||
class TestShouldCallGetQuerySetOnForeignKeyNode: | ||
""" | ||
Check that the get_queryset method is called in both forward and reversed direction | ||
of a foreignkey on types using a node interface. | ||
(see issue #1111) | ||
""" | ||
|
||
@pytest.fixture(autouse=True) | ||
def setup_schema(self): | ||
class ReporterType(DjangoObjectType): | ||
class Meta: | ||
model = Reporter | ||
interfaces = (Node,) | ||
|
||
@classmethod | ||
def get_queryset(cls, queryset, info): | ||
if info.context and info.context.get("admin"): | ||
return queryset | ||
raise Exception("Not authorized to access reporters.") | ||
|
||
class ArticleType(DjangoObjectType): | ||
class Meta: | ||
model = Article | ||
interfaces = (Node,) | ||
|
||
@classmethod | ||
def get_queryset(cls, queryset, info): | ||
return queryset.exclude(headline__startswith="Draft") | ||
|
||
class Query(graphene.ObjectType): | ||
reporter = Node.Field(ReporterType) | ||
article = Node.Field(ArticleType) | ||
|
||
self.schema = graphene.Schema(query=Query) | ||
|
||
self.reporter = Reporter.objects.create(first_name="Jane", last_name="Doe") | ||
|
||
self.articles = [ | ||
Article.objects.create( | ||
headline="A fantastic article", | ||
reporter=self.reporter, | ||
editor=self.reporter, | ||
), | ||
Article.objects.create( | ||
headline="Draft: My next best seller", | ||
reporter=self.reporter, | ||
editor=self.reporter, | ||
), | ||
] | ||
|
||
def test_get_queryset_called_on_node(self): | ||
# If a user tries to access an article it is fine as long as it's not a draft one | ||
query = """ | ||
query getArticle($id: ID!) { | ||
article(id: $id) { | ||
headline | ||
} | ||
} | ||
""" | ||
# Non-draft | ||
result = self.schema.execute( | ||
query, variables={"id": to_global_id("ArticleType", self.articles[0].id)} | ||
) | ||
assert not result.errors | ||
assert result.data["article"] == { | ||
"headline": "A fantastic article", | ||
} | ||
# Draft | ||
result = self.schema.execute( | ||
query, variables={"id": to_global_id("ArticleType", self.articles[1].id)} | ||
) | ||
assert not result.errors | ||
assert result.data["article"] is None | ||
|
||
# If a non admin user tries to access a reporter they should get our authorization error | ||
query = """ | ||
query getReporter($id: ID!) { | ||
reporter(id: $id) { | ||
firstName | ||
} | ||
} | ||
""" | ||
|
||
result = self.schema.execute( | ||
query, variables={"id": to_global_id("ReporterType", self.reporter.id)} | ||
) | ||
assert len(result.errors) == 1 | ||
assert result.errors[0].message == "Not authorized to access reporters." | ||
|
||
# An admin user should be able to get reporters | ||
query = """ | ||
query getReporter($id: ID!) { | ||
reporter(id: $id) { | ||
firstName | ||
} | ||
} | ||
""" | ||
|
||
result = self.schema.execute( | ||
query, | ||
variables={"id": to_global_id("ReporterType", self.reporter.id)}, | ||
context_value={"admin": True}, | ||
) | ||
assert not result.errors | ||
assert result.data == {"reporter": {"firstName": "Jane"}} | ||
|
||
def test_get_queryset_called_on_foreignkey(self): | ||
# If a user tries to access a reporter through an article they should get our authorization error | ||
query = """ | ||
query getArticle($id: ID!) { | ||
article(id: $id) { | ||
headline | ||
reporter { | ||
firstName | ||
} | ||
} | ||
} | ||
""" | ||
|
||
result = self.schema.execute( | ||
query, variables={"id": to_global_id("ArticleType", self.articles[0].id)} | ||
) | ||
assert len(result.errors) == 1 | ||
assert result.errors[0].message == "Not authorized to access reporters." | ||
|
||
# An admin user should be able to get reporters through an article | ||
query = """ | ||
query getArticle($id: ID!) { | ||
article(id: $id) { | ||
headline | ||
reporter { | ||
firstName | ||
} | ||
} | ||
} | ||
""" | ||
|
||
result = self.schema.execute( | ||
query, | ||
variables={"id": to_global_id("ArticleType", self.articles[0].id)}, | ||
context_value={"admin": True}, | ||
) | ||
assert not result.errors | ||
assert result.data["article"] == { | ||
"headline": "A fantastic article", | ||
"reporter": {"firstName": "Jane"}, | ||
} | ||
|
||
# An admin user should not be able to access draft article through a reporter | ||
query = """ | ||
query getReporter($id: ID!) { | ||
reporter(id: $id) { | ||
firstName | ||
articles { | ||
edges { | ||
node { | ||
headline | ||
} | ||
} | ||
} | ||
} | ||
} | ||
""" | ||
|
||
result = self.schema.execute( | ||
query, | ||
variables={"id": to_global_id("ReporterType", self.reporter.id)}, | ||
context_value={"admin": True}, | ||
) | ||
assert not result.errors | ||
assert result.data["reporter"] == { | ||
"firstName": "Jane", | ||
"articles": {"edges": [{"node": {"headline": "A fantastic article"}}]}, | ||
} |
This file contains hidden or 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
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.
Shouldn't we also lookup the primary field of the model and pass the result in as
unique_fields
? I could see a model having a different primary key field name and I think that would cause this to break