如何查询与TypeORM的关系?

时间:2019-10-19 15:47:06

标签: javascript node.js typescript graphql typeorm

如何查询关系?

我想查询帖子的作者。

这是我尝试的方式:

  async findAuthor({root: rootArg}: {root: Post}): Promise<User[]> {

    // Match post's author id with ids from user table
    const result = await User.find({where: {id: rootArg.author}})

    // Returns empty array
    console.log('@ RESULT HERE > ', result)

    // Return author
    return result;
  }

以前带有Knex.js

    const result: User[] = await knex('user')
      .where('id', rootArg.author)
      .returning('*');

还查找用户的帖子:

  // Find posts related to user
  async findPosts({root: rootArg}: {root: User}): Promise<Post[]> {

    // Match user's id with author id from post database
    const result = await Post.find({where: {author_id: rootArg.id}})

    // This returns all posts from db
   console.log('@ RESULT HERE > ', result)

    // Return posts
    return result;
  }

以前带有Knex.js

    const result: Post[] = await knex('post')
      .where('author', rootArg.id)
      .returning('*');

请帮助我。

堆栈:TypeScript,Apollo-server-express,GraphQL

0 个答案:

没有答案