gatsby-typescript
gatsby-typescript copied to clipboard
Indeterministic output causes unnecessary diffs in SCM
The order of some properties does seem indeterministic. I decided to track the file in Git, as I have a Git Hook in place to build all TypeScript which relies on the generated types and I want to be sure it works without having to run gatsby develop
beforehand. Unfortunately the output doesn't seem deterministic, so the file has changes because the order of properties in the types changes.
Example of such a diff:
@@ -2260,15 +2260,15 @@ export type SitePage = Node & {
internalComponentName: Scalars['String'];
componentChunkName: Scalars['String'];
matchPath?: Maybe<Scalars['String']>;
+ id: Scalars['ID'];
+ parent?: Maybe<Node>;
+ children: Array<Node>;
+ internal: Internal;
isCreatedByStatefulCreatePages?: Maybe<Scalars['Boolean']>;
context?: Maybe<SitePageContext>;
pluginCreator?: Maybe<SitePlugin>;
pluginCreatorId?: Maybe<Scalars['String']>;
componentPath?: Maybe<Scalars['String']>;
- id: Scalars['ID'];
- parent?: Maybe<Node>;
- children: Array<Node>;
- internal: Internal;
};
I might be wrong about this but I'm not sure how much control we have over the order of the properties because I believe this is handled upstream in graphql-code-gen
.
Instead, I'd like to see if we can figure out a way to generate the types without having to run the whole build.
At my work, I think we faced a similar problem that you're facing. We use and deploy storybook which relies on the generated gatsby types but we don't want to run the whole gatsby build in order to get those types. Instead of checking in the generated types, we copied the basic logic of this codegen plugin in gatsby-node.js
and added this check after the codegen ran:
const cliArgs = process.argv.slice(2);
exports.createPages = ({ store, reporter }) {
const { schema } = store.getState();
// do codegen…
// 👇👇👇
if (cliArgs.includes('--die-on-codegen')) {
process.exit(0);
}
// rest of create pages…
}
I'm not super happy with this solution but afaik, there is no way to get the schema from gatsby without running the build so it's the best I've got.
So TL;DR, I would prefer figure out some way to generate the types without running the build but I'm not too sure if there's an elegant way to do so.
So some questions for you:
- Would figuring out a way to generate types without running the build solve your problem?
- Do you really need consistent build output? (then might be an issue for upstream them, I might be mistaken though)
If generating the types without running the build will solve your problem then I think adding a special flag like --die-on-codegen
will work if we're okay with it's weirdness. This issue would be related to #29.
Looks like they have fixed the bug in 1.8.0
version.
Can you update the dependency of @graphql-codegen/core
?
Would you accept PR ?
@frosato-dev thanks for that notice! Yes, we would love a PR!