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

Stack traces in graphql errors #80

Open
EmrysMyrddin opened this issue Feb 22, 2021 · 1 comment
Open

Stack traces in graphql errors #80

EmrysMyrddin opened this issue Feb 22, 2021 · 1 comment

Comments

@EmrysMyrddin
Copy link

Hi,

It would be great for development purpose to include exception stack traces to graphql errors returned by graphql-server.flask when it catches an exception during the execution of a request.

For security reasons, I think it shouldn't be enabled by default, but at least offer the option to do it, since for now, it's just not possible to do.

The only way I found to debut an error is to place a break point in graphql-server.flask.GraphQLView.dispatch_request method in the excpect handler... which is not very convenient.

If you are open to the idea, I can make a PR to poc around this feature.

@guillaumep
Copy link

guillaumep commented Jul 8, 2022

Found a way to obtain stack traces and to log them server-side.

Define a middleware function like this:

import logging

def exception_logging_middleware(next, root, info, **args):
    try:
        return_value = next(root, info, **args)
    except Exception as e:
        logging.exception(e)
        raise
    return return_value

And provide it to the middleware parameter when using GraphQLView.as_view:

from graphql_server.flask import GraphQLView

def publicapi_view():
    return GraphQLView.as_view(
        "publicapi",
        # public_schema is an instance of graphene.Schema
        schema=public_schema.graphql_schema,
        middleware=[exception_logging_middleware],
    )

# app here is the Flask app
app.add_url_rule("/publicapi", view_func=publicapi_view(), methods=["POST"])

Tested with the following versions:

flask==2.0.2
graphene==3.0
graphql-core==3.1.7
graphql-server==3.0.0b4

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

No branches or pull requests

2 participants