← Back
graphql

GraphQL query aliases

This is a standard query for this blog:

query {
allPost(
sort: { fields: date, order: DESC }
filter: { tags: { elemMatch: { permalink: { eq: "blog" } } } }/
limit: 3
) {
nodes {
slug
title
date(formatString: "DD.MM.YYYY")
tags {
name
slug
}
timeToRead
}
}
}

That produces a result like this:

{
data: [
allPost: {
nodes: [{ title: '...' }, ....]
}
]
}

But sometimes we want to get not only "blog" posts but also "gists" posts. Enter graphql query aliases:

  query {
posts: allPost(
sort: { fields: date, order: DESC }
filter: { tags: { elemMatch: { permalink: { eq: "blog" } } } }/
limit: 3
) {
nodes {
...
}
}
gists: allPost(
sort: { fields: date, order: DESC }
filter: { tags: { elemMatch: { permalink: { eq: "gist" } } } }/
limit: 5
) {
nodes {
...
}
}
}

That produces a result with this shape:

{
data: [
posts: {
nodes: [{ title: '...' }, ....]
},
gists: {
nodes: [{ title: '...' }, ....]
}
]
}

More information:

🖖