Users, friendships and friend requests, problem with fetching relations


#1

Hi there,

I’m struggling with a proper modeling of schema for users which can send each other friend requests and become friends. Pretty much how it works on facebook. This is how I structured the schema so far: https://gist.github.com/bartosz-maciaszek/6e033ccfbaf36993682d98841fffb0ee.

This works great on the prisma level, which means all relations can be queried successfully like this:

query {
    user(where: {id: "xxx"}) {
        id
        displayName
        friendships {
            id
            users {
                displayName
            }
        }
        friendshipRequestsSent {
            id
            status
            to {
                displayName
            }
            from {
                displayName
            }
        }
        friendshipRequestsReceived {
            id
            status
            to {
                displayName
            }
            from {
                displayName
            }
        }
    }
}

The problem occurs on the generated client/resolvers level. This is how my schema and resolvers look like - https://gist.github.com/bartosz-maciaszek/ef24af28692ccb8ec07da5cd87849641

When I query the API with the following, I always get an errors saying friendshipRequestsReceived.from and friendshipRequestsSent.to cannot be populated.

query {
    me {
        id
        friendshipRequestsReceived {
            id
            from {
                id
                displayName
            }
            status
        }
        friendshipRequestsSent {
            id
            to {
                id
                displayName
            }
            status
        }
    }
}

This works perfectly for my other relations, however none of them require @relation directives to be added. I had to specify them here though since they are ambiguous. It is not clear to me why all of that works fine when querying prisma directly while it does not work through resolvers?

Am I missing something obvious or there’s a bug in prisma? Would you structure the datamodel differently to avoid it? Not sure it’s relevant, but I upgraded to prismagraphql/prisma:1.32 recently. Any feedback will be greatly appreciated.


#2

@bartosz

Since from and to are relations you will need to define a type resolver for them like you have done in the User type.

If you want to avoid this boilerplate consider using nexus-prisma: https://github.com/prisma/nexus-prisma


#3

Yeah, of course it’s missing resolver. Thank you so much for pointing that out.
nexus-prisma looks very interesting, I’ll definitely give it a go.


#4

This topic was automatically closed 45 days after the last reply. New replies are no longer allowed.