Skip to content

Commit

Permalink
Fix rendition definition (#337)
Browse files Browse the repository at this point in the history
* Register the correct object type for image renditions

Co-authored-by: engAmirEng <[email protected]>

* Return the actual rendition in resolve_rendition

Co-authored-by: engAmirEng <[email protected]>

* Clean up the rendition type definition

BaseImageObjectType contains definitions for image fields/properties that are not relevant on renditions

* Replace self with instance for object type or parent for query

* Tidy up image/rendition schema in docs

---------

Co-authored-by: engAmirEng <[email protected]>
  • Loading branch information
zerolab and engAmirEng authored Jul 7, 2023
1 parent 27a2791 commit 10d56a4
Show file tree
Hide file tree
Showing 5 changed files with 131 additions and 148 deletions.
31 changes: 19 additions & 12 deletions docs/general-usage/graphql-types.rst
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ need for Gatsby Image features to work (see Handy Fragments page for more info):
::

id: ID!
collection: CollectionObjectType!
title: String!
file: String!
width: Int!
Expand All @@ -122,12 +123,10 @@ need for Gatsby Image features to work (see Handy Fragments page for more info):
focalPointHeight: Int
fileSize: Int
fileHash: String!
renditions: [ImageRenditionObjectType]
src: String
srcSet(
sizes: [Int]
format: String
): String
url: String!
aspectRatio: Float!
sizes: String!
tags: [TagObjectType!]!
rendition(
max: String
min: String
Expand All @@ -139,21 +138,29 @@ need for Gatsby Image features to work (see Handy Fragments page for more info):
jpegquality: Int
webpquality: Int
): ImageRenditionObjectType
srcSet(
sizes: [Int]
format: String
): String



ImageRenditions are useful feature in Wagtail and they exist in Grapple as well
the ``ImageRenditionObjectType`` provides the following fields:

::

id: ID
url: String
id: ID!
filter_spec = String!
file: String!
width: Int
height: Int
aspectRatio: Float!
sizes: String!
width: Int!
height: Int!
focal_point_key = String!
image: ImageObjectType!
focalPoint: String
url: String!
alt = String!
backgroundPositionStyle = String!


DocumentObjectType
Expand Down
71 changes: 27 additions & 44 deletions grapple/actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
from .registry import registry
from .settings import grapple_settings
from .types.documents import DocumentObjectType
from .types.images import ImageObjectType
from .types.images import ImageObjectType, ImageRenditionObjectType
from .types.pages import Page, PageInterface
from .types.rich_text import RichText as RichTextType
from .types.streamfield import generate_streamfield_union
Expand Down Expand Up @@ -66,7 +66,7 @@ def import_apps():
add_app(name, prefix)
registry.apps.append(name)

# Register any 'decorated' streamfield structs.
# Register any 'decorated' StreamField structs.
for streamfield_type in streamfield_types:
cls = streamfield_type["cls"]
base_type = streamfield_type["base_type"]
Expand Down Expand Up @@ -98,7 +98,7 @@ def add_app(app_label: str, prefix: str = ""):
# Create a collection of models of standard models (Pages, Images, Documents).
models = list(app.get_models())

# Add snippet models to model collection.
# Add snippet models to the model collection.
for snippet in get_snippet_models():
if snippet._meta.app_label == app_label:
models.append(snippet)
Expand All @@ -122,7 +122,7 @@ def register_model(cls: type, type_prefix: str):
elif issubclass(cls, AbstractImage):
register_image_model(cls, type_prefix)
elif issubclass(cls, AbstractRendition):
register_image_model(cls, type_prefix)
register_image_rendition_model(cls, type_prefix)
elif has_wagtail_media and issubclass(cls, AbstractMedia):
register_media_model(cls, type_prefix)
elif issubclass(cls, (BaseSiteSetting, BaseGenericSetting)):
Expand Down Expand Up @@ -490,11 +490,9 @@ def register_page_model(cls: Type[WagtailPage], type_prefix: str):
if cls in registry.pages:
return

# Create a GQL type derived from page model.
page_node_type = build_node_type(cls, type_prefix, PageInterface, Page)

# Add page type to registry.
if page_node_type:
# Create a GQL type derived from the page model.
if page_node_type := build_node_type(cls, type_prefix, PageInterface, Page):
# Add page type to registry.
registry.pages[cls] = page_node_type


Expand All @@ -509,11 +507,11 @@ def register_document_model(cls: Type[AbstractDocument], type_prefix: str):
if cls in registry.documents:
return

# Create a GQL type derived from document model.
document_node_type = build_node_type(cls, type_prefix, None, DocumentObjectType)

# Add document type to registry.
if document_node_type:
# Create a GQL type derived from the document model.
if document_node_type := build_node_type(
cls, type_prefix, None, DocumentObjectType
):
# Add document type to registry.
registry.documents[cls] = document_node_type


Expand All @@ -528,11 +526,9 @@ def register_image_model(cls: Type[AbstractImage], type_prefix: str):
if cls in registry.images:
return

# Create a GQL type derived from image model.
image_node_type = build_node_type(cls, type_prefix, None, ImageObjectType)

# Add image type to registry.
if image_node_type:
# Create a GQL type derived from the image model.
if image_node_type := build_node_type(cls, type_prefix, None, ImageObjectType):
# Add image type to registry.
registry.images[cls] = image_node_type


Expand All @@ -543,16 +539,14 @@ def register_image_rendition_model(cls: Type[AbstractRendition], type_prefix: st
needs to be set in settings.
"""

# Avoid gql type duplicates
if cls in registry.images:
return

# Create a GQL type derived from image rendition model.
image_node_type = build_node_type(cls, type_prefix, None, AbstractRendition)

# Add image type to registry.
if image_node_type:
registry.images[cls] = image_node_type
# Create a GQL type derived from the image rendition model.
if rendition_type := build_node_type(
cls, type_prefix, None, ImageRenditionObjectType
):
registry.images[cls] = rendition_type


def register_media_model(cls: Type[AbstractMedia], type_prefix: str):
Expand All @@ -562,15 +556,11 @@ def register_media_model(cls: Type[AbstractMedia], type_prefix: str):
needs to be set in settings.
"""

# Avoid gql type duplicates
if cls in registry.media:
return

# Create a GQL type derived from media model.
media_node_type = build_node_type(cls, type_prefix, None, MediaObjectType)

# Add media type to registry.
if media_node_type:
# Create a GQL type derived from the media model.
if media_node_type := build_node_type(cls, type_prefix, None, MediaObjectType):
registry.media[cls] = media_node_type


Expand All @@ -581,15 +571,11 @@ def register_settings_model(
Create a graphene node type for a settings page.
"""

# Avoid gql type duplicates
if cls in registry.settings:
return

# Create a GQL type derived from document model.
settings_node_type = build_node_type(cls, type_prefix, None)

# Add image type to registry.
if settings_node_type:
# Create a GQL type that for the Settings model
if settings_node_type := build_node_type(cls, type_prefix, None):
registry.settings[cls] = settings_node_type


Expand All @@ -598,7 +584,6 @@ def register_snippet_model(cls: Type[models.Model], type_prefix: str):
Create a graphene type for a snippet model.
"""

# Avoid gql type duplicates
if cls in registry.snippets:
return

Expand All @@ -611,16 +596,14 @@ def register_snippet_model(cls: Type[models.Model], type_prefix: str):

def register_django_model(cls: Type[models.Model], type_prefix: str):
"""
Create a graphene type for (non-specific) django model.
Create a graphene type for (non-specific) Django model.
Used for Orderables and other foreign keys.
"""

# Avoid gql type duplicates
if cls in registry.django_models:
return

# Create a GQL type that implements Snippet Interface
django_node_type = build_node_type(cls, type_prefix, None)

if django_node_type:
# Create a GQL type for the non-specific Django model.
if django_node_type := build_node_type(cls, type_prefix, None):
registry.django_models[cls] = django_node_type
Loading

0 comments on commit 10d56a4

Please sign in to comment.