-
Notifications
You must be signed in to change notification settings - Fork 8
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
Hitting some issues with one to many relations and retreiving specific fields #132
Comments
@tsiege Sqlmancer was developed to avoid having to write resolvers for non-root fields, particularly relationship fields. There's a couple of problems with adding this sort of resolver: A) the root field will still try to fetch everything based on your request, B) you'll hit the n+1 problem without incorporating DataLoader or some other way to do batching. Since I didn't really consider this use case before, I don't have tests that cover it either :( I'd like to add a way to explicitly add additional where conditions like this when defining a relationship (#53). That would empower you to add extra conditions without having to write a custom resolver. The current workaround would be to use an actual view in your database or an "inline" one as shown here. To get your current approach working, there's a few things I would do. First, take the You should also fix the order the builder methods are called. Namely, make sure you call
Because you want only the posts associated with a particular franchise, I would assume your
To do that, you'll also need to specify You can give the above changes a shot and let me know if you're still seeing unexpected behavior. |
Hey @danielrearden this has gotten me unblocked! I figured I'd share my schema here for others so they can see what I did. It looks like I need to make sure the foreign key was cased exactly as it is in the db. So This schema and resolvers give me these following results type Post @model(table: "posts", pk: "id") {
id: ID!
mobiledoc: String!
slug: String!
title: String!
status: String!
createdAt: String!
publishedAt: String
franchise_id: String @private
franchise: Franchise @relate(on: { from: "franchise_id", to: "id" })
}
type Franchise @model(table: "franchises", pk: "id") {
id: ID!
name: String!
slug: String!
posts: [Post!]!
@depend(on: ["id"])
@many
@paginate
} Franchise: {
async posts(parent, args, ctx, info) {
const whereClause = {
status: { equal: 'published' },
franchise_id: { equal: parent.id },
}
return client.models.Post
.paginate()
.resolveInfo(info)
.where(whereClause)
.execute()
},
}, {
"data": {
"franchise": {
"name": "test franchise",
"posts": {
"results": [
{
"status": "published",
"title": "A Post With Every Card In It",
"franchise": {
"name": "test franchise"
}
}
]
}
}
} |
I have a model that has a one to many relationship. Franchises to Posts. Franchises can have many posts, but a post can only have on franchise. This works fine using just the schema directives, but I try to do anything to scope it down using a where clause a custom resolver I'm always returned one result and I can't retrieve any of it's fields beyond
id
even if I remove my custom where clause. I've tried removing pagination directives but that only works if I remove the custom resolver.Here's my schema
Here's my resolver:
The text was updated successfully, but these errors were encountered: