question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to get pageQuery variable from nested context property

See original GitHub issue

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.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:6
  • Comments:7

github_iconTop GitHub Comments

2reactions
Hlesscommented, Feb 27, 2020

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

1reaction
moerikicommented, Sep 24, 2019

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Spring REST - binding GET parameters to nested objects
Since at least Spring 4 you can pass in nested objects separated with "." in the url. In the OP case it would...
Read more >
Resolvers - Apollo GraphQL Docs
We want to define resolvers for the numberSix and numberSeven fields of the root Query type so that they always return 6 and...
Read more >
Eager Loading of Related Data - EF Core - Microsoft Learn
You can use the Include method to specify related data to be included in query results. In the following example, the blogs that...
Read more >
What to do when “this” loses context - freeCodeCamp
The doSomething() method has two nested functions: doAnotherThing() and log() . When service.doSomething() is called, this loses context in the ...
Read more >
Nested query | Elasticsearch Guide [8.5] | Elastic
Top-level parameters for nested edit ... (Required, query object) Query you wish to run on nested objects in the path . If an...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found