Skip to content

Commit

Permalink
6245 further edge cases fix (#6316)
Browse files Browse the repository at this point in the history
  • Loading branch information
gilgardosh committed Jul 22, 2021
1 parent f0277a8 commit 841426e
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 12 deletions.
5 changes: 5 additions & 0 deletions .changeset/violet-pens-juggle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@graphql-codegen/near-operation-file-preset': patch
---

Further fragments import on dedupeFragments edge cases fix
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,7 @@ export default function buildFragmentResolver<T>(
if (
level === 0 ||
(dedupeFragments &&
documentFileContent.definitions[0].kind === 'OperationDefinition' &&
documentFileContent.definitions[0].operation === 'query')
['OperationDefinition', 'FragmentDefinition'].includes(documentFileContent.definitions[0].kind))
) {
if (fragmentFileImports[fragmentDetails.filePath] === undefined) {
fragmentFileImports[fragmentDetails.filePath] = fragmentDetails.imports;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1028,9 +1028,82 @@ describe('near-operation-file preset', () => {
`import { AnotherGroupFragmentFragmentDoc, AnotherGroupFragmentFragment } from './fragments/AnotherGroupFragment';`
);
});

it('Should import relevant nested fragments on dedupeFragments', async () => {
const schema = parse(/* GraphQL */ `
type Address {
city: String
}
type Author {
address: Address
}
type Book {
author: Author
}
type Query {
book: Book
}
`);

const operations = [
{
location: '/author.graphql',
document: parse(/* GraphQL */ `
fragment Address on Address {
city
}
fragment Author on Author {
address {
...Address
}
}
`),
},
{
location: '/book.graphql',
document: parse(/* GraphQL */ `
fragment Book on Book {
author {
...Author
}
}
query Book {
book {
...Book
}
}
`),
},
];

const result = await preset.buildGeneratesSection({
baseOutputDir: './src/',
config: {
dedupeFragments: true,
},
presetConfig: {
extension: '.ts',
baseTypesPath: './types',
},
schema,
schemaAst: buildASTSchema(schema),
documents: operations,
plugins: [{ typescript: {} }, { 'typescript-operations': {} }, { 'typed-document-node': {} }],
pluginMap: { typescript: {} as any, 'typescript-operations': {} as any, 'typed-document-node': {} as any },
});

expect(getFragmentImportsFromResult(result, 1)).toContain(
`import { AuthorFragmentDoc, AuthorFragment, AddressFragmentDoc, AddressFragment } from './author';`
);
});
});

const getFragmentImportsFromResult = (result: Types.GenerateOptions[]) =>
result[0].config.fragmentImports
const getFragmentImportsFromResult = (result: Types.GenerateOptions[], index = 0) =>
result[index].config.fragmentImports
.map(importStatement => generateFragmentImportStatement(importStatement, 'both'))
.join('\n');
16 changes: 8 additions & 8 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3186,7 +3186,12 @@
dependencies:
"@types/node" "*"

"@types/node@*", "@types/node@14.17.5", "@types/node@^14.14.33":
"@types/node@*", "@types/node@^15.0.1":
version "15.6.0"
resolved "https://registry.yarnpkg.com/@types/node/-/node-15.6.0.tgz#f0ddca5a61e52627c9dcb771a6039d44694597bc"
integrity sha512-gCYSfQpy+LYhOFTKAeE8BkyGqaxmlFxe+n4DKM6DR0wzw/HISUE/hAmkC/KT8Sw5PCJblqg062b3z9gucv3k0A==

"@types/node@14.17.5", "@types/node@^14.14.33":
version "14.17.5"
resolved "https://registry.yarnpkg.com/@types/node/-/node-14.17.5.tgz#b59daf6a7ffa461b5648456ca59050ba8e40ed54"
integrity sha512-bjqH2cX/O33jXT/UmReo2pM7DIJREPMnarixbQ57DOOzzFaI6D2+IcwaJQaJpv0M1E9TIhPCYVxrkcityLjlqA==
Expand All @@ -3201,11 +3206,6 @@
resolved "https://registry.npmjs.org/@types/node/-/node-12.12.55.tgz#0aa266441cb9e1fd3e415a8f619cb7d776667cdd"
integrity sha512-Vd6xQUVvPCTm7Nx1N7XHcpX6t047ltm7TgcsOr4gFHjeYgwZevo+V7I1lfzHnj5BT5frztZ42+RTG4MwYw63dw==

"@types/node@^15.0.1":
version "15.6.0"
resolved "https://registry.yarnpkg.com/@types/node/-/node-15.6.0.tgz#f0ddca5a61e52627c9dcb771a6039d44694597bc"
integrity sha512-gCYSfQpy+LYhOFTKAeE8BkyGqaxmlFxe+n4DKM6DR0wzw/HISUE/hAmkC/KT8Sw5PCJblqg062b3z9gucv3k0A==

"@types/normalize-package-data@^2.4.0":
version "2.4.0"
resolved "https://registry.npmjs.org/@types/normalize-package-data/-/normalize-package-data-2.4.0.tgz#e486d0d97396d79beedd0a6e33f4534ff6b4973e"
Expand Down Expand Up @@ -13363,12 +13363,12 @@ rxjs@^6.3.3, rxjs@^6.4.0, rxjs@^6.5.1, rxjs@^6.6.0, rxjs@^6.6.3, rxjs@^6.6.7:
dependencies:
tslib "^1.9.0"

safe-buffer@5.1.2, safe-buffer@>=5.1.0, safe-buffer@^5.0.1, safe-buffer@^5.1.0, safe-buffer@^5.1.1, safe-buffer@^5.1.2, safe-buffer@~5.1.0, safe-buffer@~5.1.1:
safe-buffer@5.1.2, safe-buffer@~5.1.0, safe-buffer@~5.1.1:
version "5.1.2"
resolved "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.1.2.tgz#991ec69d296e0313747d59bdfd2b745c35f8828d"
integrity sha512-Gd2UZBJDkXlY7GbJxfsE8/nvKkUEU1G38c1siN6QP6a9PT9MmHB8GnpscSmMJSoF8LOIrt8ud/wPtojys4G6+g==

safe-buffer@^5.2.0, safe-buffer@~5.2.0:
safe-buffer@>=5.1.0, safe-buffer@^5.0.1, safe-buffer@^5.1.0, safe-buffer@^5.1.1, safe-buffer@^5.1.2, safe-buffer@^5.2.0, safe-buffer@~5.2.0:
version "5.2.1"
resolved "https://registry.yarnpkg.com/safe-buffer/-/safe-buffer-5.2.1.tgz#1eaf9fa9bdb1fdd4ec75f58f9cdb4e6b7827eec6"
integrity sha512-rp3So07KcdmmKbGvgaNxQSJr7bGVSVk5S9Eq1F+ppbRo70+YeaDxkw5Dd8NPN+GD6bjnYm2VuPuCXmpuYvmCXQ==
Expand Down

0 comments on commit 841426e

Please sign in to comment.