首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >来自gatsby-node.js上下文的GatsbyJS中的GraphQL参数

来自gatsby-node.js上下文的GatsbyJS中的GraphQL参数
EN

Stack Overflow用户
提问于 2019-06-05 01:20:11
回答 2查看 713关注 0票数 1

在GatsbyJS中筛选GraphQL查询中的项目时遇到问题。我本以为可以在createPagecontext部分(如下面的currentDateminusFiveDays )创建键-值对,然后将它们用作页面组件中的参数,但似乎行不通。

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的新手,并且没有错误消息。EDIT:当我将Date更改为Date!Variable "$minusFiveDays" of required type "Date!" was not provided.时出现错误消息

EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56448610

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档