How to inject certain context into all pages in Gatsby?










1















Using gatsby-node.js and createPages I can query something using graphql and create pages using that something as page context. So those pages can use that something as parameter in their queries.



The problem I'm facing is that I don't want to use createPages. I'm completely fine with pages that are created by default (from gatsby-plugin-page-creator), I just want all of them to have something from graphql as a context.



Basically I want a global context (which I get from gatsby graphql) to be available for all pages.



There is onCreatePage hook but unfortunately graphql is not available there according to https://github.com/gatsbyjs/gatsby/issues/3121#issuecomment-348781341.










share|improve this question




























    1















    Using gatsby-node.js and createPages I can query something using graphql and create pages using that something as page context. So those pages can use that something as parameter in their queries.



    The problem I'm facing is that I don't want to use createPages. I'm completely fine with pages that are created by default (from gatsby-plugin-page-creator), I just want all of them to have something from graphql as a context.



    Basically I want a global context (which I get from gatsby graphql) to be available for all pages.



    There is onCreatePage hook but unfortunately graphql is not available there according to https://github.com/gatsbyjs/gatsby/issues/3121#issuecomment-348781341.










    share|improve this question


























      1












      1








      1








      Using gatsby-node.js and createPages I can query something using graphql and create pages using that something as page context. So those pages can use that something as parameter in their queries.



      The problem I'm facing is that I don't want to use createPages. I'm completely fine with pages that are created by default (from gatsby-plugin-page-creator), I just want all of them to have something from graphql as a context.



      Basically I want a global context (which I get from gatsby graphql) to be available for all pages.



      There is onCreatePage hook but unfortunately graphql is not available there according to https://github.com/gatsbyjs/gatsby/issues/3121#issuecomment-348781341.










      share|improve this question
















      Using gatsby-node.js and createPages I can query something using graphql and create pages using that something as page context. So those pages can use that something as parameter in their queries.



      The problem I'm facing is that I don't want to use createPages. I'm completely fine with pages that are created by default (from gatsby-plugin-page-creator), I just want all of them to have something from graphql as a context.



      Basically I want a global context (which I get from gatsby graphql) to be available for all pages.



      There is onCreatePage hook but unfortunately graphql is not available there according to https://github.com/gatsbyjs/gatsby/issues/3121#issuecomment-348781341.







      gatsby






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 16 '18 at 9:24







      MnZrK

















      asked Nov 16 '18 at 8:30









      MnZrKMnZrK

      586418




      586418






















          1 Answer
          1






          active

          oldest

          votes


















          1














          What about this... you can try using the onCreateNode() hook instead inside your gatsby-nodejs file. E.g.:



          const allMyPageNodes = ;

          exports.onCreateNode = ( node, actions, getNode ) =>
          const createNodeField = actions;
          if(...) //whatever filtering you need to select JUST pages in your site
          const mySpecialContext = "blah"; //whatever your global context settings are
          createNodeField( node, name: "myglobal", value: mySpecialContext );
          allMyPageNodes.push(node);




          ```



          Then in all graphql queries thereafter, for pages, the graphql payload should have edge.node.fields.myglobal populated and you should be able to do whatever you like with it. Also have a look at https://www.gatsbyjs.org/docs/static-query/ for querying directly from inside a component.



          HTH.






          share|improve this answer























          • Thank you for suggestion. Unfortunately it won't work for me because the context that I need also should come from graphql, and I can't use graphql inside onCreateNode. Moreover, I need to be able to use this context as parameter for page query, not as payload.

            – MnZrK
            Nov 17 '18 at 8:30











          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53334054%2fhow-to-inject-certain-context-into-all-pages-in-gatsby%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          What about this... you can try using the onCreateNode() hook instead inside your gatsby-nodejs file. E.g.:



          const allMyPageNodes = ;

          exports.onCreateNode = ( node, actions, getNode ) =>
          const createNodeField = actions;
          if(...) //whatever filtering you need to select JUST pages in your site
          const mySpecialContext = "blah"; //whatever your global context settings are
          createNodeField( node, name: "myglobal", value: mySpecialContext );
          allMyPageNodes.push(node);




          ```



          Then in all graphql queries thereafter, for pages, the graphql payload should have edge.node.fields.myglobal populated and you should be able to do whatever you like with it. Also have a look at https://www.gatsbyjs.org/docs/static-query/ for querying directly from inside a component.



          HTH.






          share|improve this answer























          • Thank you for suggestion. Unfortunately it won't work for me because the context that I need also should come from graphql, and I can't use graphql inside onCreateNode. Moreover, I need to be able to use this context as parameter for page query, not as payload.

            – MnZrK
            Nov 17 '18 at 8:30















          1














          What about this... you can try using the onCreateNode() hook instead inside your gatsby-nodejs file. E.g.:



          const allMyPageNodes = ;

          exports.onCreateNode = ( node, actions, getNode ) =>
          const createNodeField = actions;
          if(...) //whatever filtering you need to select JUST pages in your site
          const mySpecialContext = "blah"; //whatever your global context settings are
          createNodeField( node, name: "myglobal", value: mySpecialContext );
          allMyPageNodes.push(node);




          ```



          Then in all graphql queries thereafter, for pages, the graphql payload should have edge.node.fields.myglobal populated and you should be able to do whatever you like with it. Also have a look at https://www.gatsbyjs.org/docs/static-query/ for querying directly from inside a component.



          HTH.






          share|improve this answer























          • Thank you for suggestion. Unfortunately it won't work for me because the context that I need also should come from graphql, and I can't use graphql inside onCreateNode. Moreover, I need to be able to use this context as parameter for page query, not as payload.

            – MnZrK
            Nov 17 '18 at 8:30













          1












          1








          1







          What about this... you can try using the onCreateNode() hook instead inside your gatsby-nodejs file. E.g.:



          const allMyPageNodes = ;

          exports.onCreateNode = ( node, actions, getNode ) =>
          const createNodeField = actions;
          if(...) //whatever filtering you need to select JUST pages in your site
          const mySpecialContext = "blah"; //whatever your global context settings are
          createNodeField( node, name: "myglobal", value: mySpecialContext );
          allMyPageNodes.push(node);




          ```



          Then in all graphql queries thereafter, for pages, the graphql payload should have edge.node.fields.myglobal populated and you should be able to do whatever you like with it. Also have a look at https://www.gatsbyjs.org/docs/static-query/ for querying directly from inside a component.



          HTH.






          share|improve this answer













          What about this... you can try using the onCreateNode() hook instead inside your gatsby-nodejs file. E.g.:



          const allMyPageNodes = ;

          exports.onCreateNode = ( node, actions, getNode ) =>
          const createNodeField = actions;
          if(...) //whatever filtering you need to select JUST pages in your site
          const mySpecialContext = "blah"; //whatever your global context settings are
          createNodeField( node, name: "myglobal", value: mySpecialContext );
          allMyPageNodes.push(node);




          ```



          Then in all graphql queries thereafter, for pages, the graphql payload should have edge.node.fields.myglobal populated and you should be able to do whatever you like with it. Also have a look at https://www.gatsbyjs.org/docs/static-query/ for querying directly from inside a component.



          HTH.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 16 '18 at 21:06









          yenyen

          11110




          11110












          • Thank you for suggestion. Unfortunately it won't work for me because the context that I need also should come from graphql, and I can't use graphql inside onCreateNode. Moreover, I need to be able to use this context as parameter for page query, not as payload.

            – MnZrK
            Nov 17 '18 at 8:30

















          • Thank you for suggestion. Unfortunately it won't work for me because the context that I need also should come from graphql, and I can't use graphql inside onCreateNode. Moreover, I need to be able to use this context as parameter for page query, not as payload.

            – MnZrK
            Nov 17 '18 at 8:30
















          Thank you for suggestion. Unfortunately it won't work for me because the context that I need also should come from graphql, and I can't use graphql inside onCreateNode. Moreover, I need to be able to use this context as parameter for page query, not as payload.

          – MnZrK
          Nov 17 '18 at 8:30





          Thank you for suggestion. Unfortunately it won't work for me because the context that I need also should come from graphql, and I can't use graphql inside onCreateNode. Moreover, I need to be able to use this context as parameter for page query, not as payload.

          – MnZrK
          Nov 17 '18 at 8:30



















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53334054%2fhow-to-inject-certain-context-into-all-pages-in-gatsby%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          27

          Top Tejano songwriter Luis Silva dead of heart attack at 64

          Category:Rhetoric