From 042a8d1b5707d7f4fd7b220945ff3b50e4bdb98f Mon Sep 17 00:00:00 2001 From: DerZade Date: Wed, 10 Jul 2024 21:46:25 +0200 Subject: [PATCH] fix: :bug: add imports in graphql as watched files add imports in graphql as watched files to ensure the importer is invalidated if the imported file changes --- package-lock.json | 1 + package.json | 1 + src/index.ts | 13 +++++++++++++ 3 files changed, 15 insertions(+) diff --git a/package-lock.json b/package-lock.json index 9f218db..8a37dc0 100644 --- a/package-lock.json +++ b/package-lock.json @@ -14,6 +14,7 @@ "@graphql-codegen/typescript": "^4.0.7", "@graphql-codegen/typescript-operations": "^4.2.1", "@graphql-tools/graphql-file-loader": "^8.0.1", + "@graphql-tools/import": "^7.0.1", "@graphql-tools/load": "^8.0.2", "commander": "^12.1.0", "esbuild": "^0.21.4", diff --git a/package.json b/package.json index 227f0d2..b64ca88 100644 --- a/package.json +++ b/package.json @@ -50,6 +50,7 @@ "@graphql-codegen/typescript": "^4.0.7", "@graphql-codegen/typescript-operations": "^4.2.1", "@graphql-tools/graphql-file-loader": "^8.0.1", + "@graphql-tools/import": "^7.0.1", "@graphql-tools/load": "^8.0.2", "commander": "^12.1.0", "esbuild": "^0.21.4", diff --git a/src/index.ts b/src/index.ts index fd45eae..21ff28b 100644 --- a/src/index.ts +++ b/src/index.ts @@ -9,6 +9,7 @@ import { DeclarationWriter } from './declarations_writer'; import { TypeScriptPluginConfig } from '@graphql-codegen/typescript'; import type { TypeScriptDocumentsPluginConfig } from '@graphql-codegen/typescript-operations'; import { GraphQLFileLoader } from '@graphql-tools/graphql-file-loader'; +import { extractImportLines, parseImportLine } from '@graphql-tools/import'; const EXT = /\.(gql|graphql)$/; @@ -135,6 +136,18 @@ export default function typedGraphQLPlugin(options: GraphQLPluginOptions = {}): resetGQLTagCaches(); + const { importLines } = extractImportLines(src); + + await Promise.all( + importLines.map(async (line) => { + let { from } = parseImportLine(line.replace(/^#/, '').trim()); + from = from.startsWith('./') || from.startsWith('/') ? from : `./${from}`; + const importId = await this.resolve(from, id); + if (importId === null) return; + this.addWatchFile(importId.id); + }) + ); + const [doc] = await loadDocuments(id, { loaders: [new GraphQLFileLoader()] }); return {