プロジェクトの 1 つで、この素敵なapollo-universal-starter-kitを使用しています。このページにフィルタリング オプションを追加して、2 つ以上のコメントを持つ投稿をフィルタリングするタスクがあります。
スターター キットは、バックエンドとしてApollo graphql-serverを使用します。投稿のスキーマの説明は次のようになります。
# Post
type Post {
id: Int!
title: String!
content: String!
comments: [Comment]
}
# Comment
type Comment {
id: Int!
content: String!
}
# Edges for PostsQuery
type PostEdges {
node: Post
cursor: Int
}
# PageInfo for PostsQuery
type PostPageInfo {
endCursor: Int
hasNextPage: Boolean
}
# Posts relay-style pagination query
type PostsQuery {
totalCount: Int
edges: [PostEdges]
pageInfo: PostPageInfo
}
extend type Query {
# Posts pagination query
postsQuery(limit: Int, after: Int): PostsQuery
# Post
post(id: Int!): Post
}
postsQuery
投稿のページ分割された結果を生成するために使用されます
解決方法は次のとおりですpostsQuery
(完全なコードはこちら)
async postsQuery(obj, { limit, after }, context) {
let edgesArray = [];
let posts = await context.Post.getPostsPagination(limit, after);
posts.map(post => {
edgesArray.push({
cursor: post.id,
node: {
id: post.id,
title: post.title,
content: post.content,
}
});
});
let endCursor = edgesArray.length > 0 ? edgesArray[edgesArray.length - 1].cursor : 0;
let values = await Promise.all([context.Post.getTotal(), context.Post.getNextPageFlag(endCursor)]);
return {
totalCount: values[0].count,
edges: edgesArray,
pageInfo: {
endCursor: endCursor,
hasNextPage: values[1].count > 0
}
};
}
そして、React コンポーネントのフロントエンドで使用される graphql クエリを次に示しますpost_list
(コンポーネントの完全なコードはこちら) 。
query getPosts($limit: Int!, $after: ID) {
postsQuery(limit: $limit, after: $after) {
totalCount
edges {
cursor
node {
... PostInfo
}
}
pageInfo {
endCursor
hasNextPage
}
}
}
これは長い紹介でした:-)、申し訳ありません
質問:
post_list
コンポーネント/ページにフィルタリング オプションを追加するにはどうすればよいですか? 質問の React 側はある程度理解できますが、graphql 側は理解できません。postsQuery(limit: $limit, after: $after)
のように新しい変数を追加する必要がありpostsQuery(limit: $limit, after: $after, numberOfComments: $numberOfComments)
ますか? そして、どういうわけかバックエンドでそれを解決しますか? それとも、私は間違った方向に進んでいるので、別の方向に考えるべきですか? もしそうなら、あなたは私を正しい方向に向けることができますか?:-)
前もって感謝します!