torchbox / wagtail-grapple

A Wagtail app that makes building GraphQL endpoints a breeze!
https://wagtail-grapple.readthedocs.io/en/latest/
Other
152 stars 57 forks source link

errors while using 'parent' attribute #41

Closed indirectlylit closed 4 years ago

indirectlylit commented 4 years ago

I have a couple Page subclasses, BlogPage and BlogIndexPage as described in the tutorial.

Cannot get parent

A query like this returns correctly a BlogPage:

{
  page(id:8) {
    urlPath
  }
}
{
  "data": {
    "page": {
      "urlPath": "/blog/another-post/",
    }
  }
}

However attempting to get the parent does not work:

{
  page(id:8) {
    urlPath
    parent {
      urlPath
    }
  }
}
Traceback (most recent call last):
  File "/Users/d/Projects/portfolio/wagtail/.venv/lib/python3.7/site-packages/promise/promise.py", line 489, in _resolve_from_executor
    executor(resolve, reject)
  File "/Users/d/Projects/portfolio/wagtail/.venv/lib/python3.7/site-packages/promise/promise.py", line 756, in executor
    return resolve(f(*args, **kwargs))
  File "/Users/d/Projects/portfolio/wagtail/.venv/lib/python3.7/site-packages/graphql/execution/middleware.py", line 75, in make_it_promise
    return next(*args, **kwargs)
  File "/Users/d/Projects/portfolio/wagtail-grapple/grapple/types/pages.py", line 59, in resolve_parent
    return resolve_queryset(self.get_parent().specific(), info, **kwargs)
graphql.error.located_error.GraphQLLocatedError: 'BlogIndexPage' object is not callable

This code is giving the error:

https://github.com/torchbox/wagtail-grapple/blob/d39ba1bd39f7079bdb8a41fab2ae823df7f694c6/grapple/types/pages.py#L54-L59

Updating line 59 seems to remove the error:

    return resolve_queryset(self.get_parent().specific, info, **kwargs)

Accessing parent of a root page

An unhandled exception is thrown if parent is accessed on a root page. For example, this query:

{
  pages {
    urlPath
    parent {
      urlPath
    }
  }
}

gives this error:

Traceback (most recent call last):
  File "/Users/d/Projects/portfolio/wagtail/.venv/lib/python3.7/site-packages/promise/promise.py", line 489, in _resolve_from_executor
    executor(resolve, reject)
  File "/Users/d/Projects/portfolio/wagtail/.venv/lib/python3.7/site-packages/promise/promise.py", line 756, in executor
    return resolve(f(*args, **kwargs))
  File "/Users/d/Projects/portfolio/wagtail/.venv/lib/python3.7/site-packages/graphql/execution/middleware.py", line 75, in make_it_promise
    return next(*args, **kwargs)
  File "/Users/d/Projects/portfolio/wagtail-grapple/grapple/types/pages.py", line 59, in resolve_parent
    return resolve_queryset(self.get_parent().specific, info, **kwargs)
graphql.error.located_error.GraphQLLocatedError: 'NoneType' object has no attribute 'specific'
zerolab commented 4 years ago

Hey @ indirectlylit,

Thank you for the detailed bug report! The fix for line 59 is indeed the correct one. get_parent() returns the parent node (a Wagtail Page object).

on, the root page parent issue -- needs something like

from graphql.error.located_error import GraphQLLocatedError

#...

def resolve_parent(self, info, **kwargs): 
    """ 
    Resolves the parent node of current page node.
    Docs: https://docs.wagtail.io/en/stable/reference/pages/model_reference.html#wagtail.core.models.Page.get_parent
    """

    try:
        return resolve_queryset(self.get_parent().specific, info, **kwargs) 
    except GraphQLLocatedError:
        return WagtailPage.objects.none()

and a test for getting the parent. Would you be able to sumbmit a PR?

indirectlylit commented 4 years ago

Thanks @zerolab - Grapple is a great tool and a big improvement over manually creating graphql schemas. Nice work!

I'm currently in a quick evaluation & prototyping phase so unfortunately don't have time to submit PRs at the moment. Would be happy to contribute in the future if we end up using the package though.

zerolab commented 4 years ago

Thank you. All credit goes to @NathHorrigan for the package.

Hope to get some time to sort this out today. Good luck with the prototyping!