ashkan18 / graphlient

Ruby GraphQL Client
MIT License
251 stars 44 forks source link

adding extensions to ExecutionError #53

Open RealSilo opened 6 years ago

RealSilo commented 6 years ago

Hey,

I think it would be great to have extensions with code (see here: http://graphql-ruby.org/errors/execution_errors.html) to the Graphlient::Errors::ExecutionError class.

So it could be used in tests as:

rescue Graphlient::Errors::ExecutionError => e
  expect(e.message).to eq('My error message')
  expect(e.extensions.code).to eq('UNAUTHORIZED')
end
dblock commented 6 years ago

Sounds like a good idea! PRs welcome.

yuki24 commented 6 years ago

The code used in the GraphQL Ruby documentation is just an example of what the server could return to you. Also, the ExecutionError holds a reference to the response object so you could actually get the extensions out of it:

JSON.parse(execution_error.response.body, symbolize_names: true)[:extensions]

If you are requesting to add an alias, then yes, that seems like a useful helper method. However, if you meant to mutate the extensions, it could be surprising as it could override what the server really wants to tell you.

RealSilo commented 6 years ago

@yuki24 I know it's only recommended [https://facebook.github.io/graphql/draft/#example-fce18]() at the moment but as I see everybody is using it (from startups to big companies).

Thanks for the example on how to parse the keys. I think it's a bit awkward though. It seems to be way easier to use e.extensions.code.

@dblock Let me know if you still want me to open a PR based on @yuki24 's comments.

dblock commented 6 years ago

@RealSilo PRs always welcome!

RealSilo commented 6 years ago

Sounds good. I will try to open a PR this weekend.