Relay Modern nested pagination

后端 未结 1 468
轮回少年
轮回少年 2021-02-11 03:28

I have a root query of songs, this is in a pagination container. I then have a nested property on songs called comments that I also want to be paginate

1条回答
  •  深忆病人
    2021-02-11 03:39

    SongsContainer.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)
          }
        }
      `
    );
    

    CommentsContainer.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
            }
          }
        `
      }
    );
    

    0 讨论(0)
提交回复
热议问题