Skip to content

Commit

Permalink
feat: ✨ allow #import syntax in graphql files
Browse files Browse the repository at this point in the history
Co-authored-by: wubolin001 <[email protected]>
  • Loading branch information
DerZade and clever-vpn committed Jul 10, 2024
1 parent 080d958 commit 6799e25
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 11 deletions.
76 changes: 70 additions & 6 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
"@graphql-codegen/typed-document-node": "^5.0.7",
"@graphql-codegen/typescript": "^4.0.7",
"@graphql-codegen/typescript-operations": "^4.2.1",
"@graphql-tools/graphql-file-loader": "^8.0.1",
"@graphql-tools/load": "^8.0.2",
"commander": "^12.1.0",
"esbuild": "^0.21.4",
Expand Down
6 changes: 2 additions & 4 deletions src/declarations.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type { DocumentNode } from 'graphql';
import { loadDocuments } from '@graphql-tools/load';
import { GraphQLFileLoader } from '@graphql-tools/graphql-file-loader';
import { writeFile } from 'fs/promises';
import { codegenTypedDocumentNode } from './utils';
import { readFile } from 'fs/promises';
import { GraphQLPluginOptions } from '.';

/**
Expand All @@ -24,9 +24,7 @@ export async function writeOperationDeclarations(
options: GraphQLPluginOptions = {},
schemaImports = ''
) {
const operationSrc = await readFile(path, 'utf-8');

const [doc] = await loadDocuments(operationSrc, { loaders: [] });
const [doc] = await loadDocuments(path, { loaders: [new GraphQLFileLoader()] });

const typeScript = await codegenTypedDocumentNode(
schema,
Expand Down
3 changes: 2 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import type { DocumentNode } from 'graphql';
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';

const EXT = /\.(gql|graphql)$/;

Expand Down Expand Up @@ -134,7 +135,7 @@ export default function typedGraphQLPlugin(options: GraphQLPluginOptions = {}):

resetGQLTagCaches();

const [doc] = await loadDocuments(src, { loaders: [] });
const [doc] = await loadDocuments(id, { loaders: [new GraphQLFileLoader()] });

return {
code: await codegenTypedDocumentNode(SCHEMA, doc, {
Expand Down

0 comments on commit 6799e25

Please sign in to comment.