-1

I'm trying to make to query 1:1 relation in Prisma ORM But when querying it always returns null

Here is my datamodel:

enum Role {
  ADMIN
  MEMBER
  CONSTRIBUTOR
}

type User {
  id: ID! @id
  name: String! @unique
  email: String! @unique
  password: String!
  posts: [Post!]!
  role: Role @default(value: MEMBER)
}

type Post {
  id: ID! @id
  title: String
  excerpt: String
  content: Json
  author: User! @relation(link: INLINE)
}

Im trying to query a Post with Author that has a User in it:

but in my resolver when I do:

  getPost: async (parent, args, ctx, info) => {
            if (args.id) {
                console.log('GET POST by ID');
                const id = args.id;
                return await ctx.prisma.post({ id }).author();
            }
        },

It is always returning Null. Does someone know how can fix it ?

theduck
  • 2,589
  • 13
  • 17
  • 23
Loki
  • 1,064
  • 2
  • 26
  • 55
  • You're returning an author object, not a post object. – Daniel Rearden Dec 22 '19 at 20:39
  • @DanielRearden But I want Post together with Author that is attached to that post :) How could I do that – Loki Dec 22 '19 at 21:23
  • 1
    Have you read [the docs](https://www.prisma.io/docs/prisma-client/basic-data-access/reading-data-JAVASCRIPT-rsc2/#selecting-fields)? – Daniel Rearden Dec 22 '19 at 23:15
  • @DanielRearden thank you will check the fragment api :) – Loki Dec 23 '19 at 10:03
  • @DanielRearden I thought querying for relation would be like this: https://www.prisma.io/tutorials/a-guide-to-common-resolver-patterns-ct08/#scenario:-implementing-relations-with-prisma-client – Loki Dec 23 '19 at 10:05

1 Answers1

0

Fixed it by using sperate query for author like this:

   const author = await ctx.prisma.post({ id: id }).author();
   const post = await ctx.prisma.post({ id });

    return {
       ...post,
       author
    };
Loki
  • 1,064
  • 2
  • 26
  • 55