Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(gatsby): Stabilize types output of GraphQL Typegen #35925

Merged
merged 2 commits into from
Jun 15, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 17 additions & 10 deletions packages/gatsby/src/utils/graphql-typegen/ts-codegen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ import type { TypeScriptDocumentsPluginConfig } from "@graphql-codegen/typescrip
import { CodeFileLoader } from "@graphql-tools/code-file-loader"
import { loadDocuments } from "@graphql-tools/load"
import { IDefinitionMeta, IStateProgram } from "../../redux/types"
import { filterTargetDefinitions, stabilizeSchema } from "./utils"
import {
filterTargetDefinitions,
sortDefinitions,
stabilizeSchema,
} from "./utils"

const OUTPUT_PATH = `src/gatsby-types.d.ts`
const NAMESPACE = `Queries`
Expand Down Expand Up @@ -104,6 +108,7 @@ export async function writeTypeScriptTypes(
},
}),
],
sort: true,
}
)
} catch (e) {
Expand All @@ -112,15 +117,17 @@ export async function writeTypeScriptTypes(

const documents: Array<Types.DocumentFile> = [
...filterTargetDefinitions(definitions).values(),
].map(definitionMeta => {
return {
document: {
kind: Kind.DOCUMENT,
definitions: [definitionMeta.def],
},
hash: definitionMeta.hash.toString(),
}
})
]
.sort(sortDefinitions)
.map(definitionMeta => {
return {
document: {
kind: Kind.DOCUMENT,
definitions: [definitionMeta.def],
},
hash: definitionMeta.hash.toString(),
}
})

const codegenOptions: Omit<Types.GenerateOptions, "plugins" | "pluginMap"> = {
// @ts-ignore - Incorrect types
Expand Down
15 changes: 15 additions & 0 deletions packages/gatsby/src/utils/graphql-typegen/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,21 @@ export function stabilizeSchema(schema: GraphQLSchema): GraphQLSchema {
return lexicographicSortSchema(schema)
}

export function sortDefinitions(
a: IDefinitionMeta,
b: IDefinitionMeta
): number {
const aKey = a.name
const bKey = b.name
if (aKey < bKey) {
return -1
}
if (aKey > bKey) {
return 1
}
return 0
LekoArts marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* Internally in Gatsby we use the function generateQueryName:
* packages/gatsby/src/query/file-parser.js
Expand Down