torchbox / wagtail-grapple

A Wagtail app that makes building GraphQL endpoints a breeze!
https://wagtail-grapple.readthedocs.io/en/latest/
Other
151 stars 57 forks source link
django graphene graphql graphql-endpoint hacktoberfest python wagtail wagtail-cms wagtail-graphql wagtail-grapple wagtail-plugin

A red g with a grapple hook

Wagtail Grapple

Build status Ruff PyPi pre-commit.ci status

A library to build GraphQL endpoints easily so you can grapple your Wagtail data from anywhere!

Explore the docs » · Report Bug · Request Feature

About The Project

There is a range of GraphQL packages for Python and specifically Django. However, getting these packages to work out of the box with an existing infrastructure without errors isn't as easy to come by.

The purpose of Grapple is to be able to build GraphQL endpoints on a model by model basis as quickly as possible. The setup and configuration have been designed to be as simple but also provide the best features; No complex serializers need to be written - just add a graphql_fields list to your model and away you go (although if you want to go deeper you can!).

Features

Built With

This library is an abstraction upon and relies heavily on Graphene & Graphene Django.

Getting Started

Getting Grapple installed is designed to be as simple as possible!

Prerequisites

Python >= 3.8
Wagtail >= 5.2
Django >= 4.2

Installation

Install using pip

python -m pip install wagtail_grapple

Add the following to the INSTALLED_APPS list in your Wagtail settings file:

INSTALLED_APPS = [
    # ...
    "grapple",
    "graphene_django",
    # ...
]

Add the following to the bottom of the same settings file, where each key is the app you want to this library to scan and the value is the prefix you want to give to GraphQL types (you can usually leave this blank):

# Grapple config:
GRAPHENE = {"SCHEMA": "grapple.schema.schema"}
GRAPPLE = {
    "APPS": ["home"],
}

Add the GraphQL URLs to your urls.py:

from django.urls import include, path
from grapple import urls as grapple_urls

# ...
urlpatterns = [
    # ...
    path("api/", include(grapple_urls)),
    # ...
]

Done! Now you can proceed onto configuring your models to generate GraphQL types that adopt their structure :tada: Your GraphQL endpoint is available at http://localhost:8000/api/graphql/

Usage

Here is a GraphQL model configuration for the default page from the Wagtail docs:

# ...
from grapple.models import GraphQLString, GraphQLStreamfield

class BlogPage(Page):
    author = models.CharField(max_length=255)
    date = models.DateField("Post date")
    body = StreamField(
        [
            ("heading", blocks.CharBlock(classname="full title")),
            ("paragraph", blocks.RichTextBlock()),
            ("image", ImageChooserBlock()),
        ]
    )

    content_panels = Page.content_panels + [
        FieldPanel("author"),
        FieldPanel("date"),
        StreamFieldPanel("body"),
    ]

    # Note these fields below:
    graphql_fields = [
        GraphQLString("heading"),
        GraphQLString("date"),
        GraphQLString("author"),
        GraphQLStreamfield("body"),
    ]

For more examples, please refer to the Documentation

Contributing

Contributions are what make the open source community such an amazing place to learn, inspire, and create. Any contributions you make are greatly appreciated.

  1. Fork the project
  2. Create your feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add some amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

Local development

With Postgres

Compatibility

Wagtail Grapple supports:

License

Distributed under the MIT License. See LICENSE for more information.

Inspired by