Skip to content

Commit

Permalink
Add webpquality support for renditions (#130)
Browse files Browse the repository at this point in the history
* feat: add webpquality support for renditions

* feat: add singular queries for images and documents

* fix: resolve arguments correctly
  • Loading branch information
ruisaraiva19 committed Oct 17, 2020
1 parent 4b18431 commit e738616
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 28 deletions.
1 change: 1 addition & 0 deletions docs/general-usage/graphql-types.rst
Expand Up @@ -130,6 +130,7 @@ need for Gatsby Image features to work (see Handy Fragments page for more info):
format: String
bgcolor: String
jpegquality: Int
webpquality: Int
): ImageRenditionObjectType


Expand Down
20 changes: 10 additions & 10 deletions example/images/models.py
Expand Up @@ -8,10 +8,10 @@
class CustomImage(AbstractImage):
admin_form_fields = Image.admin_form_fields

def custom_image_property(self):
def custom_image_property(self, info, **kwargs):
return "Image Model!"

graphql_fields = (GraphQLString("custom_image_property"),)
graphql_fields = (GraphQLString("custom_image_property", required=True),)


class CustomImageRendition(AbstractRendition):
Expand All @@ -22,15 +22,15 @@ class CustomImageRendition(AbstractRendition):
class Meta:
unique_together = (("image", "filter_spec", "focal_point_key"),)

def custom_rendition_property(self):
def custom_rendition_property(self, info, **kwargs):
return "Rendition Model!"

graphql_fields = (
GraphQLString("custom_rendition_property"),
GraphQLInt("id"),
GraphQLString("url"),
GraphQLString("width"),
GraphQLString("height"),
GraphQLImage("image"),
GraphQLString("file"),
GraphQLString("custom_rendition_property", required=True),
GraphQLInt("id", required=True),
GraphQLString("url", required=True),
GraphQLString("width", required=True),
GraphQLString("height", required=True),
GraphQLImage("image", required=True),
GraphQLString("file", required=True),
)
12 changes: 10 additions & 2 deletions grapple/types/documents.py
Expand Up @@ -33,7 +33,7 @@ class Meta:
file_hash = graphene.String()
url = graphene.String(required=True)

def resolve_url(self, info):
def resolve_url(self, info, **kwargs):
"""
Get document file url.
"""
Expand All @@ -46,11 +46,19 @@ def DocumentsQuery():
model_type = registry.documents[mdl]

class Mixin:
document = graphene.Field(model_type, id=graphene.ID())
documents = QuerySetList(
graphene.NonNull(model_type), enable_search=True, required=True
)

# Return all pages, ideally specific.
# Return one document.
def resolve_document(self, info, id, **kwargs):
try:
return mdl.objects.get(pk=id)
except BaseException:
return None

# Return all documents.
def resolve_documents(self, info, **kwargs):
return resolve_queryset(mdl.objects.all(), info, **kwargs)

Expand Down
21 changes: 14 additions & 7 deletions grapple/types/images.py
Expand Up @@ -13,20 +13,21 @@


class BaseImageObjectType(graphene.ObjectType):
id = graphene.ID(required=True)
width = graphene.Int(required=True)
height = graphene.Int(required=True)
src = graphene.String(required=True, deprecation_reason="Use the `url` attribute")
url = graphene.String(required=True)
aspect_ratio = graphene.Float(required=True)
sizes = graphene.String(required=True)

def resolve_url(self, info):
def resolve_url(self, info, **kwargs):
"""
Get the uploaded image url.
"""
return get_media_item_url(self)

def resolve_src(self, info):
def resolve_src(self, info, **kwargs):
"""
Deprecated. Use the `url` attribute.
"""
Expand All @@ -38,14 +39,11 @@ def resolve_aspect_ratio(self, info, **kwargs):
"""
return self.width / self.height

def resolve_sizes(self, info):
def resolve_sizes(self, info, **kwargs):
return "(max-width: {}px) 100vw, {}px".format(self.width, self.width)


class ImageRenditionObjectType(DjangoObjectType, BaseImageObjectType):
id = graphene.ID(required=True)
url = graphene.String(required=True)

class Meta:
model = WagtailImageRendition

Expand All @@ -70,6 +68,7 @@ class ImageObjectType(DjangoObjectType, BaseImageObjectType):
format=graphene.String(),
bgcolor=graphene.String(),
jpegquality=graphene.Int(),
webpquality=graphene.Int(),
)
src_set = graphene.String(sizes=graphene.List(graphene.Int))

Expand Down Expand Up @@ -130,12 +129,20 @@ def ImagesQuery():
mdl_type = get_image_type()

class Mixin:
image = graphene.Field(mdl_type, id=graphene.ID())
images = QuerySetList(
graphene.NonNull(mdl_type), enable_search=True, required=True
)
image_type = graphene.String(required=True)

# Return all pages, ideally specific.
# Return one image.
def resolve_image(self, info, id, **kwargs):
try:
return mdl.objects.get(pk=id)
except BaseException:
return None

# Return all images.
def resolve_images(self, info, **kwargs):
return resolve_queryset(mdl.objects.all(), info, **kwargs)

Expand Down
2 changes: 1 addition & 1 deletion grapple/types/media.py
Expand Up @@ -16,7 +16,7 @@ class Meta:

url = graphene.String(required=True)

def resolve_url(self, info):
def resolve_url(self, info, **kwargs):
"""
Get Media file url.
"""
Expand Down
7 changes: 3 additions & 4 deletions grapple/types/pages.py
Expand Up @@ -6,7 +6,6 @@
from wagtail_headless_preview.signals import preview_update
from graphene_django.types import DjangoObjectType
from graphql.error import GraphQLLocatedError
from graphql.execution.base import ResolveInfo

try:
from channels.routing import route_class
Expand Down Expand Up @@ -54,7 +53,7 @@ class PageInterface(graphene.Interface):
)

@classmethod
def resolve_type(cls, instance, info: ResolveInfo):
def resolve_type(cls, instance, info, **kwargs):
"""
If model has a custom Graphene Node type in registry then use it,
otherwise use base page type.
Expand All @@ -65,7 +64,7 @@ def resolve_type(cls, instance, info: ResolveInfo):
else:
return Page

def resolve_content_type(self, info: ResolveInfo):
def resolve_content_type(self, info, **kwargs):
self.content_type = ContentType.objects.get_for_model(self)
return (
self.content_type.app_label + "." + self.content_type.model_class().__name__
Expand Down Expand Up @@ -144,7 +143,7 @@ def resolve_ancestors(self, info, **kwargs):
self.get_ancestors().live().public().specific(), info, **kwargs
)

def resolve_seo_title(self, info):
def resolve_seo_title(self, info, **kwargs):
"""
Get page's SEO title. Fallback to a normal page's title if absent.
"""
Expand Down
2 changes: 1 addition & 1 deletion grapple/types/streamfield.py
Expand Up @@ -283,7 +283,7 @@ class RichTextBlock(graphene.ObjectType):
class Meta:
interfaces = (StreamFieldInterface,)

def resolve_value(self, value):
def resolve_value(self, info, **kwargs):
# Allow custom markup for RichText
return render_to_string(
"wagtailcore/richtext.html", {"html": expand_db_html(self.value.source)}
Expand Down
6 changes: 3 additions & 3 deletions grapple/types/tags.py
Expand Up @@ -18,10 +18,10 @@ class TagObjectType(graphene.ObjectType):
tag_id = graphene.ID(name="id", required=True)
name = graphene.String(required=True)

def resolve_tag_id(self, info):
def resolve_tag_id(self, info, **kwargs):
return self.id

def resolve_name(self, info):
def resolve_name(self, info, **kwargs):
return self.name


Expand All @@ -32,7 +32,7 @@ class Mixin:
graphene.NonNull(TagObjectType), required=True, enable_search=False
)

def resolve_tag(self, info, id):
def resolve_tag(self, info, id, **kwargs):
try:
return Tag.objects.get(pk=id)
except BaseException:
Expand Down

0 comments on commit e738616

Please sign in to comment.