Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Help] How to enrich a response using GraphQL, Vapor and Graphiti #27

Closed
cameroncooke opened this issue Jan 14, 2024 · 0 comments
Closed

Comments

@cameroncooke
Copy link
Contributor

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
        }
}

When I run my server and perform a query:

query {
  users(id: $userId) {
    userId
    purchases {
        name
        price
        notes
    }
}

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.

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

@cameroncooke cameroncooke changed the title [Help} How to enrich a response using GraphQL, Vapor and Graphiti [Help] How to enrich a response using GraphQL, Vapor and Graphiti Jan 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant