Skip to content

Commit

Permalink
feat(gql): Codemod existing projects to get newest gql config (#9959)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tobbe authored Feb 2, 2024
1 parent 1039545 commit 5aa6991
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 4 deletions.
4 changes: 2 additions & 2 deletions packages/cli/src/testUtils/matchFolderTransform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ type Options = {

type MatchFolderTransformFunction = (
transformFunctionOrName: (() => any) | string,
fixtureName: string,
fixtureName?: string,
options?: Options
) => Promise<void>

Expand Down Expand Up @@ -53,7 +53,7 @@ export const matchFolderTransform: MatchFolderTransformFunction = async (
const fixtureFolder = path.join(
testPath,
'../../__testfixtures__',
fixtureName
fixtureName || ''
)

const fixtureInputDir = path.join(fixtureFolder, 'input')
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const { getPaths } = require('@redwoodjs/internal')

module.exports = {
schema: getPaths().generated.schema,
}
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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)
})
})
Original file line number Diff line number Diff line change
@@ -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)
}
Original file line number Diff line number Diff line change
@@ -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()
})
}
4 changes: 2 additions & 2 deletions packages/codemods/src/testUtils/matchFolderTransform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type Options = {

type MatchFolderTransformFunction = (
transformFunctionOrName: (() => any) | string,
fixtureName: string,
fixtureName?: string,
options?: Options
) => Promise<void>

Expand Down Expand Up @@ -47,7 +47,7 @@ export const matchFolderTransform: MatchFolderTransformFunction = async (
const fixtureFolder = path.join(
testPath,
'../../__testfixtures__',
fixtureName
fixtureName || ''
)

const fixtureInputDir = path.join(fixtureFolder, 'input')
Expand Down

0 comments on commit 5aa6991

Please sign in to comment.