import graphene
from django.http import Http404
from graphql import GraphQLError
from wagtail.core.models import Page

from . import types


class Query(graphene.ObjectType):
    page = graphene.Field(types.Page, path=graphene.String())
    all_pages = graphene.List(types.Page, path=graphene.String())

    def resolve_page(self, info, **kwargs):
        path = kwargs.get('path')
        path = path[1:] if path.startswith('/') else path
        path = path[:-1] if path.endswith('/') else path

        root_page = info.context.site.root_page

        try:
            result = root_page.route(info.context, path.split('/'))
            return result.page
        except Http404:
            raise GraphQLError(f'404: Page not found for {path}')

    def resolve_all_pages(self, info):
        return Page.objects.specific().live()

import graphene
from django.http import Http404
from graphql import GraphQLError
from wagtail.core.models import Page

from . import types


class Query(graphene.ObjectType):
    page = graphene.Field(types.Page, path=graphene.String())
    all_pages = graphene.List(types.Page, path=graphene.String())

    def resolve_page(self, info, **kwargs):
        path = kwargs.get('path')
        path = path[1:] if path.startswith('/') else path
        path = path[:-1] if path.endswith('/') else path

        root_page = info.context.site.root_page

        try:
            result = root_page.route(info.context, path.split('/'))
            return result.page
        except Http404:
            return None

    def resolve_all_pages(self, info):
        return Page.objects.specific().live()

Return empty result rather than graphql error
Return empty result rather than graphql error

  import graphene
  from django.http import Http404
  from graphql import GraphQLError
  from wagtail.core.models import Page
  
  from . import types
  
  
  class Query(graphene.ObjectType):
      page = graphene.Field(types.Page, path=graphene.String())
      all_pages = graphene.List(types.Page, path=graphene.String())
  
      def resolve_page(self, info, **kwargs):
          path = kwargs.get('path')
          path = path[1:] if path.startswith('/') else path
          path = path[:-1] if path.endswith('/') else path
  
          root_page = info.context.site.root_page
  
          try:
              result = root_page.route(info.context, path.split('/'))
              return result.page
          except Http404:
-             raise GraphQLError(f'404: Page not found for {path}')
+             return None
  
      def resolve_all_pages(self, info):
          return Page.objects.specific().live()
  
Return empty result rather than graphql error
## Code Before:
import graphene
from django.http import Http404
from graphql import GraphQLError
from wagtail.core.models import Page

from . import types


class Query(graphene.ObjectType):
    page = graphene.Field(types.Page, path=graphene.String())
    all_pages = graphene.List(types.Page, path=graphene.String())

    def resolve_page(self, info, **kwargs):
        path = kwargs.get('path')
        path = path[1:] if path.startswith('/') else path
        path = path[:-1] if path.endswith('/') else path

        root_page = info.context.site.root_page

        try:
            result = root_page.route(info.context, path.split('/'))
            return result.page
        except Http404:
            raise GraphQLError(f'404: Page not found for {path}')

    def resolve_all_pages(self, info):
        return Page.objects.specific().live()

## Instruction:
Return empty result rather than graphql error
## Code After:
import graphene
from django.http import Http404
from graphql import GraphQLError
from wagtail.core.models import Page

from . import types


class Query(graphene.ObjectType):
    page = graphene.Field(types.Page, path=graphene.String())
    all_pages = graphene.List(types.Page, path=graphene.String())

    def resolve_page(self, info, **kwargs):
        path = kwargs.get('path')
        path = path[1:] if path.startswith('/') else path
        path = path[:-1] if path.endswith('/') else path

        root_page = info.context.site.root_page

        try:
            result = root_page.route(info.context, path.split('/'))
            return result.page
        except Http404:
            return None

    def resolve_all_pages(self, info):
        return Page.objects.specific().live()
