Beispiel #1
0
    def graphql(self,
                request_string: Optional[str] = None,
                variables: Optional[Dict[str, Any]] = None,
                meta: Optional[Dict[str, Any]] = None):
        """Return the GraphQL schema execution result.

        Args:
            request_string: GraphQL request passed to Graphene.
            variables: Dict of variables passed to Graphene.
            meta: Dict containing auth user etc.

        Returns:
            object: Execution result, or a list with errors.
        """
        try:
            executed: ExecutionResult = schema.execute(
                request_string,
                variable_values=variables,
                context_value={
                    'resolvers': self.resolvers,
                    'meta': meta or {},
                },
                backend=CylcGraphQLBackend(),
                middleware=list(instantiate_middleware(self.middleware)),
                executor=AsyncioExecutor(),
                validate=True,  # validate schema (dev only? default is True)
                return_promise=False,
            )
        except Exception as exc:
            return 'ERROR: GraphQL execution error \n%s' % exc
        if executed.errors:
            errors: List[Any] = []
            for error in executed.errors:
                if hasattr(error, '__traceback__'):
                    import traceback
                    errors.append({
                        'error': {
                            'message':
                            str(error),
                            'traceback':
                            traceback.format_exception(error.__class__, error,
                                                       error.__traceback__)
                        }
                    })
                    continue
                errors.append(getattr(error, 'message', None))
            return errors
        return executed.data
Beispiel #2
0
    def graphql(self, request_string=None, variables=None):
        """Return the GraphQL scheme execution result.

        Args:
            request_string (str, optional):
                GraphQL request passed to Graphene
            variables (dict, optional):
                Dict of variables passed to Graphene

        Returns:
            object: Execution result, or a list with errors.
        """
        try:
            asyncio.get_running_loop()
        except RuntimeError:
            loop = asyncio.new_event_loop()
            asyncio.set_event_loop(loop)
        try:
            executed = schema.execute(
                request_string,
                variables=variables,
                context={
                    'resolvers': self.resolvers,
                },
                executor=AsyncioExecutor(),
                return_promise=False,
            )
        except Exception as exc:
            return 'ERROR: GraphQL execution error \n%s' % exc
        if executed.errors:
            errors = []
            for error in executed.errors:
                if hasattr(error, '__traceback__'):
                    import traceback
                    errors.append({
                        'error': {
                            'message':
                            str(error),
                            'traceback':
                            traceback.format_exception(error.__class__, error,
                                                       error.__traceback__)
                        }
                    })
                    continue
                errors.append(getattr(error, 'message', None))
            return errors
        return executed.data