Skip to content

Commit

Permalink
fix: adjusted algorithm to support nested fragments, fixes #5
Browse files Browse the repository at this point in the history
  • Loading branch information
BowlingX authored May 3, 2022
1 parent e6fcda9 commit 71928e5
Show file tree
Hide file tree
Showing 4 changed files with 266 additions and 162 deletions.
6 changes: 3 additions & 3 deletions src/apollo/KeepLink.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
} from '@apollo/client'
import { argumentsObjectFromField } from 'apollo-utilities'
import {
findOriginalPaths,
findNodePaths,
removeDirectivesFromDocument,
setInObject,
} from './utils'
Expand All @@ -25,7 +25,7 @@ export function removeIgnoreSetsFromDocument<T extends DocumentNode>(
variables: Record<string, any>,
enabledFeatures: Array<string>
): { modifiedDoc: T; nullFields: Array<Array<string | number>> } {
const { modifiedDoc, pathsToRemove } = removeDirectivesFromDocument(
const { modifiedDoc, nodesToRemove } = removeDirectivesFromDocument(
[
{
// if the directive should be removed
Expand Down Expand Up @@ -57,7 +57,7 @@ export function removeIgnoreSetsFromDocument<T extends DocumentNode>(
)

// The field path's where we have set null later
const nullFields = findOriginalPaths(pathsToRemove, document)
const nullFields = findNodePaths(nodesToRemove, document)

return { modifiedDoc: modifiedDoc as T, nullFields }
}
Expand Down
102 changes: 101 additions & 1 deletion src/apollo/__tests__/KeepLink.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ describe('KeepLink with if', () => {
}
`

it('should remove argument variables if exists', () => {
it('should remove query argument variables if exists', () => {
const { modifiedDoc } = removeIgnoreSetsFromDocument(
queryWithArgument,
{
Expand All @@ -73,6 +73,7 @@ describe('KeepLink with if', () => {
)
expect(print(modifiedDoc)).toMatchSnapshot()
})

it('should keep variables if they are used elsewhere', () => {
const { modifiedDoc } = removeIgnoreSetsFromDocument(
queryWithArgumentAndUsedElsewhere,
Expand Down Expand Up @@ -593,3 +594,102 @@ describe('utilities', () => {
})
})
})

describe('fragments', () => {
it('should remove unused fragments', () => {
const queryWithFragments = gql`
fragment myFragment on SomeType {
field @keep(ifFeature: "someFeature")
}
query myQuery {
field {
someOtherKey
...myFragment
}
}
`
const { nullFields, modifiedDoc } = removeIgnoreSetsFromDocument(
queryWithFragments,
{},
[]
)
expect(nullFields).toEqual([['field', 'field']])
expect(print(modifiedDoc)).toEqual(
print(gql`
query myQuery {
field {
someOtherKey
}
}
`)
)
})
})

describe('complex nested mutations and fragments', () => {
const complexNested = gql`
fragment level2 on OtherType {
nice
to
have @keep(ifFeature: "someFeature")
some @keep(ifFeature: "someFeature") {
subselection
}
}
fragment level1 on MyType {
label
oneOnLevel1 @keep(ifFeature: "someFeature")
goesToLevel2 {
...level2
}
}
mutation complexWithNestedFragments {
complexWithNestedFragments {
nested {
firstLevelKey @keep(ifFeature: "someFeature")
...level1
}
}
}
`

const { nullFields, modifiedDoc } = removeIgnoreSetsFromDocument(
complexNested,
{},
[]
)
expect(nullFields).toEqual([
['complexWithNestedFragments', 'nested', 'goesToLevel2', 'some'],
['complexWithNestedFragments', 'nested', 'goesToLevel2', 'have'],
['complexWithNestedFragments', 'nested', 'oneOnLevel1'],
['complexWithNestedFragments', 'nested', 'firstLevelKey'],
])
expect(print(modifiedDoc)).toMatchSnapshot()
})
describe('handle inline fragments', () => {
const inlineFragment = gql`
mutation complexWithNestedFragments {
complexWithNestedFragments {
nested {
... on SomeType {
firstLevelKey @keep(ifFeature: "someFeature")
primaryFunction
... on SomeOtherType {
deeper @keep(ifFeature: "someFeature")
}
}
}
}
}
`
const { nullFields, modifiedDoc } = removeIgnoreSetsFromDocument(
inlineFragment,
{},
[]
)
expect(nullFields).toEqual([
['complexWithNestedFragments', 'nested', 'deeper'],
['complexWithNestedFragments', 'nested', 'firstLevelKey'],
])
expect(print(modifiedDoc)).toMatchSnapshot()
})
74 changes: 43 additions & 31 deletions src/apollo/__tests__/__snapshots__/KeepLink.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,48 @@ exports[` 3`] = `
"
`;

exports[`KeepLink with if Aliases should properly map aliases 1`] = `
"query someQuery {
aliasForDeeply: deeply {
exports[` 4`] = `
"fragment level2 on OtherType {
nice
to
}
fragment level1 on MyType {
label
goesToLevel2 {
...level2
}
}
mutation complexWithNestedFragments {
complexWithNestedFragments {
nested {
...someOtherIfFragment
...level1
}
}
}
"
`;

fragment someOtherIfFragment on Type
exports[` 5`] = `
"mutation complexWithNestedFragments {
complexWithNestedFragments {
nested {
... on SomeType {
primaryFunction
}
}
}
}
"
`;

exports[`KeepLink with if Aliases should properly map aliases 1`] = `
"query someQuery {
aliasForDeeply: deeply {
nested
}
}
"
`;

Expand All @@ -63,7 +95,7 @@ exports[`KeepLink with if Field Arguments should keep variables if they are used
"
`;

exports[`KeepLink with if Field Arguments should remove argument variables if exists 1`] = `
exports[`KeepLink with if Field Arguments should remove query argument variables if exists 1`] = `
"query someQuery {
someOther {
subFieldOther
Expand All @@ -75,26 +107,18 @@ exports[`KeepLink with if Field Arguments should remove argument variables if ex
exports[`KeepLink with if Fragment should work with fragments 1`] = `
"query someQuery {
deeply {
nested {
...ifQueryFragment
}
nested
}
}
fragment ifQueryFragment on Type
"
`;

exports[`KeepLink with if and ifFeature Aliases should properly map aliases 1`] = `
"query someQuery {
aliasForDeeply: deeply {
nested {
...someOtherIfAndIfFeatureFragment
}
nested
}
}
fragment someOtherIfAndIfFeatureFragment on Type
"
`;

Expand All @@ -121,13 +145,9 @@ exports[`KeepLink with if and ifFeature Field Arguments should remove argument v
exports[`KeepLink with if and ifFeature Fragment should work with fragments 1`] = `
"query someQuery {
deeply {
nested {
...ifAndIfFeatureQueryFragment
}
nested
}
}
fragment ifAndIfFeatureQueryFragment on Type
"
`;

Expand Down Expand Up @@ -178,13 +198,9 @@ exports[`KeepLink with if should remove directives and fields if shouldKeep is f
exports[`KeepLink with ifFeature Aliases should properly map aliases 1`] = `
"query someQuery {
aliasForDeeply: deeply {
nested {
...someOtherIfFeatureFragment
}
nested
}
}
fragment someOtherIfFeatureFragment on Type
"
`;

Expand All @@ -211,13 +227,9 @@ exports[`KeepLink with ifFeature Field Arguments should remove argument variable
exports[`KeepLink with ifFeature Fragment should work with fragments 1`] = `
"query someQuery {
deeply {
nested {
...ifFeatureQueryFragment
}
nested
}
}
fragment ifFeatureQueryFragment on Type
"
`;

Expand Down
Loading

0 comments on commit 71928e5

Please sign in to comment.