Skip to content

Commit aaa517a

Browse files
ikusakov2ikusakoveddeee888
authored
Don't render external documents (#1456)
* dont render external documents * Use latest codegen * Add changeset * Format --------- Co-authored-by: Igor Kusakov <[email protected]> Co-authored-by: Eddy Nguyen <[email protected]>
1 parent 908725e commit aaa517a

5 files changed

Lines changed: 492 additions & 264 deletions

File tree

.changeset/bumpy-feet-stick.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@graphql-codegen/near-operation-file-preset': minor
3+
---
4+
5+
Handle externalDocuments from Codegen CLI

packages/presets/near-operation-file/package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,15 @@
3939
"graphql": "^0.8.0 || ^0.9.0 || ^0.10.0 || ^0.11.0 || ^0.12.0 || ^0.13.0 || ^14.0.0 || ^15.0.0 || ^16.0.0"
4040
},
4141
"dependencies": {
42-
"@graphql-codegen/add": "^6.0.0",
43-
"@graphql-codegen/plugin-helpers": "^6.3.0",
44-
"@graphql-codegen/visitor-plugin-common": "^6.3.0",
42+
"@graphql-codegen/add": "^7.0.0",
43+
"@graphql-codegen/plugin-helpers": "^7.0.0",
44+
"@graphql-codegen/visitor-plugin-common": "^7.0.0",
4545
"@graphql-tools/utils": "^11.0.0",
4646
"parse-filepath": "^1.0.2",
4747
"tslib": "^2.8.1"
4848
},
4949
"devDependencies": {
50-
"@graphql-codegen/cli": "6.3.1",
50+
"@graphql-codegen/cli": "7.0.0",
5151
"@types/parse-filepath": "1.0.2"
5252
},
5353
"publishConfig": {

packages/presets/near-operation-file/src/resolve-document-imports.ts

Lines changed: 58 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -78,67 +78,69 @@ export function resolveDocumentImports<T>(
7878
const { baseOutputDir, documents } = presetOptions;
7979
const { generateFilePath, schemaTypesSource, baseDir, typesImport } = importResolverOptions;
8080

81-
return documents.map(documentFile => {
82-
try {
83-
const meta: {
84-
operations: OperationDefinitionNode[];
85-
fragments: FragmentDefinitionNode[];
86-
} = {
87-
operations: [],
88-
fragments: [],
89-
};
90-
for (const definition of documentFile.document.definitions) {
91-
if (definition.kind === Kind.OPERATION_DEFINITION) {
92-
meta.operations.push(definition);
93-
} else if (definition.kind === Kind.FRAGMENT_DEFINITION) {
94-
meta.fragments.push(definition);
81+
return documents
82+
.filter(documentFile => documentFile.type !== 'external')
83+
.map(documentFile => {
84+
try {
85+
const meta: {
86+
operations: OperationDefinitionNode[];
87+
fragments: FragmentDefinitionNode[];
88+
} = {
89+
operations: [],
90+
fragments: [],
91+
};
92+
for (const definition of documentFile.document.definitions) {
93+
if (definition.kind === Kind.OPERATION_DEFINITION) {
94+
meta.operations.push(definition);
95+
} else if (definition.kind === Kind.FRAGMENT_DEFINITION) {
96+
meta.fragments.push(definition);
97+
}
9598
}
96-
}
97-
const generatedFilePath = generateFilePath({ location: documentFile.location, meta });
99+
const generatedFilePath = generateFilePath({ location: documentFile.location, meta });
98100

99-
const importStatements: string[] = [];
100-
const { externalFragments, fragmentImports } = resolveFragments(
101-
generatedFilePath,
102-
documentFile.document,
103-
);
101+
const importStatements: string[] = [];
102+
const { externalFragments, fragmentImports } = resolveFragments(
103+
generatedFilePath,
104+
documentFile.document,
105+
);
104106

105-
const externalFragmentsInjectedDocument = {
106-
...documentFile.document,
107-
definitions: [
108-
...documentFile.document.definitions,
109-
...externalFragments.map(fragment => fragment.node),
110-
],
111-
};
107+
const externalFragmentsInjectedDocument = {
108+
...documentFile.document,
109+
definitions: [
110+
...documentFile.document.definitions,
111+
...externalFragments.map(fragment => fragment.node),
112+
],
113+
};
112114

113-
if (
114-
isUsingTypes(externalFragmentsInjectedDocument, [], schemaObject) &&
115-
schemaTypesSource.namespace
116-
) {
117-
const schemaTypesImportStatement = generateImportStatement({
118-
baseDir,
119-
emitLegacyCommonJSImports: presetOptions.config.emitLegacyCommonJSImports,
120-
importExtension: presetOptions.config.importExtension,
121-
importSource: resolveImportSource(schemaTypesSource),
122-
baseOutputDir,
123-
outputPath: generatedFilePath,
124-
typesImport,
125-
});
126-
importStatements.unshift(schemaTypesImportStatement);
127-
}
115+
if (
116+
isUsingTypes(externalFragmentsInjectedDocument, [], schemaObject) &&
117+
schemaTypesSource.namespace
118+
) {
119+
const schemaTypesImportStatement = generateImportStatement({
120+
baseDir,
121+
emitLegacyCommonJSImports: presetOptions.config.emitLegacyCommonJSImports,
122+
importExtension: presetOptions.config.importExtension,
123+
importSource: resolveImportSource(schemaTypesSource),
124+
baseOutputDir,
125+
outputPath: generatedFilePath,
126+
typesImport,
127+
});
128+
importStatements.unshift(schemaTypesImportStatement);
129+
}
128130

129-
return {
130-
filename: generatedFilePath,
131-
documents: [documentFile],
132-
importStatements,
133-
fragmentImports,
134-
externalFragments,
135-
};
136-
} catch (e) {
137-
throw new Error(
138-
`Unable to validate GraphQL document! \n
131+
return {
132+
filename: generatedFilePath,
133+
documents: [documentFile],
134+
importStatements,
135+
fragmentImports,
136+
externalFragments,
137+
};
138+
} catch (e) {
139+
throw new Error(
140+
`Unable to validate GraphQL document! \n
139141
File ${documentFile.location} caused error:
140142
${e.message || e.toString()}`,
141-
);
142-
}
143-
});
143+
);
144+
}
145+
});
144146
}

packages/presets/near-operation-file/tests/near-operation-file.spec.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1521,6 +1521,39 @@ describe('near-operation-file preset', () => {
15211521
"
15221522
`);
15231523
});
1524+
it('should not generate output for documents marked as external', async () => {
1525+
const result = await executePreset({
1526+
baseOutputDir: '/some/path',
1527+
config: {},
1528+
presetConfig: {
1529+
baseTypesPath: 'types.ts',
1530+
},
1531+
schema: schemaDocumentNode,
1532+
schemaAst: schemaNode,
1533+
documents: [
1534+
{
1535+
location: '/some/deep/path/src/graphql/me-query.graphql',
1536+
document: operationAst,
1537+
type: 'standard',
1538+
},
1539+
{
1540+
location: '/some/deep/path/src/graphql/external-query.graphql',
1541+
document: operationAst,
1542+
type: 'external',
1543+
},
1544+
{
1545+
location: '/some/deep/path/src/graphql/user-fragment.graphql',
1546+
document: fragmentAst,
1547+
type: 'external',
1548+
},
1549+
],
1550+
plugins: [{ 'typescript-operations': {} }],
1551+
pluginMap: { 'typescript-operations': {} as any },
1552+
});
1553+
1554+
expect(result).toHaveLength(1);
1555+
expect(result[0].filename).toContain('me-query.generated.ts');
1556+
});
15241557
});
15251558

15261559
const getFragmentImportsFromResult = (result: Types.GenerateOptions[], index = 0) =>

0 commit comments

Comments
 (0)