Prisma ORM查询关系用户

时间:2019-12-22 19:24:21

标签: graphql prisma prisma-graphql

我试图在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。有人知道如何解决吗?

1 个答案:

答案 0 :(得分:0)

通过对作者使用单独查询来解决此问题:

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

    return {
       ...post,
       author
    };