Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Catching GraphQLLocatedError for tests #1072

Closed
laurent-brisbois opened this issue Dec 8, 2020 · 7 comments · Fixed by #1109
Closed

Catching GraphQLLocatedError for tests #1072

laurent-brisbois opened this issue Dec 8, 2020 · 7 comments · Fixed by #1109

Comments

@laurent-brisbois
Copy link

laurent-brisbois commented Dec 8, 2020

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 ?

@ulgens
Copy link
Collaborator

ulgens commented Dec 8, 2020

I think what does do_smth do here is important. Graphql executor doesn't just throw that error and break the operation. It tries to return the error in response body. You may want to look at graphene_django.utils.testing.GraphQLTestCase

@laurent-brisbois
Copy link
Author

laurent-brisbois commented Dec 8, 2020

@ulgens Thanks for your answer.

I think what does do_smth do here is important. Graphql executor doesn't just throw that error and break the operation. It tries to return the error in response body. You may want to look at graphene_django.utils.testing.GraphQLTestCase

I edited my post. the do_smth is in my case the execution of the query. In the test I'm trying to write, I execute the test once while being logged, which should be okay (I use graphql-jwt for authentication and I've set the @login_required decorator on my query).

And I also execute the query whereas I'm not logged in in a second test. The goal is to check that an exception is thrown If I perform a query while not logged in. If, for instance, an assertRaises fails, that would mean for me that someone has removed the @login_required decorator.

I hope I'm clear :D

The only way that I've found up to now is to do :

self.assertIsNotNone(
    response.errors, "blablabla"
)

or to check if there isn't any error :

self.assertIsNone(
    response.errors, "blablabla"
)

I guess that does the trick too. But how can I be sure that the error is caused by the fact that I'm not logged in ?
Should I parse the error message ?

Or should I be satisfied by the fact that there is an error and..that's it ?

@zbyte64
Copy link
Collaborator

zbyte64 commented Jan 14, 2021

Could provide a custom ExecutionContext class with it's own handle_field_error that could stash the original errors for you and then assert they are proper in the test. We need more documentation covering this pattern.

@laurent-brisbois
Copy link
Author

Could provide a custom ExecutionContext class with it's own handle_field_error that could stash the original errors for you and then assert they are proper in the test. We need more documentation covering this pattern.

@zbyte64
Could your provide a basic structure for that ? I'm not sure to understand what you mean.

@zbyte64
Copy link
Collaborator

zbyte64 commented 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

@kubami
Copy link
Contributor

kubami commented Feb 3, 2021

There is a new UnforgivingExecutionContext in graphene's beta releases.

However passing the execution_context_class to GraphQLView.as_view yields this error:

TypeError: GraphQLView() received an invalid keyword 'execution_context_class'. as_view only accepts arguments that are already attributes of the class.

Looking at the code of the GraphQLView and .as_view it seems it should work.
@zbyte64 have you had this problem?

kubami added a commit to kubami/graphene-django that referenced this issue Feb 3, 2021
Currently when passing `execution_context_class` like this:

```
GraphQLView.as_view(execution_context_class=CustomContext)
```

you get the following error from `View.as_view()`
```
TypeError: GraphQLView() received an invalid keyword 'execution_context_class'. as_view only accepts arguments that are already attributes of the class.
```

this PR fixes the `hasattr` check in `.as_view`.

Fixes: graphql-python#1072
@kubami
Copy link
Contributor

kubami commented 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.

zbyte64 pushed a commit that referenced this issue Mar 31, 2021
…()` (#1109)

* Add ability to pass `execution_context_class` to `GraphQLView.as_view()`

Currently when passing `execution_context_class` like this:

```
GraphQLView.as_view(execution_context_class=CustomContext)
```

you get the following error from `View.as_view()`
```
TypeError: GraphQLView() received an invalid keyword 'execution_context_class'. as_view only accepts arguments that are already attributes of the class.
```

this PR fixes the `hasattr` check in `.as_view`.

Fixes: #1072

* make black happy

removed whitespace
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants