Is it possible to prepare static queries, dynamically (at build time)? #29120
-
I'm new to Gatsby and I'm facing the same problem as you probably did at some time: I would like to use variables in a static query. I get it, I get it, of course this is not possible, because the static queries are executed at build time and Gatsby cannot know what the variables will be that I feed into my query when the page executes at the client. So far so good. But I wonder: Is it not possible to have dynamic logic (in node at build time) to prepare static queries at all? I mean, we are already able to create static websites based on dynamic sources within gatsby-node.js so I thought the same should be possible to prepare queries. Im my case, I want to get the 20 newest posts of each category from Wordpress (using WP GraphQL), the category part is dynamic, here. It seems logical to me to query the categories on build time with node (within gatsby-node.js or gatsby-ssr.js) and prepare a static query (or many static queries, one per category) that get(s) all my posts how I want them. I'm so sure this should be possible in theory, I'm trying to work that out right now. I still want to open this discussion, maybe I miss something or I can give - at least - inspiration to the devs to consider this to be baked into the framework in the future. Thanks. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Ok it seems gatsby-node.js is the right direction. I can use the sourceNodes function to create my own node type that will serve all my possible data, statically. In that I could query my GraphQL API like described in the createPages function. I also figured out that I could simply request categories and get their 20 newest posts in one step using one static query, I don't need to do this in node at all, but it gave me a good idea that this is possible. In my case I am a little bit screwed, because the category type is not from Wordpress, but from a plugin, so I have to expose this to the WP GraphQL API, but this is another story and has nothing to do with this discussion. |
Beta Was this translation helpful? Give feedback.
Ok it seems gatsby-node.js is the right direction. I can use the sourceNodes function to create my own node type that will serve all my possible data, statically. In that I could query my GraphQL API like described in the createPages function.
I also figured out that I could simply request categories and get their 20 newest posts in one step using one static query, I don't need to do this in node at all, but it gave me a good idea that this is possible. In my case I am a little bit screwed, because the category type is not from Wordpress, but from a plugin, so I have to expose this to the WP GraphQL API, but this is another story and has nothing to do with this discussion.