首页
学习
活动
专区
工具
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

代码语言:javascript
复制
        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文件中:

代码语言:javascript
复制
// 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查询中:

代码语言:javascript
复制
                context: {
                    currentDate: currentDate,
                    minusFiveDays: "2018-11-01",
                },

这在GraphiQL中是有效的:

代码语言:javascript
复制
{
    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

回答 2

Stack Overflow用户

发布于 2019-06-24 20:16:43

我认为问题在于您的模板在src/pages目录中。

src/pages中的*.js文件将自动构建到页面中,包括您的模板someContentType.js。这意味着Gatsby会在您在gatsby-node.js中进行的自定义createPage调用之上自动为您生成此页面。

但是,当Gatsby自动生成此页面时,它并不知道您通过createPage传入的上下文。这就是它不起作用的原因。

将您的模板移出src/pages可能会解决您的问题。

票数 1
EN

Stack Overflow用户

发布于 2019-06-06 03:18:48

我认为这可能是因为你从这一行得到的值:

代码语言:javascript
复制
const minusFiveDays = moment().subtract(5, 'days').format('YYYY-MM-DD');

不是Date类型。从控制台中的一个小试用来看,它似乎是一个字符串。

如果改为:

代码语言:javascript
复制
$minusFiveDays: String!

如果没有看到你的项目,我就不能确定它可能会起作用。

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

https://stackoverflow.com/questions/56448610

复制
相关文章

相似问题

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