-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce
dedupeFragments
config flag for dealing with duplicate fr…
…agments (#6018) Co-authored-by: Dotan Simha <dotansimha@gmail.com>
- Loading branch information
1 parent
ecd205a
commit cf1e5ab
Showing
4 changed files
with
181 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
'@graphql-codegen/visitor-plugin-common': patch | ||
'@graphql-codegen/typed-document-node': patch | ||
--- | ||
|
||
Introduce new feature for removing duplicated fragments |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
162 changes: 161 additions & 1 deletion
162
packages/plugins/typescript/typed-document-node/tests/typed-document-node.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,170 @@ | ||
import { Types } from '@graphql-codegen/plugin-helpers'; | ||
import { buildSchema, parse } from 'graphql'; | ||
import { plugin } from '../src'; | ||
|
||
describe('TypeDocumentNode', () => { | ||
describe('TypedDocumentNode', () => { | ||
it('Should not output imports when there are no operations at all', async () => { | ||
const result = (await plugin(null as any, [], {})) as Types.ComplexPluginOutput; | ||
expect(result.content).toBe(''); | ||
expect(result.prepend.length).toBe(0); | ||
}); | ||
|
||
it('Duplicated nested fragments handle (dedupeFragments=true)', async () => { | ||
const schema = buildSchema(/* GraphQL */ ` | ||
schema { | ||
query: Query | ||
} | ||
type Query { | ||
jobs: [Job!]! | ||
} | ||
type Job { | ||
id: ID! | ||
recruiterName: String! | ||
title: String! | ||
} | ||
`); | ||
|
||
const ast = parse(/* GraphQL */ ` | ||
query GetJobs { | ||
jobs { | ||
...DataForPageA | ||
...DataForPageB | ||
...JobSimpleRecruiterData | ||
} | ||
} | ||
fragment DataForPageA on Job { | ||
id | ||
...JobSimpleRecruiterData | ||
} | ||
fragment DataForPageB on Job { | ||
title | ||
...JobSimpleRecruiterData | ||
} | ||
fragment JobSimpleRecruiterData on Job { | ||
recruiterName | ||
} | ||
`); | ||
|
||
const res = (await plugin( | ||
schema, | ||
[{ location: '', document: ast }], | ||
{ dedupeFragments: true }, | ||
{ outputFile: '' } | ||
)) as Types.ComplexPluginOutput; | ||
|
||
expect((res.content.match(/JobSimpleRecruiterDataFragmentDoc.definitions/g) || []).length).toBe(1); | ||
}); | ||
|
||
it('Check with nested and recursive fragments handle (dedupeFragments=true)', async () => { | ||
const schema = buildSchema(/* GraphQL */ ` | ||
type Query { | ||
test: MyType | ||
nested: MyOtherType | ||
} | ||
type MyOtherType { | ||
myType: MyType! | ||
myOtherTypeRecursive: MyOtherType! | ||
} | ||
type MyType { | ||
foo: String! | ||
} | ||
`); | ||
|
||
const ast = parse(/* GraphQL */ ` | ||
query test { | ||
test { | ||
...MyTypeFields | ||
nested { | ||
myOtherTypeRecursive { | ||
myType { | ||
...MyTypeFields | ||
} | ||
myOtherTypeRecursive { | ||
...MyOtherTypeRecursiveFields | ||
} | ||
} | ||
myType { | ||
...MyTypeFields | ||
} | ||
} | ||
} | ||
} | ||
fragment MyOtherTypeRecursiveFields on MyOtherType { | ||
myType { | ||
...MyTypeFields | ||
} | ||
} | ||
fragment MyTypeFields on MyType { | ||
foo | ||
} | ||
`); | ||
|
||
const res = (await plugin( | ||
schema, | ||
[{ location: '', document: ast }], | ||
{ dedupeFragments: true }, | ||
{ outputFile: '' } | ||
)) as Types.ComplexPluginOutput; | ||
|
||
expect((res.content.match(/MyTypeFieldsFragmentDoc.definitions/g) || []).length).toBe(1); | ||
}); | ||
|
||
it('Ignore duplicated nested fragments handle (dedupeFragments=false)', async () => { | ||
const schema = buildSchema(/* GraphQL */ ` | ||
schema { | ||
query: Query | ||
} | ||
type Query { | ||
jobs: [Job!]! | ||
} | ||
type Job { | ||
id: ID! | ||
recruiterName: String! | ||
title: String! | ||
} | ||
`); | ||
|
||
const ast = parse(/* GraphQL */ ` | ||
query GetJobs { | ||
jobs { | ||
...DataForPageA | ||
...DataForPageB | ||
} | ||
} | ||
fragment DataForPageA on Job { | ||
id | ||
...JobSimpleRecruiterData | ||
} | ||
fragment DataForPageB on Job { | ||
title | ||
...JobSimpleRecruiterData | ||
} | ||
fragment JobSimpleRecruiterData on Job { | ||
recruiterName | ||
} | ||
`); | ||
|
||
const res = (await plugin( | ||
schema, | ||
[{ location: '', document: ast }], | ||
{ dedupeFragments: false }, | ||
{ outputFile: '' } | ||
)) as Types.ComplexPluginOutput; | ||
|
||
expect((res.content.match(/JobSimpleRecruiterDataFragmentDoc.definitions/g) || []).length).toBe(2); | ||
}); | ||
}); |