Prisma ORM查询关系用户

问题描述 投票:-1回答:1

我正在尝试在Prisma ORM中查询1:1关系但是查询时总是返回null

这是我的数据模型:

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)
}

我正在尝试查询其中包含用户的作者帖子:

但是在我的解析器中:

  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();
            }
        },

它总是返回Null。有人知道如何解决吗?

graphql prisma prisma-graphql
1个回答
0
投票

通过像这样对作者使用单独查询来解决:

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

    return {
       ...post,
       author
    };
© www.soinside.com 2019 - 2024. All rights reserved.