From ec218b05dc981c8a6149b50925349df6d459bc49 Mon Sep 17 00:00:00 2001 From: LekoArts Date: Mon, 13 Jun 2022 15:46:33 +0200 Subject: [PATCH 1/2] initial --- .../src/utils/graphql-typegen/ts-codegen.ts | 27 ++++++++++++------- .../gatsby/src/utils/graphql-typegen/utils.ts | 15 +++++++++++ 2 files changed, 32 insertions(+), 10 deletions(-) diff --git a/packages/gatsby/src/utils/graphql-typegen/ts-codegen.ts b/packages/gatsby/src/utils/graphql-typegen/ts-codegen.ts index 4e36bba7487cd..35b20194c6e2f 100644 --- a/packages/gatsby/src/utils/graphql-typegen/ts-codegen.ts +++ b/packages/gatsby/src/utils/graphql-typegen/ts-codegen.ts @@ -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` @@ -104,6 +108,7 @@ export async function writeTypeScriptTypes( }, }), ], + sort: true, } ) } catch (e) { @@ -112,15 +117,17 @@ export async function writeTypeScriptTypes( const documents: Array = [ ...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 = { // @ts-ignore - Incorrect types diff --git a/packages/gatsby/src/utils/graphql-typegen/utils.ts b/packages/gatsby/src/utils/graphql-typegen/utils.ts index c9e8f5be32a6c..75eff2ad7af20 100644 --- a/packages/gatsby/src/utils/graphql-typegen/utils.ts +++ b/packages/gatsby/src/utils/graphql-typegen/utils.ts @@ -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 +} + /** * Internally in Gatsby we use the function generateQueryName: * packages/gatsby/src/query/file-parser.js From 40bd718c7aa07ba79d80d63b56c5b20b6a3728af Mon Sep 17 00:00:00 2001 From: Lennart Date: Mon, 13 Jun 2022 17:01:16 +0200 Subject: [PATCH 2/2] Update packages/gatsby/src/utils/graphql-typegen/utils.ts Co-authored-by: Michal Piechowiak --- packages/gatsby/src/utils/graphql-typegen/utils.ts | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/packages/gatsby/src/utils/graphql-typegen/utils.ts b/packages/gatsby/src/utils/graphql-typegen/utils.ts index 75eff2ad7af20..517af218c6a96 100644 --- a/packages/gatsby/src/utils/graphql-typegen/utils.ts +++ b/packages/gatsby/src/utils/graphql-typegen/utils.ts @@ -18,15 +18,7 @@ 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 + return a.name.localeCompare(b.name) } /**