【问题标题】:How to do order by `createdAt` in my GraphQl query?如何在我的 GraphQl 查询中按“createdAt”进行排序?
【发布时间】:2022-02-01 03:00:09
【问题描述】:

我想将 oNewest 帖子返回到最旧的帖子。

这是我的查询:

export const getPosts = async () => {
  const query = gql `
    query MyQuery {
      postsConnection {
        edges {
          cursor
          node {
            author {
              bio
              name
              id
              photo {
                url
              }
            }
            createdAt
            slug
            title
            excerpt
            featuredImage {
              url
            }
            categories {
              name
              slug
            }
          }
        }
      }
    }
  `;
  const result = await request(graphqlAPI, query);

  return result.postsConnection.edges;
};

【问题讨论】:

    标签: javascript sorting graphql sql-order-by


    【解决方案1】:

    您可以使用以下语法:

    postsConnection (sort: "createdAt:desc") {
    

    完整示例:

    export const getPosts = async () => {
      const query = gql `
        query MyQuery {
          postsConnection (sort: "createdAt:desc") {
            edges {
              cursor
              node {
                author {
                  bio
                  name
                  id
                  photo {
                    url
                  }
                }
                createdAt
                slug
                title
                excerpt
                featuredImage {
                  url
                }
                categories {
                  name
                  slug
                }
              }
            }
          }
        }
      `;
      const result = await request(graphqlAPI, query);
    
      return result.postsConnection.edges;
    };
    

    【讨论】:

      猜你喜欢
      • 2020-01-31
      • 2020-02-19
      • 2020-09-24
      • 2020-01-09
      • 2019-07-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-23
      相关资源
      最近更新 更多