Skip to content

Commit

Permalink
Add contentType field on SnippetInterface
Browse files Browse the repository at this point in the history
  • Loading branch information
mgax committed Sep 23, 2024
1 parent 34a3c3b commit 09ea03b
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 1 deletion.
4 changes: 3 additions & 1 deletion docs/general-usage/interfaces.rst
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,8 @@ in the interface:

::

snipeptType: String!
snippetType: String!
contentType: String!

An example of querying all snippets:

Expand All @@ -124,6 +125,7 @@ An example of querying all snippets:
query {
snippets {
snippetType
contentType
...on Advert {
id
url
Expand Down
7 changes: 7 additions & 0 deletions grapple/types/interfaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,10 +217,17 @@ def get_snippet_interface():

class SnippetInterface(graphene.Interface):
snippet_type = graphene.String(required=True)
content_type = graphene.String(required=True)

@classmethod
def resolve_type(cls, instance, info, **kwargs):
return registry.snippets[type(instance)]

def resolve_snippet_type(self, info, **kwargs):
return self.__class__.__name__

def resolve_content_type(self, info, **kwargs):
self.content_type = ContentType.objects.get_for_model(self)
return (
f"{self.content_type.app_label}.{self.content_type.model_class().__name__}"
)
2 changes: 2 additions & 0 deletions tests/test_grapple.py
Original file line number Diff line number Diff line change
Expand Up @@ -1592,6 +1592,7 @@ def test_snippets(self):
{
snippets {
snippetType
contentType
}
}
"""
Expand All @@ -1604,3 +1605,4 @@ def test_snippets(self):

snippets_data = executed["data"]["snippets"]
self.assertEqual(snippets_data[0]["snippetType"], "Advert")
self.assertEqual(snippets_data[0]["contentType"], "testapp.Advert")

0 comments on commit 09ea03b

Please sign in to comment.