-
-
Notifications
You must be signed in to change notification settings - Fork 0
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
Filter results by existence of relation #4
Comments
First of all, this library in its simpler use is a thin wrapper around objection.js and its Second, with your GraphQL query, that is also not possible. Consider the two queries: query Assets1($limit: Int, $offset: Int, $eventsFilter: Filter) {
assets(limit: $limit, offset: $offset, filter: $filter) {
name
events(filter: $eventsFilter) {
id
event_type
}
}
}
query Assets2($limit: Int, $offset: Int, $eventsFilter: Filter) {
assets(limit: $limit, offset: $offset, filter: $filter) {
name
}
} They obviously are supposed to return the same set of assets. It would be very counter intuitive if the first query returned less assets than the second. So my recommendation would be adding a new parameter to the assets query: query Assets1($limit: Int, $offset: Int, $events_filter: Filter) {
assets(limit: $limit, offset: $offset, filter: $filter, eventsFilter: $eventsFilter) {
name
# events could be included or could be omitted - that doesn't affect the assets query!
events(filter: $eventsFilter) {
id
event_type
}
}
} and then creating a custom query: Assets: async (parent, args, context, info) => {
const assets = await resolveGraph(
context,
info,
Asset.query().whereExists(
// See https://vincit.github.io/objection.js/guide/query-examples.html
Assets.relatedQuery('events').where(
/* construct custom condition from args.eventsFilter */
)
)
)
return assets
} Honestly I don't remember if there is an exported helper for constructing a condition from a Do you think this will work for you? I'll keep this issue opened because having a helper for that would be a good addition. |
Thanks for getting back so quickly :) This is brilliant! I was able to achieve what I need by adding an additional argument to the root resolver, and then using Perhaps you are right about my intention not making sense -- maybe I explained it poorly, or maybe I could simply achieve what I wanted in a better manner. It's useful, though, because now I can query the Assets resolver for Assets that have a specific type of Event.
This now does exactly what I wanted :) That being said, I'd be happy to contribute to this library any time. It's a great plugin for Objection, and I'd love to see this continue being maintained. Thanks again! |
Is it possible to access the arguments passed into a child relation, and expose a query builder to handle them? I see that relations can handle the built-in filters, paginators, and modifiers, but having access to a query builder for additional arguments would be super helpful — I’m just not sure if this is already doable, and I’m missing something in the docs that explains how. Otherwise, I seem to only be able to access the root arguments within the corresponding resolver, which also limits me to tailoring the root query builder. I also realize this is a lot more complicated, but either a little bit of guidance I’d gladly submit a pull request to make it possible if it isn’t already doable out of the box. |
@mrodriguez-bc I think that's what Edit: ah, I didn't real well, you want to access the arguments. Apparently that's not possible: I don't see |
🎉 This issue has been resolved in version 4.2.0 🎉 The release is available on: Your semantic-release bot 📦🚀 |
@mrodriguez-bc modifiers now accept second argument which is a GraphQL resolve tree object. You can access GraphQL args for the field being resolved with export const Book = ModelResolver(BookModel, {
fields: {
id: true,
title: true,
authors: RelationResolver({
modifier(authors, { args }) {
authors.orderBy("name")
if (typeof args.country === "string" || args.country === null) {
authors.where("country", args.country)
}
},
}),
},
}) |
How would I go about filtering results where the condition of a nested relationship exists? I've looked through the docs and tried various approaches to no avail.
Example:
Let's say we have Authors and Books, and we want to filter Authors by existence of a Book with a genre.
Authors
{ id: 1, name: "John" },
{ id: 2, name: "Bob" },
{ id: 3, name: "Mary" }
Books
{ id: 1, author_id: 1, title: "Foo", genre: "Fiction" },
{ id: 2, author_id: 1, title: "Bar", genre: "SciFi" },
{ id: 3, author_id: 2, title: "Some Book", genre: "Non-Fiction" },
{ id: 4, author_id: 3, title: "Other Book", genre: "Fiction" },
{ id: 5, author_id: 3, title: "Another Book", genre: "Non-Fiction" },
{ id: 6, author_id: 3, title: "Yet Another Book", genre: "SciFi" },
Suppose we query Authors where genre = Fiction, only Author ID 1 and 3 are returned.
In my real use case, it's not Authors and Books, but Assets and Events.
Query:
Variables:
returns:
But I want these assets to be excluded, because they don't meet the Events criteria.
The text was updated successfully, but these errors were encountered: