如何在ejs和graphql API中实现分页

时间:2018-12-01 11:04:05

标签: node.js pagination graphql ejs

我写了一个有很多页面的网站。其中一个我想显示所有帖子。我写了这部分,但现在我想在页面上有一个显示以前帖子的按钮。我在nodejs中编写此代码,并使用graphql API。我应该如何修改此代码以向用户显示以前的帖子?

const express = require("express");
const router = express.Router();
const { fetchQuery } = require("./../utils");

const query = `query getChannelPosts($id: ID!){
    channel : node( id : $id){
      __typename
      ... on Channel {
        posts(last:10 before: "NWMwMWZmMjRlZTE4NjM0ZDgwODE1MWE3"){
          edges{
            node{
              media{
                __typename
                ... on TextMedium {
                  text
                }
                ... on MusicMedium {
                  url
                  artworkUrl
                  duration
                }
                ... on GifMedium {
                  thumbnail
                  url
                        width
                  height
                  posterUrl
                }
                ... on LinkMedium {
                  title
                  url
                  description
                  poster {
                    thumbnail
                    width
                    height
                  }

                }
                }
              }
            }
          }
        }
      }
    }`;

router.get("/channel/:id", async (req, res) => {
  const data = await fetchQuery(query, { id: req.params.id });
  console.log(data);
  await res.render("channel", {
    ...data,
    title: "Posts of each Channel"
  });
});

module.exports = router;

这是我的ejs模板:

<html>
  <head>
    <title><%= title %></title>
  </head>
  <body>
    <h1><%= title %></h1>
    <div>
      <% channel.posts.edges.forEach(function({node:post}){ %>
      <div>
        <a href="category/<%= post.id %>">
          نام: <% post.media.forEach(function(medium){ if(medium.__typename ==
          "TextMedium" ){%>
          <p><%= medium.text %></p>

          <% if (medium.__typename == "MusicMedium") %>
          <p><%= medium.artworkUrl %></p>

          <% if (medium.__typename == "GifMedium") %>
          <p><%= medium.posterUrl %></p>

          <% if (medium.__typename == "LinkMedium") %>
          <p><%= medium.description %></p>

          <%} }); %>
        </a>
      </div>
      <% }); %>
    </div>
  </body>
</html>

0 个答案:

没有答案