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

Change response status code without info.context.response #1801

Open
ZeroIntensity opened this issue Apr 8, 2022 · 2 comments
Open

Change response status code without info.context.response #1801

ZeroIntensity opened this issue Apr 8, 2022 · 2 comments

Comments

@ZeroIntensity
Copy link

ZeroIntensity commented Apr 8, 2022

i would like to change a response status code without having to manually use info.context.response

example:

@strawberry.field
def something(some_arg: Optional[str]) -> str:
    response: Response = info.context.response

    if cond:
        response.status_code = 400
        raise Exception("...")

    if cond:
        response.status_code = 401
        raise Exception("...")

    if cond:
        response.status_code = 402
        raise Exception("...")

    if cond:
        response.status_code = 403
        raise Exception("...")

    return "ok"

this gets very repetitive, so i tried doing something like this:

class Error400(Exception):
    pass

@strawberry.field
def something() -> str:
    if cond:
        raise Error400("...")
    return "ok"

class Router(GraphQLRouter):
    async def process_result(
        self, request: Request, result: ExecutionResult
    ) -> GraphQLHTTPResponse:
        ...
        if Error400 in errors:
            response.status_code = 400 # we cant access response from process_result

is it possible to do something like this, or do i have to continue to do it manually?

Upvote & Fund

  • We're using Polar.sh so you can upvote and help fund this issue.
  • We receive the funding once the issue is completed & confirmed by you.
  • Thank you in advance for helping prioritize & fund our backlog.
Fund with Polar
@patrick91
Copy link
Member

@ZeroIntensity not at the moment, you could implement a custom decorator for your use case, maybe.

It is definitely a good feature request, but not sure how to best implement it, maybe we could do this via extensions 🤔

@ZeroIntensity
Copy link
Author

alright, ill leave this open in case you figure out a way to do this

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

No branches or pull requests

2 participants