From 2df05e33c87388945b7dc068946f2cfe69009a83 Mon Sep 17 00:00:00 2001 From: Tobbe Lundberg Date: Fri, 2 Feb 2024 20:04:56 +0100 Subject: [PATCH] feat(gql): Codemod existing projects to get newest gql config (#9959) --- .../cli/src/testUtils/matchFolderTransform.ts | 4 ++-- .../v7.x.x/updateGraphQLConfig/README.md | 8 ++++++++ .../__testfixtures__/input/graphql.config.js | 5 +++++ .../__testfixtures__/output/graphql.config.js | 11 +++++++++++ .../__tests__/updateGraphqlConfig.test.ts | 7 +++++++ .../updateGraphQLConfig/updateGraphqlConfig.ts | 17 +++++++++++++++++ .../updateGraphqlConfig.yargs.ts | 13 +++++++++++++ .../src/testUtils/matchFolderTransform.ts | 4 ++-- 8 files changed, 65 insertions(+), 4 deletions(-) create mode 100644 packages/codemods/src/codemods/v7.x.x/updateGraphQLConfig/README.md create mode 100644 packages/codemods/src/codemods/v7.x.x/updateGraphQLConfig/__testfixtures__/input/graphql.config.js create mode 100644 packages/codemods/src/codemods/v7.x.x/updateGraphQLConfig/__testfixtures__/output/graphql.config.js create mode 100644 packages/codemods/src/codemods/v7.x.x/updateGraphQLConfig/__tests__/updateGraphqlConfig.test.ts create mode 100644 packages/codemods/src/codemods/v7.x.x/updateGraphQLConfig/updateGraphqlConfig.ts create mode 100644 packages/codemods/src/codemods/v7.x.x/updateGraphQLConfig/updateGraphqlConfig.yargs.ts diff --git a/packages/cli/src/testUtils/matchFolderTransform.ts b/packages/cli/src/testUtils/matchFolderTransform.ts index c17b14822db6..03bdb766c7c3 100644 --- a/packages/cli/src/testUtils/matchFolderTransform.ts +++ b/packages/cli/src/testUtils/matchFolderTransform.ts @@ -21,7 +21,7 @@ type Options = { type MatchFolderTransformFunction = ( transformFunctionOrName: (() => any) | string, - fixtureName: string, + fixtureName?: string, options?: Options ) => Promise @@ -53,7 +53,7 @@ export const matchFolderTransform: MatchFolderTransformFunction = async ( const fixtureFolder = path.join( testPath, '../../__testfixtures__', - fixtureName + fixtureName || '' ) const fixtureInputDir = path.join(fixtureFolder, 'input') diff --git a/packages/codemods/src/codemods/v7.x.x/updateGraphQLConfig/README.md b/packages/codemods/src/codemods/v7.x.x/updateGraphQLConfig/README.md new file mode 100644 index 000000000000..cb6ad5fb8147 --- /dev/null +++ b/packages/codemods/src/codemods/v7.x.x/updateGraphQLConfig/README.md @@ -0,0 +1,8 @@ +# Update graphql.config.js + +Replaces the root `graphql.config.js` file with one that includes types and gql +documents support. + +Fetches the file from the create-redwood-app template files on GitHub + +No jscodeshift is involved. diff --git a/packages/codemods/src/codemods/v7.x.x/updateGraphQLConfig/__testfixtures__/input/graphql.config.js b/packages/codemods/src/codemods/v7.x.x/updateGraphQLConfig/__testfixtures__/input/graphql.config.js new file mode 100644 index 000000000000..2da7862f6b57 --- /dev/null +++ b/packages/codemods/src/codemods/v7.x.x/updateGraphQLConfig/__testfixtures__/input/graphql.config.js @@ -0,0 +1,5 @@ +const { getPaths } = require('@redwoodjs/internal') + +module.exports = { + schema: getPaths().generated.schema, +} diff --git a/packages/codemods/src/codemods/v7.x.x/updateGraphQLConfig/__testfixtures__/output/graphql.config.js b/packages/codemods/src/codemods/v7.x.x/updateGraphQLConfig/__testfixtures__/output/graphql.config.js new file mode 100644 index 000000000000..c564451acb66 --- /dev/null +++ b/packages/codemods/src/codemods/v7.x.x/updateGraphQLConfig/__testfixtures__/output/graphql.config.js @@ -0,0 +1,11 @@ +// This file is used by the VSCode GraphQL extension + +const { getPaths } = require('@redwoodjs/project-config') + +/** @type {import('graphql-config').IGraphQLConfig} */ +const config = { + schema: getPaths().generated.schema, + documents: './web/src/**/!(*.d).{ts,tsx,js,jsx}', +} + +module.exports = config diff --git a/packages/codemods/src/codemods/v7.x.x/updateGraphQLConfig/__tests__/updateGraphqlConfig.test.ts b/packages/codemods/src/codemods/v7.x.x/updateGraphQLConfig/__tests__/updateGraphqlConfig.test.ts new file mode 100644 index 000000000000..b7a79df8ee9b --- /dev/null +++ b/packages/codemods/src/codemods/v7.x.x/updateGraphQLConfig/__tests__/updateGraphqlConfig.test.ts @@ -0,0 +1,7 @@ +import { updateGraphqlConfig } from '../updateGraphqlConfig' + +describe('updateGraphQLConfig', () => { + it('Replaces graphql.config.js with a new version downloaded from GH', async () => { + await matchFolderTransform(updateGraphqlConfig) + }) +}) diff --git a/packages/codemods/src/codemods/v7.x.x/updateGraphQLConfig/updateGraphqlConfig.ts b/packages/codemods/src/codemods/v7.x.x/updateGraphQLConfig/updateGraphqlConfig.ts new file mode 100644 index 000000000000..e00172c980d7 --- /dev/null +++ b/packages/codemods/src/codemods/v7.x.x/updateGraphQLConfig/updateGraphqlConfig.ts @@ -0,0 +1,17 @@ +import fs from 'fs' +import path from 'path' + +import { fetch } from '@whatwg-node/fetch' + +import { getPaths } from '@redwoodjs/project-config' + +export const updateGraphqlConfig = async () => { + const res = await fetch( + // TODO: Have to come back here to update the URL when we have a more + // stable location than main + // 'https://raw.githubusercontent.com/redwoodjs/redwood/release/major/v7.0.0/packages/create-redwood-app/templates/ts/graphql.config.js' + 'https://raw.githubusercontent.com/redwoodjs/redwood/main/packages/create-redwood-app/templates/ts/graphql.config.js' + ) + const text = await res.text() + fs.writeFileSync(path.join(getPaths().base, 'graphql.config.js'), text) +} diff --git a/packages/codemods/src/codemods/v7.x.x/updateGraphQLConfig/updateGraphqlConfig.yargs.ts b/packages/codemods/src/codemods/v7.x.x/updateGraphQLConfig/updateGraphqlConfig.yargs.ts new file mode 100644 index 000000000000..7e8a7b286534 --- /dev/null +++ b/packages/codemods/src/codemods/v7.x.x/updateGraphQLConfig/updateGraphqlConfig.yargs.ts @@ -0,0 +1,13 @@ +import task from 'tasuku' + +import { updateGraphqlConfig } from './updateGraphqlConfig' + +export const command = 'update-graphql-config' +export const description = + '(v6.x->v7.x) Update graphql.config.js from the create-redwood-app template' + +export const handler = () => { + task('Update root graphql.config.js file', async () => { + await updateGraphqlConfig() + }) +} diff --git a/packages/codemods/src/testUtils/matchFolderTransform.ts b/packages/codemods/src/testUtils/matchFolderTransform.ts index a56298c6ae84..8c7a77cbf2ef 100644 --- a/packages/codemods/src/testUtils/matchFolderTransform.ts +++ b/packages/codemods/src/testUtils/matchFolderTransform.ts @@ -19,7 +19,7 @@ type Options = { type MatchFolderTransformFunction = ( transformFunctionOrName: (() => any) | string, - fixtureName: string, + fixtureName?: string, options?: Options ) => Promise @@ -47,7 +47,7 @@ export const matchFolderTransform: MatchFolderTransformFunction = async ( const fixtureFolder = path.join( testPath, '../../__testfixtures__', - fixtureName + fixtureName || '' ) const fixtureInputDir = path.join(fixtureFolder, 'input')