d-exclaimation / pioneer

GraphQL for Swift.
https://pioneer.dexclaimation.com
Apache License 2.0
39 stars 8 forks source link

Use operation specific Response to return an GraphQL Result when formatting thrown error by resolver #66

Closed d-exclaimation closed 2 years ago

d-exclaimation commented 2 years ago

Describe the bug Currently when a resolver thrown an error, it will use that operation's Response status code to send back a GraphQLResult with the thrown error, but leave out all other things set on it (i.e. headers, cookies, etc.).

It should not make the decision that an error meant the headers, cookies should not be set, and instead use the Response and all its values to encode the GraphQLResult with the error message.

Steps to reproduce

Have a resolver that throw an error after settings values in the response header.

extension Resolver {
    func doSomething(ctx: Context, _: NoArguments) throws -> String {
        ctx.response.headers.add(name: .init("X-Some-Header"), value: "something")
        throw Abort(.notImplemented, reason: "Expected error")
    }
}

Have the schema with this as mutation or query, so it can run on HTTP

Schema<Resolver, Context> {
    Query { ... }

    Mutation { 
        Field("doSomething", at: Resolver.doSomething)
    }
}

Run the server and make a request with the GraphQL query of:

mutation {
  doSomething
}

It should return a JSON of GraphQLResult with an empty data field and errors field with the thrown error (formatted as GraphQLError), but it will not send back any headers.