Giter Site home page Giter Site logo

tr11 / wagtail-graphql Goto Github PK

View Code? Open in Web Editor NEW
38.0 3.0 17.0 134 KB

App to automatically add GraphQL support to a Wagtail website

License: MIT License

Python 99.24% HTML 0.58% Shell 0.17%
graphql wagtail wagtail-apps graphene wagtail-website wagtail-graphql

wagtail-graphql's Introduction

    


wagtail-graphql

An app to automatically add GraphQL support to a Wagtail website

This Wagtail app adds GraphQL types to other Wagtail apps. The objective is for this library to interact with an existing website in a generic way and with minimal effort. In particular, it makes minimal assumptions about the structure of the website to allow for a generic API.

Installing / Getting started

To install as a general app:

pip install wagtail-graphql

Add it together with graphene_django to the Django INSTALLED_APPS:

INSTALLED_APPS = [
    ...
    'wagtail_graphql',
    'graphene_django',
    ...
]

Initial Configuration

Add the required graphene schema GRAPHENE and a GRAPHQL_API dictionary. Include all the Wagtail apps the library should generate bindings to in the APPS list and optionally specify the prefix for each app in PREFIX. To remove a leading part of all the urls for a specific site, specify the URL_PREFIX parameter for each needed host.

GRAPHENE = {
    'SCHEMA': 'wagtail_graphql.schema.schema',
}

GRAPHQL_API = {
    'APPS': [
        'home'
    ],
    'PREFIX': {
        'home': ''        # optional, prefix for all the app classes generated by the wrapper
    },
    'URL_PREFIX': {
        'localhost': '/home'   # optional, read from the site information if not specified 
    }
}

The example above generates bindings for the home app, . Every url in this example will be stripped of the initial /home substring.

Finally, set up the GraphQL views in the project urls.py. For example, to add two endpoints for GraphQL and the GraphiQL IDE:

from django.views.decorators.csrf import csrf_exempt
from graphene_django.views import GraphQLView

urlpatterns = [
    ...
    url(r'^api/graphql', csrf_exempt(GraphQLView.as_view())),
    url(r'^api/graphiql', csrf_exempt(GraphQLView.as_view(graphiql=True, pretty=True)),
    ...
]

Note that the urls above need to appear before the wagtail_urls catchall entry.

Images

To be able to generate urls for images the following also needs to be included in the project's urls.py:

from wagtail.images.views.serve import ServeView

urlpatterns = [
    ...
    url(r'^images/([^/]*)/(\d*)/([^/]*)/[^/]*$', ServeView.as_view(), name='wagtailimages_serve'),
    ...
]

Multi-site configuration

This library works transparently with a multi-site Wagtail install without any extra configuration required. To strip a custom leading prefix for each site, specify each host in the URL_PREFIX. For exaple, for two hosts host1.example.com and host2.example.com:

GRAPHQL_API = {
    ...
    'URL_PREFIX': {
        'host1.example.com': '/prefix1',
        'host2.example.com': '/prefix2'
    }
    ...
}

Note that the prefix for a site is taken from the root page url if a host is not included in the URL_PREFIX dictionary.

Developing

To develop this library, download the source code and install a local version in your Wagtail website.

Features

This project is intended to require minimal configuration and interaction. It currently supports

Contributing

If you'd like to contribute, please fork the repository and use a feature branch. Pull requests are welcome.

Links

Licensing

The code in this project is licensed under MIT license.

wagtail-graphql's People

Contributors

iamnatch avatar tr11 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

wagtail-graphql's Issues

Package isn't published on PyPi

Library looks awesome but I can't actually install it in my project:

$ pip3 install wagtail-graphql
Collecting wagtail-graphql
  Could not find a version that satisfies the requirement wagtail-graphql (from versions: )
No matching distribution found for wagtail-graphql

Installing via git also doesn't work because there is no setup.py:

$ pip3 install git+https://github.com/tr11/wagtail-graphql
Collecting git+https://github.com/tr11/wagtail-graphql
  Cloning https://github.com/tr11/wagtail-graphql to /tmp/pip-req-build-wp0q110w
  Installing build dependencies ... done
    Complete output from command python setup.py egg_info:
    Traceback (most recent call last):
      File "<string>", line 1, in <module>
      File "/home/vagrant/.virtualenvs/torchbox/lib/python3.6/tokenize.py", line 452, in open
        buffer = _builtin_open(filename, 'rb')
    FileNotFoundError: [Errno 2] No such file or directory: '/tmp/pip-req-build-wp0q110w/setup.py'

    ----------------------------------------
Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-req-build-wp0q110w/

Streamfield doesnt work

Using streamfield blocks of any sort causes an error. I've tried this on my installation and the 'test_project' but I get the issue on both...

# models.py
from wagtail.core.fields import StreamField
from wagtail.core.models import Page
from wagtail.core.blocks import CharBlock
from wagtail.admin.edit_handlers import FieldPanel, StreamFieldPanel


class HomePage(Page):
    streamfield = StreamField([
        ('h2', CharBlock(icon="title", classname="title")),
    ], null=True, blank=True)

    content_panels = [
        FieldPanel('title', classname="full title"),
        StreamFieldPanel('streamfield'),
    ]

Error: Could not import 'wagtail_graphql.schema.schema' for Graphene setting 'SCHEMA'. AttributeError: 'GraphQLScalarType' object has no attribute 'graphene_type'.

RichTextBlock link and embed HTML isn't rewritten

It seems that any RichTextBlock instances are passing their HTML string values straight through the streamfield resolver. This results in Wagtail's proprietary faux-HTML being exposed.

E.g. internal links in Wagtail's RichTextBlock are stored as such:

<a id="7" linktype="page">link to somewhere internal</a>

When Wagtail renders this to a final template the HTML is normally rewritten using some specific rewriter functions. The above example, when rewritten, would then become something like:

<a href="/somewhere">link to somewhere internal</a>

wagtail-graphql doesn't seem to do this.

There is a handy helper function available called expand_db_html which could be leveraged.

Or maybe I'm not doing it right? Should I register these rewriters specifically?

ClusterTaggableManager support

I used the official recipes for adding tagging to my project which uses

from modelcluster.contrib.taggit import ClusterTaggableManager

I've noticed in wagtail-graphql/wagtail_graphql/types/core.py you have support for TaggableManager, could you possibly add ClusterTaggableManager in as well?

I'm just requesting this since the method I've used for tags is the way Wagtail suggests, so it should be a common way of doing so.

Add Dataloader

Dataloader or some other batching and/or caching mechanism.

Support multiple sites

Current implementation assumes a single site. Add support for multiple sites with correct permissions.

Support for python 3.8

Trying to upgrade my app to use python 3.8 and this is the only dependency that doesn't allow me to do that. From a quick look at the source code I think it is totally compatible with 3.8, no need to have <3.8 in the requirements

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.