How to enrich a response using GraphQL, Vapor and Graphiti #133
-
| I'm building a GraphQL server application in Swift using GraphQL-Kit, Graphiti, Vapor, and Fluent with an SQL database. Below is a basic example to attempt to illustrate my problem. I have a  I have a resolver that exposes a function called  What I would like to do is enrich the response that  final class User: Model, Codable {
    static let schema = "users"
    @ID(key: .id)
    var id: UUID?
    @Children(for: \.$user)
    var purchases: [Purchase]
    init() {}
    init(id: UUID? = nil) {
        self.id = id
    }
}
final class Purchase: Model, Codable {
    static let schema = "purchases"
    @ID(key: .id)
    var id: UUID?
    @Field(key: "name")
    var name: String
    @Field(key: "price")
    var price: Double
    @Parent(key: "user_id")
    var user: User
    var notes: String?
    init() {}
    init(id: UUID? = nil, name: String, price: Double, userId: UUID, notes: String?) {
        self.id = id
        self.name = name
        self.price = price
        self.$user.id = userId
        self.notes = notes
    }
}
func getUser(
    request: Request,
    arguments: GetUserArguments
) throws -> EventLoopFuture<User> {
    User.find(arguments.id, on: request.db)
        .unwrap(or: Abort(.notFound))
        .map {
            $0.purchases.forEach {
                $0.notes = "Some value that comes from somewhere else that I can compute at resolution time"
            }
            return $0
        }
}When I run my server and perform a query: query {
  users(id: $userId) {
    userId
    purchases {
        name
        price
        notes
    }
}The  I'm pretty sure this is just a lack of understanding of how this all works but at the moment I can't for the life of me work out how to enrich the response of a query with data that isn't coming from the db. For context, I followed the setup and examples in this tutorial series: https://www.kodeco.com/21148796-graphql-tutorial-for-server-side-swift-with-vapor-getting-started#toc-anchor-001 Cross posted on StackOverflow: https://stackoverflow.com/questions/77812573/how-to-enrich-a-response-using-graphql-vapor-and-graphiti | 
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
| I've solved this I didn't realise that I could just point the schema field at a custom resolver function 🤦 , once I worked that out I could then write my own query code. I am having problems with accessing the identifier of an  | 
Beta Was this translation helpful? Give feedback.
I've solved this I didn't realise that I could just point the schema field at a custom resolver function 🤦 , once I worked that out I could then write my own query code. I am having problems with accessing the identifier of an
OptionalParenton the root object. For some reason, I can only see anilvalue, but if I change the field toParent(non-optional) the value isnon-nilleven though the data is the same in both scenarios.