Skip to content

Commit

Permalink
ensure we refresh the schema.graphql after a change
Browse files Browse the repository at this point in the history
  • Loading branch information
JoviDeCroock committed Jan 18, 2024
1 parent 66f5b79 commit b5d552b
Show file tree
Hide file tree
Showing 6 changed files with 228 additions and 10 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`
2 changes: 1 addition & 1 deletion examples/standalone/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ type Launch implements Node {
id: ID!
image: String!
launchDate: String!
name: String!
nam: String!
rocket: Rocket!
site: Site!
}
Expand Down
2 changes: 1 addition & 1 deletion examples/standalone/src/fuse/introspection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ const introspection = {
args: [],
},
{
name: 'name',
name: 'nam',
type: {
kind: 'NON_NULL',
ofType: {
Expand Down
10 changes: 9 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,7 +188,7 @@ prog
}

if (opts.client) {
if (!(await isUsingGraphQLTada(baseDirectory))) {
if (!isUsingTada) {
await boostrapCodegen(opts.schema, true)
} else {
const hasSrcDir = existsSync(path.resolve(baseDirectory, 'src'))
Expand Down
54 changes: 47 additions & 7 deletions packages/core/src/next/plugin.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
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'

import { isUsingGraphQLTada, tadaGqlContents } from '../utils/gql-tada'
import { sign } from 'crypto'

interface Options {
port?: number
Expand All @@ -19,13 +20,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 b5d552b

Please sign in to comment.