Skip to content

Commit bb354c9

Browse files
committed
Update schema and image types
1 parent 23f4bd6 commit bb354c9

File tree

5 files changed

+51
-27
lines changed

5 files changed

+51
-27
lines changed

grapple/models.py

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -29,34 +29,30 @@ def __init__(self, field_name: str, field_type: type = None):
2929

3030

3131
def GraphQLString(field_name: str):
32-
class Mixin(GraphQLField):
33-
def __init__(self):
34-
super().__init__(field_name, graphene.String)
35-
32+
def Mixin():
33+
return GraphQLField(field_name, graphene.String)
34+
3635
return Mixin
3736

3837

3938
def GraphQLFloat(field_name: str):
40-
class Mixin(GraphQLField):
41-
def __init__(self):
42-
super().__init__(field_name, graphene.Float)
43-
39+
def Mixin():
40+
return GraphQLField(field_name, graphene.Float)
41+
4442
return Mixin
4543

4644

4745
def GraphQLInt(field_name: str):
48-
class Mixin(GraphQLField):
49-
def __init__(self):
50-
super().__init__(field_name, graphene.Int)
51-
46+
def Mixin():
47+
return GraphQLField(field_name, graphene.Int)
48+
5249
return Mixin
5350

5451

5552
def GraphQLBoolean(field_name: str):
56-
class Mixin(GraphQLField):
57-
def __init__(self):
58-
super().__init__(field_name, graphene.Boolean)
59-
53+
def Mixin():
54+
return GraphQLField(field_name, graphene.Boolean)
55+
6056
return Mixin
6157

6258

@@ -76,18 +72,18 @@ def __init__(self):
7672

7773
def GraphQLStreamfield(field_name: str):
7874
from .types.streamfield import StreamFieldInterface
75+
def Mixin():
76+
return GraphQLField(field_name, graphene.List(StreamFieldInterface))
7977

80-
class Mixin(GraphQLField):
81-
def __init__(self):
82-
super().__init__(field_name, graphene.List(StreamFieldInterface))
83-
8478
return Mixin
8579

8680

8781
def GraphQLImage(field_name: str):
88-
from wagtail.images import get_image_model_string
89-
90-
return GraphQLForeignKey(field_name, get_image_model_string())
82+
def Mixin():
83+
from .types.images import get_image_type, ImageObjectType
84+
return GraphQLField(field_name, graphene.Field(ImageObjectType))
85+
86+
return Mixin
9187

9288

9389
def GraphQLDocument(field_name: str):
@@ -206,7 +202,9 @@ def serve_preview(self, request, mode_name):
206202
"grapple/preview.html",
207203
{"preview_url": self.get_preview_url(page_preview.token)},
208204
)
209-
response.set_cookie(key="used-token", value=page_preview.token)
205+
206+
# Set cookie that auto-expires after 5mins
207+
response.set_cookie(key="used-token", value=page_preview.token, max_age=300)
210208
return response
211209

212210
@classmethod

grapple/schema.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import graphene
2+
from graphql.validation.rules import NoUnusedFragments, specified_rules
23

34
from .actions import import_apps
45
from .types.pages import PagesQuery, PagesSubscription
@@ -17,12 +18,25 @@
1718
import_apps()
1819
register_streamfield_blocks()
1920

20-
2121
"""
2222
Root schema object that graphene is pointed at.
2323
It inherits its queries from each of the specific type mixins.
2424
"""
2525

26+
# HACK: Remove NoUnusedFragments validator
27+
# Due to the way previews work on the frontend, we need to pass all
28+
# fragments into the query even if they're not used.
29+
# This would usually cause a validation error. There doesn't appear
30+
# to be a nice way to disable this validator so we monkey-patch it instead.
31+
32+
33+
# We need to update specified_rules in-place so the change appears
34+
# everywhere it's been imported
35+
36+
specified_rules[:] = [
37+
rule for rule in specified_rules
38+
if rule is not NoUnusedFragments
39+
]
2640

2741
class Query(
2842
graphene.ObjectType,

grapple/types/images.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,10 @@ class ImageObjectType(DjangoObjectType):
4343
bgcolor=graphene.String(),
4444
jpegquality=graphene.Int(),
4545
)
46-
traced_SVG = graphene.String()
46+
traced_SVG = graphene.String(name='tracedSVG')
4747
base64 = graphene.String()
48+
aspect_ratio = graphene.Float()
49+
sizes = graphene.String()
4850

4951
class Meta:
5052
model = WagtailImage
@@ -101,6 +103,15 @@ def resolve_traced_SVG(self, info):
101103
with open(svg_trace_image, "r") as svgFile:
102104
return "data:image/svg+xml," + urllib.parse.quote(svgFile.read())
103105

106+
def resolve_aspect_ratio(self, info, **kwargs):
107+
"""
108+
Calculate aspect ratio for Gatsby Image.
109+
"""
110+
return self.width / self.height
111+
112+
def resolve_sizes(self, info):
113+
return "(max-width: {}px) 100vw, {}px".format(self.width, self.width)
114+
104115

105116
def ImagesQuery():
106117
from wagtail.images import get_image_model

grapple/types/pages.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
class PageInterface(graphene.Interface):
1616
id = graphene.ID()
1717
url = graphene.String()
18+
url_path = graphene.String()
1819
slug = graphene.String()
1920
depth = graphene.Int()
2021
page_type = graphene.String()

setup.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ name = wagtail-grapple
33
author = Nathan Horrigan
44
author_email = [email protected]
55
description = A django app that speeds up and simplifies implementing a GraphQL endoint!
6-
version = 0.0.5
6+
version = 0.1.2
77
url = https://github.com/NathHorrigan/wagtail-grapple
88
keywords =
99
wagtail

0 commit comments

Comments
 (0)