Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

graphql - 如何在基于非模板的页面(src / pages的一部分)中将变量传递给GraphQL(How to pass variable to GraphQL in a non-template based page (part of src/pages))

While I'm able to pass variable via pageContext to GraphQL on all template based pages ( which use createPage function), I'm not able to pass/modify the context for pages which are part of src/page.

(虽然我能够通过pageContext将变量传递给所有基于模板的页面(使用createPage函数)上的GraphQL,但是我无法传递/修改属于src / page的页面的上下文。)

Consider this home page : src/pages/index.js

(考虑此主页:src / pages / index.js)

//want to pass dynamic value to $today
const IndexPage = ( {data, pageContext} ) => {
    console.log(pageContext); // prints "isCreatedByStatefulCreatePages: true"

    return (
    <Layout />
)};

export default IndexPage;


export const Query = graphql`
  offers( where : { day: $today})
  {
    ..fields
  }
`;
  ask by Shatiz translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

In order to modify automatically created pages you will need to use the onCreatePage node API.

(为了修改自动创建的页面,您将需要使用onCreatePage节点API。)

You basically recreate (delete/create) pages while modifying the page object.

(您基本上可以在修改页面对象的同时重新创建(删除/创建)页面。)

Just follow instructions in the docs Creating and Modifying Pages

(只需按照docs 创建和修改页面中的说明进行操作)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...