From 52866a5fd411c8bf0ea52af1e9b37bb09d787d9a Mon Sep 17 00:00:00 2001 From: Tiago Gimenes Date: Mon, 23 Aug 2021 16:48:09 -0300 Subject: [PATCH 1/2] export types --- .../gatsby-plugin-graphql/src/gatsby-node.ts | 33 +++++-- packages/gatsby-plugin-graphql/src/webpack.ts | 87 +++++++++++++------ 2 files changed, 88 insertions(+), 32 deletions(-) diff --git a/packages/gatsby-plugin-graphql/src/gatsby-node.ts b/packages/gatsby-plugin-graphql/src/gatsby-node.ts index 26136f47c1..6964165fe2 100644 --- a/packages/gatsby-plugin-graphql/src/gatsby-node.ts +++ b/packages/gatsby-plugin-graphql/src/gatsby-node.ts @@ -1,14 +1,29 @@ +import { join } from 'path' + import { makeExecutableSchema } from '@graphql-tools/schema' import { parse, printSchema } from 'graphql' -import type { CreatePageArgs, CreateWebpackConfigArgs } from 'gatsby' +import type { + CreatePageArgs, + CreateWebpackConfigArgs, + PluginOptionsSchemaArgs, +} from 'gatsby' import { WebpackPlugin } from './webpack' -export const onCreateWebpackConfig = async ({ - actions: { setWebpackConfig }, - store, - stage, -}: CreateWebpackConfigArgs) => { +interface Options { + schemaPath?: string +} + +export const pluginOptionsSchema = ({ Joi }: PluginOptionsSchemaArgs) => { + return Joi.object({ + schemaPath: Joi.string(), + }) +} + +export const onCreateWebpackConfig = async ( + { actions: { setWebpackConfig }, store, stage }: CreateWebpackConfigArgs, + options: Options +) => { if (stage === 'build-html' || stage === 'develop-html') { return } @@ -23,9 +38,13 @@ export const onCreateWebpackConfig = async ({ const { schema: dirtySchema } = store.getState() const typeDefs = parse(printSchema(dirtySchema)) const schema = makeExecutableSchema({ typeDefs }) + const schemaPath = join( + process.cwd(), + options.schemaPath ?? '/src/typings/schema.graphql.d.ts' + ) setWebpackConfig({ - plugins: [new WebpackPlugin(schema)], + plugins: [new WebpackPlugin(schema, { schemaPath })], }) } diff --git a/packages/gatsby-plugin-graphql/src/webpack.ts b/packages/gatsby-plugin-graphql/src/webpack.ts index 75b216c079..57717e7901 100644 --- a/packages/gatsby-plugin-graphql/src/webpack.ts +++ b/packages/gatsby-plugin-graphql/src/webpack.ts @@ -34,6 +34,14 @@ export const publicPath = '/page-data/_graphql' export const target = join(root, 'public', publicPath) +const disclaimer = ` +/** + * Warning: This is an autogenerated file. + * + * Changes in this file won't take effect and will be overwritten + */ +` + const queryCode = ({ name, value, sha256Hash }: QueryNode) => ` export const ${name} = { query: process.env.NODE_ENV === 'production' ? undefined : ${JSON.stringify( @@ -45,22 +53,7 @@ export const ${name} = { ` const wrapTypes = (types: string, node: QueryNode | null) => ` -/** - * Warning: This is an autogenerated file. - * - * Changes in this file won't take effect and will be overwritten - */ - -// Base Types -${typeScriptPlugin.EXACT_SIGNATURE} -type Maybe = T | null | undefined -type Scalars = { - Boolean: boolean - String: string - Float: number - Int: number - ID: string -} +${disclaimer} // Operation related types ${types} @@ -69,6 +62,13 @@ ${types} ${node ? queryCode(node) : ''} ` +const wrapSchema = (schema: string) => + ` +${disclaimer} + +${schema} +`.trim() + type QueryNode = Node & { sha256Hash: string } type FragmentNode = Node @@ -80,7 +80,10 @@ export class WebpackPlugin { public queryInfoPath: string public cachePath: string - constructor(public schema: GraphQLSchema) { + constructor( + public schema: GraphQLSchema, + public options: { schemaPath: string } + ) { this.persistedPath = join(root, 'public', publicPath, persisted) this.queryInfoPath = join(root, 'public', publicPath, queryInfo) this.cachePath = join(root, 'public', publicPath, cache) @@ -106,7 +109,39 @@ export class WebpackPlugin { return print(optimized[0]) } - public generateCode = async (nodes: Array) => { + public generateSchemaCode = async () => + codegen({ + config: { + avoidOptionals: true, + preResolveTypes: true, + enumsAsTypes: true, + skipTypeNameForRoot: true, + skipTypename: true, + noExport: true, + allowEnumStringTypes: true, + namingConvention: 'change-case-all#pascalCase', + }, + documents: [], + // used by a plugin internally, although the 'typescript' plugin currently + // returns the string output, rather than writing to a file + filename: this.options.schemaPath, + schemaAst: this.schema, + schema: parse(printSchema(this.schema)), + // Plugins to use + pluginMap: { + typeScript: typeScriptPlugin, + }, + // Plugins configurations + plugins: [ + { + typeScript: {}, + }, + ], + }).then(wrapSchema) + + public generateOperationsCode = async ( + nodes: Array + ) => { return Promise.all( nodes.map(async (node) => { const { value, filename, ...rest } = node @@ -117,6 +152,7 @@ export class WebpackPlugin { enumsAsTypes: true, skipTypeNameForRoot: true, skipTypename: true, + namingConvention: 'change-case-all#pascalCase', }, documents: [{ document: parse(value) }], // used by a plugin internally, although the 'typescript' plugin currently @@ -193,14 +229,14 @@ export class WebpackPlugin { // ------------------------------------- // Generate code using @graphql-codegen - const codeNodes = await this.generateCode([ - ...optimizedQueries, - ...allFragments, + const [schemaNode, operationNodes] = await Promise.all([ + this.generateSchemaCode(), + this.generateOperationsCode([...optimizedQueries, ...allFragments]), ]) // write generated files - await Promise.all( - codeNodes.map(async ({ value, filename: filepath, name }) => { + await Promise.all([ + ...operationNodes.map(async ({ value, filename: filepath, name }) => { const filename = join( dirname(filepath), '__generated__', @@ -208,8 +244,9 @@ export class WebpackPlugin { ) return outputFile(filename, value) - }) - ) + }), + outputFile(this.options.schemaPath, schemaNode), + ]) await fsExtraOutputFile(this.cachePath, serialize(manager)) } catch (err) { From aee0fbab9164eea2176bc838771164e08c69fb66 Mon Sep 17 00:00:00 2001 From: Tiago Gimenes Date: Mon, 23 Aug 2021 21:05:30 -0300 Subject: [PATCH 2/2] remove enum string types --- packages/gatsby-plugin-graphql/src/webpack.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/packages/gatsby-plugin-graphql/src/webpack.ts b/packages/gatsby-plugin-graphql/src/webpack.ts index 57717e7901..6e06d9a8b6 100644 --- a/packages/gatsby-plugin-graphql/src/webpack.ts +++ b/packages/gatsby-plugin-graphql/src/webpack.ts @@ -112,13 +112,12 @@ export class WebpackPlugin { public generateSchemaCode = async () => codegen({ config: { + allowEnumStringTypes: false, avoidOptionals: true, - preResolveTypes: true, enumsAsTypes: true, skipTypeNameForRoot: true, skipTypename: true, noExport: true, - allowEnumStringTypes: true, namingConvention: 'change-case-all#pascalCase', }, documents: [],