python-graphql-client icon indicating copy to clipboard operation
python-graphql-client copied to clipboard

Allow status codes, other than 200

Open Blusten23 opened this issue 4 years ago • 1 comments

Is your feature request related to a problem? Please describe. Currently result.raise_for_status() raises the error but I would like to deal with status codes later on rather than to get an exception from the client. The issue is described here: https://github.com/graphql-python/gql/issues/40

Describe the solution you'd like Add a flag, raise_for_status=True/False in client.execute().

Describe alternatives you've considered Comment out the result.raise_for_status() line

Additional context

Blusten23 avatar Mar 08 '21 14:03 Blusten23

I also ran into this issue today as well; in that because of the use of 'raise_for_status', my underlying '400' error (and its message; that would allow me to debug the issue) was eaten/stripped away from me.

Suggested Code change to allow for this functionality to be disabled?

    def execute(
        self,
        query: str,
        variables: dict = None,
        operation_name: str = None,
        headers: dict = {},
        riase_for_status: bool = True,
        **kwargs: Any,
    ):
        """Make synchronous request to graphQL server."""
        request_body = self.__request_body(
            query=query, variables=variables, operation_name=operation_name
        )

        result = requests.post(
            self.endpoint,
            json=request_body,
            headers={**self.headers, **headers},
            **{**self.options, **kwargs},
        )

        if riase_for_status:
            result.raise_for_status()

        return result.json()

sferich888 avatar Mar 27 '23 13:03 sferich888