From 58d8c7b8263e8b9dddf7e01965ae02c101d8f555 Mon Sep 17 00:00:00 2001 From: LekoArts Date: Wed, 17 Aug 2022 14:47:33 +0200 Subject: [PATCH 1/3] initial --- packages/gatsby/src/services/initialize.ts | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/packages/gatsby/src/services/initialize.ts b/packages/gatsby/src/services/initialize.ts index a9b206940631d..8245537adaadd 100644 --- a/packages/gatsby/src/services/initialize.ts +++ b/packages/gatsby/src/services/initialize.ts @@ -659,10 +659,12 @@ export async function initialize({ const workerPool = WorkerPool.create() - // This is only run during `gatsby develop` if (state.config.graphqlTypegen) { telemetry.trackFeatureIsUsed(`GraphQLTypegen`) - writeGraphQLConfig(program) + // This is only run during `gatsby develop` + if (process.env.gatsby_executing_command === `develop`) { + writeGraphQLConfig(program) + } } return { From 8269c153c59e9dff2815bd7815de86a3b89ef153 Mon Sep 17 00:00:00 2001 From: LekoArts Date: Wed, 17 Aug 2022 16:30:06 +0200 Subject: [PATCH 2/3] initial --- .../local-development/graphql-typegen.md | 2 +- .../reference/config-files/gatsby-config.md | 4 ++ packages/gatsby/index.d.ts | 1 + packages/gatsby/src/commands/build.ts | 37 +++++++++++++++++++ .../gatsby/src/joi-schemas/__tests__/joi.ts | 22 ++++++++++- packages/gatsby/src/joi-schemas/joi.ts | 2 + packages/gatsby/src/redux/types.ts | 1 + 7 files changed, 67 insertions(+), 2 deletions(-) diff --git a/docs/docs/how-to/local-development/graphql-typegen.md b/docs/docs/how-to/local-development/graphql-typegen.md index 46172e1b69681..765db00efd8de 100644 --- a/docs/docs/how-to/local-development/graphql-typegen.md +++ b/docs/docs/how-to/local-development/graphql-typegen.md @@ -9,7 +9,7 @@ examples: If you're already using [Gatsby with TypeScript](/docs/how-to/custom-configuration/typescript) and manually typing the results of your queries, you'll learn in this guide how Gatsby's automatic GraphQL Typegen feature can make your life easier. By relying on the types that are generated by Gatsby itself and using autocompletion for GraphQL queries in your IDE you'll be able to write GraphQL queries quicker and safer. -This feature was added in `gatsby@4.15.0`. +This feature was added in `gatsby@4.15.0`. By default, this feature is only generating files during `gatsby develop`. ## Prerequisites diff --git a/docs/docs/reference/config-files/gatsby-config.md b/docs/docs/reference/config-files/gatsby-config.md index ee1f81c0d3eba..e46cbe3ade730 100644 --- a/docs/docs/reference/config-files/gatsby-config.md +++ b/docs/docs/reference/config-files/gatsby-config.md @@ -206,6 +206,10 @@ module.exports = { You can specifiy the path of the generated TypeScript types file relative to the site root. Default: `src/gatsby-types.d.ts`. +### gatsbyBuild + +By default, `graphqlTypegen` is only run during `gatsby develop`. Set this option to `true` to create the `src/gatsby-types.d.ts` file also during `gatsby build`. Default: `false`. + ## polyfill Gatsby uses the ES6 Promise API. Because some browsers don't support this, Gatsby includes a Promise polyfill by default. diff --git a/packages/gatsby/index.d.ts b/packages/gatsby/index.d.ts index d31bced4ffb26..9a3583bf10f1f 100644 --- a/packages/gatsby/index.d.ts +++ b/packages/gatsby/index.d.ts @@ -258,6 +258,7 @@ export const graphql: (query: TemplateStringsArray) => StaticQueryDocument export interface GraphQLTypegenOptions { typesOutputPath?: string + gatsbyBuild?: boolean } /** diff --git a/packages/gatsby/src/commands/build.ts b/packages/gatsby/src/commands/build.ts index 0bde01c8689c9..d5303027e9c46 100644 --- a/packages/gatsby/src/commands/build.ts +++ b/packages/gatsby/src/commands/build.ts @@ -63,6 +63,7 @@ import { import { validateEngines } from "../utils/validate-engines" import { constructConfigObject } from "../utils/gatsby-cloud-config" import { waitUntilWorkerJobsAreComplete } from "../utils/jobs/worker-messaging" +import { writeTypeScriptTypes } from "../utils/graphql-typegen/ts-codegen" module.exports = async function build( program: IBuildArgs, @@ -419,6 +420,42 @@ module.exports = async function build( await waitMaterializePageMode const waitWorkerPoolEnd = Promise.all(workerPool.end()) + // create scope so we don't leak state object + { + const { schema, definitions, config } = store.getState() + const directory = program.directory + const graphqlTypegenOptions = config.graphqlTypegen + + // Only generate types when the option is enabled + if (graphqlTypegenOptions && graphqlTypegenOptions.gatsbyBuild) { + const typegenActivity = reporter.activityTimer( + `Generating TypeScript types`, + { + parentSpan: buildSpan, + } + ) + typegenActivity.start() + + try { + await writeTypeScriptTypes( + directory, + schema, + definitions, + graphqlTypegenOptions + ) + } catch (err) { + typegenActivity.panicOnBuild({ + id: `12100`, + context: { + sourceMessage: err, + }, + }) + } + + typegenActivity.end() + } + } + { let SSGCount = 0 let DSGCount = 0 diff --git a/packages/gatsby/src/joi-schemas/__tests__/joi.ts b/packages/gatsby/src/joi-schemas/__tests__/joi.ts index 58bb6aefbcaa5..d0b2606504f51 100644 --- a/packages/gatsby/src/joi-schemas/__tests__/joi.ts +++ b/packages/gatsby/src/joi-schemas/__tests__/joi.ts @@ -186,6 +186,7 @@ describe(`gatsby config`, () => { expect.objectContaining({ graphqlTypegen: { typesOutputPath: `src/gatsby-types.d.ts`, + gatsbyBuild: false, }, }) ) @@ -201,12 +202,12 @@ describe(`gatsby config`, () => { expect.objectContaining({ graphqlTypegen: { typesOutputPath: `src/gatsby-types.d.ts`, + gatsbyBuild: false, }, }) ) }) - // TODO: Write "return partial defaults for graphqlTypegen when partial options object is set" test with more graphqlTypegen options it(`graphqlTypegen config object can be overwritten`, () => { const config = { graphqlTypegen: { @@ -219,6 +220,25 @@ describe(`gatsby config`, () => { expect.objectContaining({ graphqlTypegen: { typesOutputPath: `gatsby-types.d.ts`, + gatsbyBuild: false, + }, + }) + ) + }) + + it(`returns partial defaults for graphqlTypegen when partial options object is set`, () => { + const config = { + graphqlTypegen: { + gatsbyBuild: true, + }, + } + + const result = gatsbyConfigSchema.validate(config) + expect(result.value).toEqual( + expect.objectContaining({ + graphqlTypegen: { + typesOutputPath: `src/gatsby-types.d.ts`, + gatsbyBuild: true, }, }) ) diff --git a/packages/gatsby/src/joi-schemas/joi.ts b/packages/gatsby/src/joi-schemas/joi.ts index 8e953ac7ebb85..c5cc47f489c32 100644 --- a/packages/gatsby/src/joi-schemas/joi.ts +++ b/packages/gatsby/src/joi-schemas/joi.ts @@ -61,6 +61,7 @@ export const gatsbyConfigSchema: Joi.ObjectSchema = Joi.object() Joi.object() .keys({ typesOutputPath: Joi.string().default(DEFAULT_TYPES_OUTPUT_PATH), + gatsbyBuild: Joi.boolean().default(false), }) .unknown(false) ) @@ -69,6 +70,7 @@ export const gatsbyConfigSchema: Joi.ObjectSchema = Joi.object() if (value === true) { return { typesOutputPath: DEFAULT_TYPES_OUTPUT_PATH, + gatsbyBuild: false, } } diff --git a/packages/gatsby/src/redux/types.ts b/packages/gatsby/src/redux/types.ts index cb92f3340f91c..b892cf9a0b7f5 100644 --- a/packages/gatsby/src/redux/types.ts +++ b/packages/gatsby/src/redux/types.ts @@ -80,6 +80,7 @@ export interface IGatsbyFunction { export interface IGraphQLTypegenOptions { typesOutputPath: string + gatsbyBuild: boolean } export interface IGatsbyConfig { From c4da7fb21cc7e4a7985822355797b1975be1ce0f Mon Sep 17 00:00:00 2001 From: LekoArts Date: Thu, 18 Aug 2022 07:02:18 +0200 Subject: [PATCH 3/3] change name --- docs/docs/reference/config-files/gatsby-config.md | 2 +- packages/gatsby/index.d.ts | 2 +- packages/gatsby/src/commands/build.ts | 2 +- packages/gatsby/src/joi-schemas/__tests__/joi.ts | 10 +++++----- packages/gatsby/src/joi-schemas/joi.ts | 4 ++-- packages/gatsby/src/redux/types.ts | 2 +- 6 files changed, 11 insertions(+), 11 deletions(-) diff --git a/docs/docs/reference/config-files/gatsby-config.md b/docs/docs/reference/config-files/gatsby-config.md index e46cbe3ade730..f49173853f86c 100644 --- a/docs/docs/reference/config-files/gatsby-config.md +++ b/docs/docs/reference/config-files/gatsby-config.md @@ -206,7 +206,7 @@ module.exports = { You can specifiy the path of the generated TypeScript types file relative to the site root. Default: `src/gatsby-types.d.ts`. -### gatsbyBuild +### generateOnBuild By default, `graphqlTypegen` is only run during `gatsby develop`. Set this option to `true` to create the `src/gatsby-types.d.ts` file also during `gatsby build`. Default: `false`. diff --git a/packages/gatsby/index.d.ts b/packages/gatsby/index.d.ts index 9a3583bf10f1f..7fbb4c467560d 100644 --- a/packages/gatsby/index.d.ts +++ b/packages/gatsby/index.d.ts @@ -258,7 +258,7 @@ export const graphql: (query: TemplateStringsArray) => StaticQueryDocument export interface GraphQLTypegenOptions { typesOutputPath?: string - gatsbyBuild?: boolean + generateOnBuild?: boolean } /** diff --git a/packages/gatsby/src/commands/build.ts b/packages/gatsby/src/commands/build.ts index d5303027e9c46..b8783d6db4c27 100644 --- a/packages/gatsby/src/commands/build.ts +++ b/packages/gatsby/src/commands/build.ts @@ -427,7 +427,7 @@ module.exports = async function build( const graphqlTypegenOptions = config.graphqlTypegen // Only generate types when the option is enabled - if (graphqlTypegenOptions && graphqlTypegenOptions.gatsbyBuild) { + if (graphqlTypegenOptions && graphqlTypegenOptions.generateOnBuild) { const typegenActivity = reporter.activityTimer( `Generating TypeScript types`, { diff --git a/packages/gatsby/src/joi-schemas/__tests__/joi.ts b/packages/gatsby/src/joi-schemas/__tests__/joi.ts index d0b2606504f51..4ba077388a4ed 100644 --- a/packages/gatsby/src/joi-schemas/__tests__/joi.ts +++ b/packages/gatsby/src/joi-schemas/__tests__/joi.ts @@ -186,7 +186,7 @@ describe(`gatsby config`, () => { expect.objectContaining({ graphqlTypegen: { typesOutputPath: `src/gatsby-types.d.ts`, - gatsbyBuild: false, + generateOnBuild: false, }, }) ) @@ -202,7 +202,7 @@ describe(`gatsby config`, () => { expect.objectContaining({ graphqlTypegen: { typesOutputPath: `src/gatsby-types.d.ts`, - gatsbyBuild: false, + generateOnBuild: false, }, }) ) @@ -220,7 +220,7 @@ describe(`gatsby config`, () => { expect.objectContaining({ graphqlTypegen: { typesOutputPath: `gatsby-types.d.ts`, - gatsbyBuild: false, + generateOnBuild: false, }, }) ) @@ -229,7 +229,7 @@ describe(`gatsby config`, () => { it(`returns partial defaults for graphqlTypegen when partial options object is set`, () => { const config = { graphqlTypegen: { - gatsbyBuild: true, + generateOnBuild: true, }, } @@ -238,7 +238,7 @@ describe(`gatsby config`, () => { expect.objectContaining({ graphqlTypegen: { typesOutputPath: `src/gatsby-types.d.ts`, - gatsbyBuild: true, + generateOnBuild: true, }, }) ) diff --git a/packages/gatsby/src/joi-schemas/joi.ts b/packages/gatsby/src/joi-schemas/joi.ts index c5cc47f489c32..a41dc284396e9 100644 --- a/packages/gatsby/src/joi-schemas/joi.ts +++ b/packages/gatsby/src/joi-schemas/joi.ts @@ -61,7 +61,7 @@ export const gatsbyConfigSchema: Joi.ObjectSchema = Joi.object() Joi.object() .keys({ typesOutputPath: Joi.string().default(DEFAULT_TYPES_OUTPUT_PATH), - gatsbyBuild: Joi.boolean().default(false), + generateOnBuild: Joi.boolean().default(false), }) .unknown(false) ) @@ -70,7 +70,7 @@ export const gatsbyConfigSchema: Joi.ObjectSchema = Joi.object() if (value === true) { return { typesOutputPath: DEFAULT_TYPES_OUTPUT_PATH, - gatsbyBuild: false, + generateOnBuild: false, } } diff --git a/packages/gatsby/src/redux/types.ts b/packages/gatsby/src/redux/types.ts index b892cf9a0b7f5..a0c4308780551 100644 --- a/packages/gatsby/src/redux/types.ts +++ b/packages/gatsby/src/redux/types.ts @@ -80,7 +80,7 @@ export interface IGatsbyFunction { export interface IGraphQLTypegenOptions { typesOutputPath: string - gatsbyBuild: boolean + generateOnBuild: boolean } export interface IGatsbyConfig {