Skip to content

Commit

Permalink
ensure we refresh the schema.graphql after a change (#126)
Browse files Browse the repository at this point in the history
  • Loading branch information
JoviDeCroock authored Jan 18, 2024
1 parent 66f5b79 commit 8ac86fb
Show file tree
Hide file tree
Showing 4 changed files with 228 additions and 8 deletions.
5 changes: 5 additions & 0 deletions .changeset/flat-goats-taste.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'fuse': patch
---

Ensure we refresh the `schema.graphql`
13 changes: 12 additions & 1 deletion packages/core/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ prog
}

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

if (opts.server) {
let yoga
Expand Down Expand Up @@ -171,6 +172,13 @@ prog

server.watcher.on('change', async (file) => {
if (file.includes('types/')) {
if (isUsingTada) {
setTimeout(() => {
fetch(
`http://localhost:${opts.port}/api/graphql?query={__typename}`,
)
}, 500)
}
server.restart()
}
})
Expand All @@ -180,9 +188,12 @@ prog
}

if (opts.client) {
if (!(await isUsingGraphQLTada(baseDirectory))) {
if (!isUsingTada) {
await boostrapCodegen(opts.schema, true)
} else {
setTimeout(() => {
fetch(`http://localhost:${opts.port}/api/graphql?query={__typename}`)
}, 1000)
const hasSrcDir = existsSync(path.resolve(baseDirectory, 'src'))
const base = hasSrcDir
? path.resolve(baseDirectory, 'src')
Expand Down
53 changes: 46 additions & 7 deletions packages/core/src/next/plugin.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import path from 'path'
import { generate, CodegenContext } from '@graphql-codegen/cli'
import { existsSync, promises as fs } from 'fs'
import { existsSync, promises as fs, watch } from 'fs'
import { resolve } from 'path'
import { DateTimeResolver, JSONResolver } from 'graphql-scalars'

Expand All @@ -19,13 +19,52 @@ export function nextFusePlugin(options: Options = {}) {
isUsingGraphQLTada(process.cwd()).then((isUsing) => {
boostrapFuse(isUsing)
try {
setTimeout(() => {
try {
if (!isUsing) {
if (isUsing) {
let baseDirectory = process.cwd()
const hasSrcDir = existsSync(resolve(baseDirectory, 'src'))
if (hasSrcDir) {
baseDirectory = resolve(baseDirectory, 'src')
}

setTimeout(() => {
fetch(
`http://localhost:${options.port || 3000}/api/${
options.path || 'fuse'
}?query={__typename}`,
)
}, 1000)
const watcher = watch(
resolve(baseDirectory, 'types'),
{ recursive: true },
() => {
setTimeout(() => {
fetch(
`http://localhost:${options.port || 3000}/api/${
options.path || 'fuse'
}?query={__typename}`,
)
}, 1000)
},
)

function exitHandler() {
try {
watcher.close()
} catch (e) {}
}

process.on('exit', exitHandler)
process.on('SIGINT', exitHandler)
process.on('SIGUSR1', exitHandler)
process.on('SIGUSR2', exitHandler)
process.on('uncaughtException', exitHandler)
} else {
setTimeout(() => {
try {
boostrapCodegen(options.port || 3000, options.path || 'fuse')
}
} catch (e) {}
}, 1000)
} catch (e) {}
}, 1000)
}
} catch (e) {}
})
}
Expand Down
165 changes: 165 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 8ac86fb

Please sign in to comment.