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 User model that maps to a users table, and a Purchase model that maps to a purchases table.
I have a resolver that exposes a function called getUsers. For brevity I've not included the schema or setup code, for this example assume that the schema replicates roughly the same structure and links to the resolver.
What I would like to do is enrich the response that getUsers function returns. I have data that comes from another source that I would like to add to the Purchase response, my idea was to just create a normal property on the Purchase model called notes and then in the resolver just use map to access the instance of each purchase model and assign the values into the notes field, I assumed this would work as the models are classes.
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
}
}
The notes field in the graph comes back with a null.
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.
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
User
model that maps to ausers
table, and aPurchase
model that maps to apurchases
table.I have a resolver that exposes a function called
getUsers
. For brevity I've not included the schema or setup code, for this example assume that the schema replicates roughly the same structure and links to the resolver.What I would like to do is enrich the response that
getUsers
function returns. I have data that comes from another source that I would like to add to thePurchase
response, my idea was to just create a normal property on thePurchase
model callednotes
and then in the resolver just usemap
to access the instance of each purchase model and assign the values into the notes field, I assumed this would work as the models are classes.When I run my server and perform a query:
The
notes
field in the graph comes back with anull
.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