rmosolgo / graphiql-rails

Mount the GraphiQL query editor in a Rails app
MIT License
447 stars 135 forks source link

Exposing server error in response pane if invalid JSON returned #38

Closed AndrewRayCode closed 6 years ago

AndrewRayCode commented 6 years ago

If a server 500s or otherwise fails to return valid JSON, the GraphiQL interface will simply show the error of trying to parse the response as JSON. This means that for any meaningful GraphQL development, you have to keep the network tab open at all times to read the actual server response.

This replaces .json() with .text() and JSON.parse(). This is a quick fix without much thought put into it, but I couldn't find a more obvious way to preserve the JSON parsing error along with the actual server error. The Response() API is...interesting.

rmosolgo commented 6 years ago

Wow, this is great! For years, I have just kept the network tab open.

image

rmosolgo commented 6 years ago

I just tweaked this a tiny bit and released it as 1.4.8!

I separated our message from the server response like so:

image

Thanks again for this great improvement :)

AndrewRayCode commented 6 years ago

:O woohoo!

For a future improvement I think it would be great to preserve line breaks / not treat it as json. Rails (sometimes) provides a comprehensive stack trace with line breaks, and it's much harder to parse as one line.

My original code, even when not returning JSON, did not seem to make the line breaks work and instead showed \n but I did not investigate why