Skip to content
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

SvgChooserBlock field to graphql #16

Open
warcivil opened this issue Apr 20, 2022 · 3 comments
Open

SvgChooserBlock field to graphql #16

warcivil opened this issue Apr 20, 2022 · 3 comments

Comments

@warcivil
Copy link

warcivil commented Apr 20, 2022

is there any way to add the SvgChooserBlock field to graphql?

@Aleksi44
Copy link
Owner

Hi @warcivil

I've never tried it but I think it's possible.

Can you give me more context?
Libraries used for this?
Attempts and code you tried?

@warcivil
Copy link
Author

I use a wagtail and a wagtail grapple, I create a block and specify for example image=Svg ChooserBlock()
actually this is all if you need additional information, write
thank you!

@kbayliss
Copy link

kbayliss commented Jul 28, 2022

Hey @warcivil ,

Official SVG support is coming to Wagtail soon (it should be in version five), though you can use the below with wagtail-grapple and wagtailsvg.

(note: this example is for wagtail>=3, though if you adjust the import paths and change WAGTAILADMIN_BASE_URL for BASE_URL then it'll work for wagtail<3 too)

In graphene_fields.py:

import graphene
from django.conf import settings
from grapple.types import collections, tags

class SVGFieldObjectType(graphene.ObjectType):
    title = graphene.String(required=True)
    filename = graphene.String(required=True)
    url = graphene.String(required=True)
    url_path = graphene.String(required=True)
    tags = graphene.List(tags.TagObjectType)
    collection = graphene.Field(
        collections.CollectionObjectType, required=True
    )

    def resolve_tags(self, info, **kwargs):
        return self.tags.all()

    def resolve_url(self, info, **kwargs):
        # URL path including domain name
        if self.url[0] == "/":
            return f"{settings.WAGTAILADMIN_BASE_URL}{self.url}"
        return self.url

    def resolve_url_path(self, info, **kwargs):
        # URL path excluding domain
        return self.url

In blocks.py

from grapple.helpers import register_streamfield_block
from grapple.models import GraphQLField
from wagtailsvg.blocks import SvgChooserBlock

from your_project import graphene_fields


@register_streamfield_block
class YourBlock():
    svg = SvgChooserBlock(label="SVG")

    graphql_fields = [
        GraphQLField(
            field_name="svg",
            field_type=graphene_fields.SVGFieldObjectType,
            required=True,
        ),
    ]

In queries.py:

import graphene
from grapple.types.structures import QuerySetList
from grapple.utils import resolve_queryset
from wagtailsvg.models import Svg

from your_project import graphene_fields


class SVGQuery:
    svg = graphene.Field(
        graphene_fields.SVGFieldObjectType,
        id=graphene.ID(),
    )

    svgs = QuerySetList(
        graphene.NonNull(graphene_fields.SVGFieldObjectType),
        enable_search=True,
        required=True,
        collection=graphene.Argument(
            graphene.ID, description="Filter by collection ID"
        ),
    )

    def resolve_svg(self, info, id, **kwargs):
        try:
            return Svg.objects.filter(
                collection__view_restrictions__isnull=True
            ).get(pk=id)
        except BaseException:
            return None

    def resolve_svgs(self, info, **kwargs):
        qs = Svg.objects.filter(collection__view_restrictions__isnull=True)
        return resolve_queryset(qs, info, **kwargs)

In wagtail_hooks.py:

from wagtail import hooks

from your_project import queries


@hooks.register("register_schema_query")
def register_extra_schema_queries(query_mixins):
    query_mixins += [
        queries.SVGQuery,
    ]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants