Support custom GraphQL fragments inside gatsby-node.js #12155
Replies: 27 comments 2 replies
-
You can use normal string interpolation: |
Beta Was this translation helpful? Give feedback.
-
The problem with string interpolation is that it can't be used inside page queries or static queries. I thought fragments were supposed to solve the issue of repetition (and dependencies) inside GraphQL queries? |
Beta Was this translation helpful? Give feedback.
-
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! Thanks for being a part of the Gatsby community! 💪💜 |
Beta Was this translation helpful? Give feedback.
-
Is it possible to give us some more information what you tried and create a reproduction so we can easily test this out. |
Beta Was this translation helpful? Give feedback.
-
@wardpeet Here is more information about various things I tried: #12092 |
Beta Was this translation helpful? Give feedback.
-
i just ran into the same issue trying to refactor my growing gatsby-node query into fragments stored elsewhere. hope theres a fix for this |
Beta Was this translation helpful? Give feedback.
-
@realgt If you only need those fragments inside a gatsby-node query, then string interpolation is not a bad solution. I actually ran into this issue again yesterday, but I need to use the fragments in static queries as well, which means I can't use string interpolation and now I have duplicated code :( |
Beta Was this translation helpful? Give feedback.
-
@baobabKoodaa string interpolation just seems like a dirty solution, the graphql equivalent of sql injection |
Beta Was this translation helpful? Give feedback.
-
Chiming in here to report that I expected my GraphQL fragments to be available in |
Beta Was this translation helpful? Give feedback.
-
It would make sense to support your fragments inside gatsby-node.js as well. This would require some refactoring as we don't collect all the fragments until we run page/static queries later in bootstrap. But we should be able to move that earlier and use the Relay Compiler to "expand" queries with fragments into the full query when they're passed into the graphql function. /cc @freiksenet |
Beta Was this translation helpful? Give feedback.
-
check this https://github.com/n1ru4l/relay-compiler-repl to understand how relay compiler transforms graphql queries and fragments |
Beta Was this translation helpful? Give feedback.
-
Any news on this? |
Beta Was this translation helpful? Give feedback.
-
I would appreciate more info as well. Thanks! :) |
Beta Was this translation helpful? Give feedback.
-
Here to also say this would be awesome! I use the same query in |
Beta Was this translation helpful? Give feedback.
-
I would love this functionality as well! |
Beta Was this translation helpful? Give feedback.
-
Here you go ;) import { promisify } from "util";
import GatsbyParser from "gatsby/dist/query/file-parser";
import fs from "fs";
import path from "path";
/**
* Collect all graphql queries from a directory
* @param dirname
* @returns {Promise<*>}
*/
export const collectGQLQueries = async dirname => {
const parser = new GatsbyParser();
const files = await promisify(fs.readdir)(dirname);
const result = await parser.parseFiles(
files.map(file => path.join(dirname, file))
);
return result
.filter(item => item.doc && item.doc.kind === "Document")
.map(doc => doc.text)
.join("\n");
}; How to use: 2- make sure you use graphql to query them 3- Use the // fragments
const fragments = await collectGQLQueries(
path.resolve(__dirname, "fragments")
);
// data
const { data, errors } = await graphql(`
${fragments}
{
allContentfulPage {
nodes {
id
contentful_id
// ...
}
}
}`); |
Beta Was this translation helpful? Give feedback.
-
Any news on this? |
Beta Was this translation helpful? Give feedback.
-
I had to change @digital-flowers' function a bit to pick fragments by name, since an unused fragment fails a query. const fragments = await collectGraphQLFragments(
resolve(__dirname, "src/features"),
["TweetDiscussEditLinksDataOnMdx"]
); |
Beta Was this translation helpful? Give feedback.
-
How do you access fragments from |
Beta Was this translation helpful? Give feedback.
-
Is there an api that allows injection of fragments into gatsby-node ? |
Beta Was this translation helpful? Give feedback.
-
Any update? |
Beta Was this translation helpful? Give feedback.
-
Any update on this? Really want to get this solved :) |
Beta Was this translation helpful? Give feedback.
-
Throwing my hat in for this request. I'm using the same fragment across 4 different files (one of which is gatsby-node) |
Beta Was this translation helpful? Give feedback.
-
Came here for this exact request, I want to generate "dynamic"-ish fragments on gatsby-node for localization purposes. The idea for my case is that the user has defined a list of languages that the app will use, and the fragment would query each one. The contentQuery would give me back an array of languages, string formatted to graphql
ultimately I can then use this in static queries, as the user may decide to swap between languages mid-application. My localization hook on the frontend will already have the data ready to go.
|
Beta Was this translation helpful? Give feedback.
-
Commenting to get an update on this request, facing this issue in my project aswell and had to duplicate fragment twice. I am now using @digital-flowers solution and that works for now. |
Beta Was this translation helpful? Give feedback.
-
As another data point: We're also using a solution like described by @digital-flowers, but we'd much prefer not to inherit that technical debt in our theme! We also had to filter for actually used fragments in the page, which is pretty involved as we're supporting page-builder type data sources where you can't say what components will be used on any given page / layout ahead of time. |
Beta Was this translation helpful? Give feedback.
-
The @digital-flowers solution seems outdated, and I couldn't replicate it with the current FileParser. Moving your framents into a large text chunk and interpolating doesn't work unless you use every fragment, and even then, the frags will have to be duplicated in code for your page queries. |
Beta Was this translation helpful? Give feedback.
-
How can I use custom GraphQL fragments inside gatsby-node? Here is more information on what I've tried so far, but it would perhaps be better if someone just provided a working example. The example in the docs doesn't work when applied to gatsby-node (or I'm doing something wrong).
Beta Was this translation helpful? Give feedback.
All reactions