question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Catching GraphQLLocatedError for tests

See original GitHub issue

Is your feature request related to a problem? Please describe. No, question about doing a particular thing

Describe the solution you’d like I’d like to know the way to catch GraphQLLocatedError, or a way to assertRaises them.

I’ve tried :

  • try…except way :
try:
    response = self.client.execute(query, variables)
except GraphQLLocatedError:
    self.fail(...)
  • with self.assertRaises(…) way :
with self.assertRaises(GraphQLLocatedError):
    response = self.client.execute(query, variables)

But none of these work. How can I do this ?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
zbyte64commented, Jan 19, 2021

In the past I have used the following class to get stack traces of exceptions:

from graphql.execution import ExecutionContext

class DebugExecutionContext(ExecutionContext):
        def handle_field_error(
            self, raw_error: Exception, field_nodes, path, return_type,
        ) -> None:
            import traceback            
            traceback.print_exception(
                type(raw_error), raw_error, raw_error.__traceback__
            )
            return super().handle_field_error(raw_error, field_nodes, path, return_type)

Then when I setup my view in the urls.py I pass it as an option:

GraphQLView.as_view(execution_context_class=DebugExectionContext)

Kind of lame that this method still requires patching the urlconf for use in tests: https://pytest-django.readthedocs.io/en/latest/helpers.html#pytest-mark-urls-override-the-urlconf

0reactions
kubamicommented, Feb 3, 2021

The above error is caused by hasattr check in .as_view, as it is not aware that the GraphQLView has that attribute. The below PR fixes that.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to test GraphQLError exception in graphene_django or ...
Here are my two files: the code and the corresponding tests located in ... from graphql.error.located_error import GraphQLLocatedError from ...
Read more >
Full Stack Error Handling with GraphQL and Apollo
Different types of GraphQL errors · Best practices to deal with those errors · Practical use-cases and examples using Apollo Server 2.0 ...
Read more >
Error Handling - graphql-python
All applications fail, and GraphQL is no different. Some clients may ask for information that's not available or execute a forbidden action.
Read more >
Testing GraphQL API in python - DataDrivenInvestor
A complete guide to test graphql api in python. ... this library helps us in capturing endpoints response and check it again when...
Read more >
Release 0.6.1 - Read the Docs
GraphQLLocatedError (message, nodes=None, path=None) ... This is mostly useful for testing and when you need to convert nodes to JSON such as ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found