【问题标题】:Amplify with Graph QL how to query a related object用 Graphql 放大如何查询相关对象
【发布时间】:2019-12-13 08:40:13
【问题描述】:

我正在学习如何使用 Amplify 部署 AWS 应用程序。

我对使用 graphql 的关系有疑问。我只是按照文档的教程进行操作。我在这里生成了关于博客和帖子的示例模式:https://aws-amplify.github.io/docs/cli-toolchain/graphql?sdk=angular

我想知道如何进行比“查找全部”更复杂的查询。

具体来说:我可以像这样找到所有帖子:

import { APIService } from '../API.service';

export class BlogComponent implements OnInit {

constructor(private apiService: APIService) { }

@Input()
blog: any;

posts: any;

ngOnInit() {
    this.apiService.ListPosts().then((evt) => {
      this.posts = evt.items;
    });
}
}

ngOnInit() 函数中的代码可以很好地列出所有帖子。但是如何通过博客 ID 找到帖子?

graphhql 架构如下,这只是上面发布的链接中教程的一部分:

type Blog @model {
  id: ID!
  name: String!
  posts: [Post] @connection(name: "BlogPosts")
}
type Post @model {
  id: ID!
  title: String!
  blog: Blog @connection(name: "BlogPosts")
  comments: [Comment] @connection(name: "PostComments")
}

在生成的代码中,我看到了这些的 crud 操作,但没有看到“连接”。如何找到特定博客的所有帖子?

【问题讨论】:

    标签: angular amazon-web-services graphql


    【解决方案1】:

    我想通了。我有一个博客列表,但该博客列表没有每个博客项目中的帖子。

    所以在我有一个博客的“博客组件”上,我想列出帖子,我必须按 id 查询博客(在博客列表中提供,查询由放大)。查询的 blog-by-id 包含填充的帖子。

    @Component({
      selector: 'app-blog',
      templateUrl: './blog.component.html',
      styleUrls: ['./blog.component.css']
    })
    export class BlogComponent implements OnInit {
    
      @Input()
      blog: Blog;
    
      posts: Array<Post>;
    
      constructor(private apiService: APIService) {}
    
      ngOnInit() {
    
        this.apiService.GetBlog(this.blog.id).then((blog) => {
          this.posts = blog.posts.items;
        });
    
      }
    
    }
    

    【讨论】:

      猜你喜欢
      • 2021-01-13
      • 2019-10-05
      • 2020-07-16
      • 2019-08-27
      • 2021-04-17
      • 1970-01-01
      • 1970-01-01
      • 2020-11-11
      • 1970-01-01
      相关资源
      最近更新 更多