Skip to content

Commit

Permalink
increase timeout for CI environments
Browse files Browse the repository at this point in the history
  • Loading branch information
JoviDeCroock committed Feb 6, 2024
1 parent 8605083 commit 58095aa
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 86 deletions.
89 changes: 11 additions & 78 deletions packages/core/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,13 @@ import { existsSync } from 'fs'
import fs from 'fs/promises'
import { createServer, build } from 'vite'
import { VitePluginNode } from 'vite-plugin-node'
import { generate, CodegenContext } from '@graphql-codegen/cli'
import { DateTimeResolver, JSONResolver } from 'graphql-scalars'

import {
ensureTadaIntrospection,
isUsingGraphQLTada,
tadaGqlContents,
} from './utils/gql-tada'
import { boostrapCodegen } from './utils/codegen'

const prog = sade('fuse')

Expand Down Expand Up @@ -148,6 +147,7 @@ prog

const baseDirectory = process.cwd()
const isUsingTada = opts.client && (await isUsingGraphQLTada(baseDirectory))
let hasTadaWatcherRunning = false

if (opts.server) {
let yoga
Expand All @@ -161,7 +161,12 @@ prog
path.resolve(baseDirectory, 'schema.graphql'),
yo.stringifiedSchema,
'utf-8',
)
).then(() => {
if (isUsingTada && !hasTadaWatcherRunning) {
hasTadaWatcherRunning = true
ensureTadaIntrospection(baseDirectory, true)
}
})

return yo
})
Expand All @@ -183,9 +188,7 @@ prog
if (file.includes('types/')) {
if (isUsingTada) {
setTimeout(() => {
fetch(
`http://localhost:${opts.port}/api/graphql?query={__typename}`,
)
fetch(`http://localhost:${opts.port}/graphql?query={__typename}`)
}, 500)
}
server.restart()
Expand All @@ -200,18 +203,14 @@ prog
if (!isUsingTada) {
setTimeout(() => {
fetch(
`http://localhost:${opts.port}/api/graphql?query={__typename}`,
`http://localhost:${opts.port}/graphql?query={__typename}`,
).then(() => {
boostrapCodegen(opts.schema, true)
})
}, 1000)
} else {
setTimeout(() => {
fetch(
`http://localhost:${opts.port}/api/graphql?query={__typename}`,
).then(() => {
ensureTadaIntrospection(baseDirectory, true)
})
fetch(`http://localhost:${opts.port}/graphql?query={__typename}`)
}, 1000)
const hasSrcDir = existsSync(path.resolve(baseDirectory, 'src'))
const base = hasSrcDir
Expand All @@ -234,69 +233,3 @@ prog
})

prog.parse(process.argv)

async function boostrapCodegen(location: string, watch: boolean) {
const baseDirectory = process.cwd()
const hasSrcDir = existsSync(path.resolve(baseDirectory, 'src'))

const contents = `export * from "./fragment-masking";
export * from "./gql";
export * from "fuse/client";\n`
const ctx = new CodegenContext({
filepath: 'codgen.yml',
config: {
ignoreNoDocuments: true,
errorsOnly: true,
noSilentErrors: true,
hooks: {
afterOneFileWrite: async () => {
await fs.writeFile(
hasSrcDir
? baseDirectory + '/src/fuse/index.ts'
: baseDirectory + '/fuse/index.ts',
contents,
)
},
},
watch: watch
? [
hasSrcDir
? baseDirectory + '/src/**/*.{ts,tsx}'
: baseDirectory + '/**/*.{ts,tsx}',
'!./{node_modules,.next,.git}/**/*',
hasSrcDir ? '!./src/fuse/*.{ts,tsx}' : '!./fuse/*.{ts,tsx}',
]
: false,
schema: location,
generates: {
[hasSrcDir ? baseDirectory + '/src/fuse/' : baseDirectory + '/fuse/']: {
documents: [
hasSrcDir ? './src/**/*.{ts,tsx}' : './**/*.{ts,tsx}',
'!./{node_modules,.next,.git}/**/*',
hasSrcDir ? '!./src/fuse/*.{ts,tsx}' : '!./fuse/*.{ts,tsx}',
],
preset: 'client',
// presetConfig: {
// persistedDocuments: true,
// },
config: {
scalars: {
ID: {
input: 'string',
output: 'string',
},
DateTime: DateTimeResolver.extensions.codegenScalarType,
JSON: JSONResolver.extensions.codegenScalarType,
},
avoidOptionals: false,
enumsAsTypes: true,
nonOptionalTypename: true,
skipTypename: false,
},
},
},
},
})

await generate(ctx, true)
}
70 changes: 70 additions & 0 deletions packages/core/src/utils/codegen.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import { generate, CodegenContext } from '@graphql-codegen/cli'
import { DateTimeResolver, JSONResolver } from 'graphql-scalars'
import { existsSync, promises as fs } from 'fs'
import path from 'path'

export async function boostrapCodegen(location: string, watch: boolean) {
const baseDirectory = process.cwd()
const hasSrcDir = existsSync(path.resolve(baseDirectory, 'src'))

const contents = `export * from "./fragment-masking";
export * from "./gql";
export * from "fuse/client";\n`
const ctx = new CodegenContext({
filepath: 'codgen.yml',
config: {
ignoreNoDocuments: true,
errorsOnly: true,
noSilentErrors: true,
hooks: {
afterOneFileWrite: async () => {
await fs.writeFile(
hasSrcDir
? baseDirectory + '/src/fuse/index.ts'
: baseDirectory + '/fuse/index.ts',
contents,
)
},
},
watch: watch
? [
hasSrcDir
? baseDirectory + '/src/**/*.{ts,tsx}'
: baseDirectory + '/**/*.{ts,tsx}',
'!./{node_modules,.next,.git}/**/*',
hasSrcDir ? '!./src/fuse/*.{ts,tsx}' : '!./fuse/*.{ts,tsx}',
]
: false,
schema: location,
generates: {
[hasSrcDir ? baseDirectory + '/src/fuse/' : baseDirectory + '/fuse/']: {
documents: [
hasSrcDir ? './src/**/*.{ts,tsx}' : './**/*.{ts,tsx}',
'!./{node_modules,.next,.git}/**/*',
hasSrcDir ? '!./src/fuse/*.{ts,tsx}' : '!./fuse/*.{ts,tsx}',
],
preset: 'client',
// presetConfig: {
// persistedDocuments: true,
// },
config: {
scalars: {
ID: {
input: 'string',
output: 'string',
},
DateTime: DateTimeResolver.extensions.codegenScalarType,
JSON: JSONResolver.extensions.codegenScalarType,
},
avoidOptionals: false,
enumsAsTypes: true,
nonOptionalTypename: true,
skipTypename: false,
},
},
},
},
})

await generate(ctx, true)
}
5 changes: 4 additions & 1 deletion packages/core/src/utils/gql-tada.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,13 @@ export async function ensureTadaIntrospection(
].join('\n')

await fs.writeFile(outputLocation, contents)
} catch (e) {}
} catch (e) {
console.error(e)
}
}

await writeTada()

if (shouldWatch) {
watch(schemaLocation, async () => {
await writeTada()
Expand Down
21 changes: 14 additions & 7 deletions packages/core/test/cli.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ describe.each(allFixtures)('%s', (fixtureName) => {
})

test('Should run the dev command', async () => {
process = execa('pnpm', ['fuse', 'dev', '--server'], {
process = execa('pnpm', ['fuse', 'dev', '--server', '--port', '4000'], {
cwd: fixtureDir,
})

Expand All @@ -76,7 +76,7 @@ describe.each(allFixtures)('%s', (fixtureName) => {

if (fixtureName === 'tada') {
test('Should run the client dev command', async () => {
process = execa('pnpm', ['fuse', 'dev'], {
process = execa('pnpm', ['fuse', 'dev', '--port', '4001'], {
cwd: fixtureDir,
})

Expand All @@ -91,19 +91,26 @@ describe.each(allFixtures)('%s', (fixtureName) => {
})

// We have a timeout internally to generate the schema of 1 second
await wait(2000)
await new Promise((resolve) => {
let interval = setInterval(() => {
if (fs.existsSync(path.join(fixtureDir, 'schema.graphql'))) {
clearInterval(interval)
resolve(null)
}
}, 500)
})

expect(existsSync(path.join(fixtureDir, 'fuse'))).toBe(true)
expect(existsSync(path.join(fixtureDir, 'fuse', 'tada.ts'))).toBe(true)
expect(existsSync(path.join(fixtureDir, 'fuse', 'index.ts'))).toBe(true)
expect(
existsSync(path.join(fixtureDir, 'fuse', 'introspection.ts')),
).toBe(true)
expect(existsSync(path.join(fixtureDir, 'fuse', 'tada.ts'))).toBe(true)
expect(existsSync(path.join(fixtureDir, 'fuse', 'index.ts'))).toBe(true)
}, 10_000)
}, 15_000)
}

test('Should run the build command', async () => {
process = execa('pnpm', ['fuse', 'build', '--server'], {
process = execa('pnpm', ['fuse', 'build', '--server', '--port', '4002'], {
cwd: fixtureDir,
})

Expand Down

0 comments on commit 58095aa

Please sign in to comment.