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

support for @Sibling many-to-many relationships? #6

Closed
jaredh159 opened this issue Jul 16, 2021 · 4 comments
Closed

support for @Sibling many-to-many relationships? #6

jaredh159 opened this issue Jul 16, 2021 · 4 comments

Comments

@jaredh159
Copy link
Contributor

Hi there. Thanks for this amazing library and all the work you put into it! It's been fantastic to use so far! 🙏

I'm having trouble modeling a many-to-many pivot-table based sibling relationship with vapor+graphql-kit. I'm a bit at a loss of even how to approach the problem. I'm wondering if it's something that is not supported yet, or if possibly I just don't know how to set up the schema and resolvers to make it work.

I see that you extended Graphiti's Field class here to add keypath-based resolution specifically for children and parent relationships. I'm wondering if something similar would need to be done for sibling relationships. Or is it not possible without some deeper work in the library? Or is it possible by dropping down and working at the Graphiti layer itself somehow?

Have you written an example with some code somewhere of how to do this? (I ask because I relied heavily on your excellent blog posts with the Todo examples when getting up and running).

Any info or pointers would be greatly appreciated. Thanks again!

@alexsteinerde
Copy link
Owner

Hi, awesome that you like the package!

The current version doesn't support Siblings relationships yet. But as you already pointed out this can be achieved by adding another extension to Graphiti's Field class.
I quickly prototyped a solution that works with the same syntax as Child and Parent relationships do (including a new example in the ToDo demo application).

// Siblings Relationship
extension Graphiti.Field where Arguments == NoArguments, Context == Request, ObjectType: Model {
    public convenience init<ToType: Model, ThroughType: Model>(
        _ name: FieldKey,
        with keyPath: KeyPath<ObjectType, SiblingsProperty<ObjectType, ToType, ThroughType>>
    ) where FieldType == [TypeReference<ToType>] {
        self.init(name.description, at: { (type) -> (Request, NoArguments, EventLoopGroup) throws -> EventLoopFuture<[ToType]> in
            return { (context: Request, arguments: NoArguments, eventLoop: EventLoopGroup) in
                return type[keyPath: keyPath].query(on: context.db).all()
            }
        }, as: [TypeReference<ToType>].self)
    }
}

I'll clean up my code, add some documentation and ship a new version in the next day or two. When I have the time, I'll also publish a blog post explaining its usage. But it should be straightforward to use.

I hope this will help you solve your use case.
I really appreciate that you use GraphQL+Server Side Swift. I think it's a great combination.

@jaredh159
Copy link
Contributor Author

@alexsteinerde thanks for getting back so quickly. I copied your sample code into my project, and it seems to work perfectly, at least at first glance. I'll just use that until you're able to get a new release out, no hurry. Really appreciate it!

@alexsteinerde
Copy link
Owner

I published a new release 2.2.0 with the mentioned extension included.
If you encounter any bugs, feel free to reopen the issue.

@jaredh159
Copy link
Contributor Author

New version works great! Thanks again!

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

2 participants