Gatsby: How to get pageQuery variable from nested context property

Created on 24 Jul 2019  Â·  7Comments  Â·  Source: gatsbyjs/gatsby

It looks like there is no way to access nested context property values in page query variables. Is that correct?

What I'm doing

Right now I'm duplicating the page context variable intl.language as language so I can use it as pageQuery variable.

_gatsby-node.js_

exports.onCreatePage = ({ page, actions: { createPage, deletePage } }) => {
  if (!page.context.intl || page.context.language) {
    return;
  }
  deletePage(page);
  createPage({
    ...page,
    context: {
      ...page.context,
      language: page.context.intl.language },
  });
};

_src/pages/index.jsx_

export const query = graphql`
  query MyQuery($language: String!) {
    # …
  }
`

Ideal scenario

Ideally I could access nested context variables by default.

_gatsby-node.js_

-exports.onCreatePage = ({ page, actions: { createPage, deletePage } }) => {
-  if (!page.context.intl || page.context.language) {
-    return;
-  }
-  deletePage(page);
-  createPage({
-    ...page,
-    context: {
-      ...page.context,
-      language: page.context.intl.language },
-  });
-};

_src/pages/index.jsx_

export const query = graphql`
-  query MyQuery($language: String!) {
+  query MyQuery($intl__language: String!) {
    # …
  }
`

If this is an acceptable addition I'd love to PR.

stale? GraphQL question or discussion

Most helpful comment

Hmmm, shame this never got any attention. However, I would like to thank you for sharing your solution nevertheless! Helped me out today

All 7 comments

Hiya!

This issue has gone quiet. Spooky quiet. 👻

We get a lot of issues, so we currently close issues after 30 days of inactivity. It’s been at least 20 days since the last update here.

If we missed this issue or if you want to keep it open, please reply here. You can also add the label "not stale" to keep this issue open!

As a friendly reminder: the best way to see this issue, or any other, fixed is to open a Pull Request. Check out gatsby.dev/contributefor more information about opening PRs, triaging issues, and contributing!

Thanks for being a part of the Gatsby community! 💪💜

Hey again!

It’s been 30 days since anything happened on this issue, so our friendly neighborhood robot (that’s me!) is going to close it.

Please keep in mind that I’m only a robot, so if I’ve closed this issue in error, I’m HUMAN_EMOTION_SORRY. Please feel free to reopen this issue or create a new one if you need anything else.

As a friendly reminder: the best way to see this issue, or any other, fixed is to open a Pull Request. Check out gatsby.dev/contribute for more information about opening PRs, triaging issues, and contributing!

Thanks again for being part of the Gatsby community!

This is not a great experience. I would PR fixes and improvements if I got feedback.

But as it is now I'm not inclined to open another issue again. You're losing potential contributors this way.

Hmmm, shame this never got any attention. However, I would like to thank you for sharing your solution nevertheless! Helped me out today

Is it still the case that this isn't possible? Running into the same thing.

Having same issue

Same here!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

kalinchernev picture kalinchernev  Â·  3Comments

Oppenheimer1 picture Oppenheimer1  Â·  3Comments

ferMartz picture ferMartz  Â·  3Comments

andykais picture andykais  Â·  3Comments

brandonmp picture brandonmp  Â·  3Comments