У меня проблемы с фильтрацией элементов в запросе GraphQL в GatsbyJS. Я подумал, что смогу создать пары ключ-значение в context
разделе createPage
(как currentDate
и minusFiveDays
ниже), а затем использовать их в качестве аргументов в компонентах страницы, но, похоже, это не работает.
В gatsby-node.js
:
const currentDate = moment().format('YYYY-MM-DD');
const minusFiveDays = moment()
.subtract(5, 'days')
.format('YYYY-MM-DD');
console.log('DATE', minusFiveDays); // this logs correctly
result.data.allMarkdownRemark.edges.forEach(({ node }) => {
createPage({
path: node.frontmatter.slug,
component: someContentTypeTemplate,
context: {
// Can the names passed in here be accessed in
// graphql queries, prefixed with a dollar sign?
currentDate: currentDate,
minusFiveDays: minusFiveDays,
},
});
});
В моем src/pages/someContentType.js
файле:
// TODO: this query doesn't work. No matter what condition I try to use
// for $minusFiveDays, it doesn't affect the output. The query does work in
// graphiql when I hard-code a string there like "2018-11-01".
export const pageQuery = graphql`
query($minusFiveDays: Date) { // this is the key from the file above
allMarkdownRemark(
filter: {
frontmatter: {
content_type: { eq: "some_content_type" }
start_date: { ne: null, gte: $minusFiveDays }
}
}
sort: { order: ASC, fields: [frontmatter___start_date] }
) {
edges {
node {
id
frontmatter {
created_at
slug
title
start_date(formatString: "DD MMMM YYYY")
end_date
}
}
}
}
}
`;
Даже если я жестко закодирую это так, это не сработает, поэтому, кажется, что контекст не передается в запрос GraphQL компонента:
context: {
currentDate: currentDate,
minusFiveDays: "2018-11-01",
},
Это работает в GraphiQL:
{
allMarkdownRemark(filter: {frontmatter: {content_type: {eq: "some_content_type"}, start_date: {ne: null, gte: "2018-11-01"}}}, sort: {order: DESC, fields: [frontmatter___start_date]}) {
edges {
node {
id
frontmatter {
created_at
slug
title
start_date(formatString: "DD MMMM YYYY")
end_date
}
}
}
}
}
Я думаю, что мой синтаксис должен быть неправильным, но я новичок в GraphQL и Gatsby, и сообщений об ошибках нет. РЕДАКТИРОВАТЬ: появляется сообщение об ошибке при изменении Date
на Date!
: Variable "$minusFiveDays" of required type "Date!" was not provided.