Hi guys I'm using Prisma1 and i'm tring to get the...
# orm-help
n
Hi guys I'm using Prisma1 and i'm tring to get the foreign key of a relation. Take the basic example of Post and User.
type User {
id: ID! @id
name: String!
posts: [Post!]!
}
type Post {
id: ID! @id
title: String!
published: Boolean! @default(value: false)
author: User @relation(link: INLINE, "PostAuthor")
}
If I query the post I would like to have the id of user so I can then use it directly from a second level resolver.
const posts = await db.posts()
Its a 1:n relation so the key is stored on the Post entity. With SQL it would be a simple
select *
And yes we will migrate away from prisma1 but for now we have to fix current not working stuff.
d
is it not available on
posts[0].author.id
?
If it isn't being returned, you can define what is returned by passing gql into the query like so:
Copy code
const posts = await db.posts(
    {
      where: { ... },
    },
    `{
      author {
        id
      }`
  )
n
Yea I've tried that. I've also tried with a fragment on the second resolver:
Copy code
Post: {
  author: {
    fragment: `fragment Author on Post { author {id} }`,
    resolve: async ({id, author}, args, {redis, userLoader}) => {
...
}
I'm starting to think that I'm doing something really wrong.
d
are you able to retrieve the author at all? It might just be that there is no author connected?
n
No the other fields are there, only the external keys are missing
I confirm that the other fields are presents. only the relations are missing. Only using a graphql query I can get this to work.
d
Hmmm, that's pretty strange
n
// This works
`const posts = await db.$graphql(
query ($where: PostWhereInput!){
posts(where: $where) {
id
name
author {
id
}
headingImage {
id
}
}
}
, {where: where});`
//This does not work
`const posts = await db.posts({where: where},
{author { id } headingImage { id }
});`