私はこの素晴らしいapollo-universal-starter-kitを自分のプロジェクトに使用しています。私はthis pageにフィルタリングオプションを追加して、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
(完全なコードhere)
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 post_list
コンポーネント(コンポーネントの完全なコードはhereです)でフロントエンドで使用されるgraphqlクエリがあります
post_list
コンポーネント/ページへのオプションをフィルタリング追加することができますどのように
:
query getPosts($limit: Int!, $after: ID) {
postsQuery(limit: $limit, after: $after) {
totalCount
edges {
cursor
node {
... PostInfo
}
}
pageInfo {
endCursor
hasNextPage
}
}
}
は、これは長い導入:-)、申し訳ありません
質問でしたか?私は質問のリアクション側を理解していますが、私はgraphqlを理解していません。 postsQuery(limit: $limit, after: $after)
に新しい変数を追加して、postsQuery(limit: $limit, after: $after, numberOfComments: $numberOfComments)
のように表示する必要がありますか?そして、何とかバックエンドでそれを解決しますか?あるいは、私は間違った道を歩み、違った方向に考えるべきですか?もしそうなら、正しい方向に向けることができますか? :-)
ありがとうございます!