gpt4 book ai didi

javascript - 中继现代嵌套分页

转载 作者:行者123 更新时间:2023-11-30 20:55:38 24 4
gpt4 key购买 nike

我有一个 songs 的根查询,它在分页容器中。然后,我在名为 comments 的歌曲上有一个嵌套属性,我也想对其进行分页,因为我不想一次为每首歌曲加载 10k 条评论。

歌曲容器.js:

fragment songsContainer on Query {
songs(
first: $count
after: $cursor
genre: $genre
filter: $filter
) @connection(key: "songsContainer_songs") {
edges {
node {
audioId
name
coverImageUrl
artist
likes
dislikes
...commentsContainer
}
}
}
}

const connectionConfig = {
direction: 'forward',
query: graphql`
query songsContainerForwardQuery(
$count: Int!
$cursor: String
$genre: String
$filter: FilterInput
) {
...songsContainer
}
`,
getVariables: (_, { count, cursor }) => ({
count,
cursor,
}),
};

paginationContainer(fragments, connectionConfig);

评论容器.js

  fragment commentsContainer on Audio {
comments(
first: $count
after: $cursor
getReplies: $getReplies
) @connection(key: "commentsContainer_comments") {
edges {
node {
commentId
body
date
likes
dislikes
repliesCount
originalComment {
id
}
user {
userName
}
}
}
}
}

评论的connectionConfig怎么写?我试过这个:

const connectionConfig = {
direction: 'forward',
query: graphql`
query commentsContainerForwardQuery(
$count: Int!
$cursor: String
) {
...commentsContainer
}
`,
getVariables: (_, { count, cursor }) => ({
count,
cursor,
}),
};

但是因为评论是嵌套在歌曲上的,所以它会抛出一个错误,指出查询不存在于根上。

最佳答案

歌曲容器.js

createPaginationContainer(
SongsContainer,
{
viewer: graphql`
fragment SongsContainer_viewer on Query {
id
songs(first: $count, after: $cursor)
@connection(key: "SongsContainer_songs", filters: []) {
edges {
cursor
node {
id
...SongItem_song
}
}
}
}
`
},
{
direction: 'forward',
getConnectionFromProps(props) {
return props.viewer && props.viewer.songs;
},
getFragmentVariables(prevVars, totalCount) {
return {
...prevVars,
count: totalCount
};
},
getVariables(props, { count, cursor }, fragmentVariables) {
return {
count,
cursor
};
},
query: graphql`
query SongsContainerQuery($count: Int!, $cursor: String) {
viewer {
...SongsContainer_viewer
}
}
`
}
);

SongItem.js

createRefetchContainer(
SongItem,
{
song: graphql`
fragment SongItem_song on Audio
@argumentDefinitions(
count: { type: "Int", defaultValue: 20 }
cursor: { type: "String", defaultValue: null }
) {
id
...CommentsContainer_song
# ...more pagination container other_song
}
`
},
graphql`
query SongItemQuery($id: ID!, $count: Int!, $cursor: String) {
song(id: $id) {
...SongItem_song @arguments(count: $count, cursor: $cursor)
}
}
`
);

评论容器.js

createPaginationContainer(
CommentsContainer,
{
song: graphql`
fragment CommentsContainer_song on Audio {
id
comments(first: $count, after: $cursor)
@connection(key: "CommentsContainer_comments", filters: []) {
edges {
id
}
}
}
`
},
{
direction: 'forward',
getConnectionFromProps(props) {
return props.song && props.song.comments;
},
getFragmentVariables(prevVars, totalCount) {
return {
...prevVars,
count: totalCount
};
},
getVariables(props, { count, cursor }, fragmentVariables) {
return {
count,
cursor,
id: props.song.id
};
},
query: graphql`
query CommentsContainerQuery($id: ID!, $count: Int!, $cursor: String) {
song(id: $id) {
...CommentsContainer_song
}
}
`
}
);

关于javascript - 中继现代嵌套分页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47699873/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com