ohler55 / agoo

A High Performance HTTP Server for Ruby
MIT License
912 stars 39 forks source link

How to render a error message using GraphQL #94

Closed JosimarCamargo closed 4 years ago

JosimarCamargo commented 4 years ago

Hello, first of all, thanks for this cool blazing fast gem, I currently creating a microservice using the GraphQL, and didn't find how to fill the errors in the response, like: { "data": { ... }, "errors": [ ... ] } https://graphql.org/learn/serving-over-http/#response

there is any example that you may provide ?

ohler55 commented 4 years ago

Glad you like it. It's nice to get feedback everyone in a while, thanks.

I think you will laugh when I tell you... just raise an exception. 😄

JosimarCamargo commented 4 years ago

Hahaha, you are right, I laughed. Thanks for the quick response, it worked I'm curious, I'm getting something like this

{ "errors" => [{
              "message" => "Unauthorized: Unauthorized",
              "code" => "eval error",
              "timestamp"=>"2020-08-11T19:29:37.788091347Z"
          }]}

is this"code" => "eval error" expected?

ohler55 commented 4 years ago

I'll have to check on where the code is being set. I'll do that in an hour or two. Breaking for dinner.

ohler55 commented 4 years ago

Slight delay on dinner. When the evaluation of a query fails it causes a error in the GraphQL evaluator which becomes "eval error". That string is in err.c. The code is supposed to be a string hence the string version of the code.

JosimarCamargo commented 4 years ago

Thanks again, you are doing an awesome work here

viniciusvmda commented 2 years ago

@ohler55 or @JosimarCamargo ,

I have created an exception with code and message

class Unauthorized < StandardError
    attr_reader :code

    def initialize
      @code = 401
      super('Unauthorized')
    end
end

And raised the exception inside a query

raise Unauthorized.new

But it keeps returning 400 code on the response and the "eval error" on the code attribute besides the expected 401 for both cases.

< HTTP/1.1 400 Bad Request
{
    "errors": [
        {
            "message": "GraphQL::Queries::Unauthorized: Unauthorized",
            "code": "eval error",
            "timestamp": "2022-06-15T01:52:08.083186200Z"
        }
    ]
}

Could you please show me how to change the status code?