發(fā)布于:2021-02-04 09:50:20
0
665
0
假設(shè)您要基于一個參數(shù)或條件在一個頁面中獲取特定數(shù)據(jù),而該參數(shù)或條件不能使用一個查詢運(yùn)行,因?yàn)槟荒苁褂貌煌臈l件或參數(shù)查詢同一字段。一種方法是使用GraphQL別名,您可以使用它將返回的數(shù)據(jù)集重命名為任何您想要的名稱。
示例
export const query = graphql`
query {
post: allMarkdownRemark(
limit: 3
sort: { order: DESC, fields: [frontmatter___date] }
filter: { frontmatter: { type: { ne: "portfolio" } } }
) {
edges {
node {
timeToRead
frontmatter {
title
path
date(formatString: "DD MMMM YYYY")
summary
images
tags
type
}
}
}
}
portfolio: allMarkdownRemark(
sort: { order: DESC, fields: [frontmatter___date] }
filter: { frontmatter: { type: { eq: "portfolio" } } }
) {
edges {
node {
frontmatter {
title
path
images
tags
type
}
}
}
}
siteMetaData: site {
siteMetadata {
title
}
}
}
`;
查看上面的示例,我們可以看到我所做的查詢將返回多個數(shù)據(jù)集,方法是為其提供一個別名,該別名允許我使用不同的參數(shù)和條件運(yùn)行多個查詢,以獲得您在屏幕截圖中看到的所需的特定數(shù)據(jù)對象。
作者介紹