-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #72 from Quramy/remove-duplicated-fragments
Remove duplicated fragments
- Loading branch information
Showing
18 changed files
with
362 additions
and
10 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
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
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
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
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,11 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`detectDuplicatedFragments should detect duplicated fragments info 1`] = ` | ||
Array [ | ||
Object { | ||
"end": 149, | ||
"name": "Hoge", | ||
"start": 106, | ||
}, | ||
] | ||
`; |
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,31 @@ | ||
import { detectDuplicatedFragments } from './'; | ||
import { parse } from 'graphql'; | ||
|
||
describe(detectDuplicatedFragments, () => { | ||
it('should detect duplicated fragments info', () => { | ||
const documentContent = ` | ||
fragment Hoge on Query { | ||
id | ||
} | ||
fragment Foo on Query { | ||
id | ||
} | ||
fragment Hoge on Query { | ||
id | ||
} | ||
`; | ||
expect(detectDuplicatedFragments(parse(documentContent))).toMatchSnapshot(); | ||
}); | ||
|
||
it('should return empty array when no duplication', () => { | ||
const documentContent = ` | ||
fragment Hoge on Query { | ||
id | ||
} | ||
fragment Foo on Query { | ||
id | ||
} | ||
`; | ||
expect(detectDuplicatedFragments(parse(documentContent))).toStrictEqual([]); | ||
}); | ||
}); |
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,22 @@ | ||
import { DocumentNode, FragmentDefinitionNode } from 'graphql'; | ||
|
||
export function detectDuplicatedFragments(documentNode: DocumentNode) { | ||
const fragments: FragmentDefinitionNode[] = []; | ||
const duplicatedFragments: FragmentDefinitionNode[] = []; | ||
documentNode.definitions.forEach(def => { | ||
if (def.kind === 'FragmentDefinition') { | ||
if (fragments.some(f => f.name.value === def.name.value)) { | ||
duplicatedFragments.push(def); | ||
} else { | ||
fragments.push(def); | ||
} | ||
} | ||
}); | ||
return duplicatedFragments.map(def => { | ||
return { | ||
name: def.name.value, | ||
start: def.loc!.start, | ||
end: def.loc!.end, | ||
}; | ||
}); | ||
} |
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
Oops, something went wrong.