Skip to content

Commit

Permalink
Simplify getDirectiveNames and remove flattenSelections helper.
Browse files Browse the repository at this point in the history
Though it's possible that someone might have been using flattenSelections
directly, the function signature seems awfully specific to the needs of
the former implementation of getDirectiveNames.
  • Loading branch information
benjamn committed Jan 17, 2019
1 parent b4f0c8e commit 8c85dc9
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 39 deletions.
20 changes: 20 additions & 0 deletions CHANGELOG.md
Expand Up @@ -8,6 +8,26 @@
`graphql/language/printer`. If you need this functionality, import it
directly: `import { print } from "graphql/language/printer"`

### Apollo Cache In-Memory (vNext)

- The `flattenSelections` helper function is no longer exported from
`apollo-utilities`, since `getDirectiveNames` has been reimplemented
without using `flattenSelections`, and `flattenSelections` has no clear
purpose now. If you need the old functionality, use a visitor:
```ts
import { visit } from "graphql/language/visitor";

function flattenSelections(selection: SelectionNode) {
const selections: SelectionNode[] = [];
visit(selection, {
SelectionSet(ss) {
selections.push(...ss.selections);
}
});
return selections;
}
```

## Apollo Client (2.4.9)

### Apollo Client (2.4.9)
Expand Down
50 changes: 11 additions & 39 deletions packages/apollo-utilities/src/directives.ts
Expand Up @@ -2,14 +2,15 @@
// the `skip` and `include` directives within GraphQL.
import {
FieldNode,
OperationDefinitionNode,
SelectionNode,
VariableNode,
BooleanValueNode,
DirectiveNode,
DocumentNode,
} from 'graphql';

import { visit } from 'graphql/language/visitor';

import { argumentsObjectFromField } from './storeUtils';

export type DirectiveInfo = {
Expand Down Expand Up @@ -95,45 +96,16 @@ export function shouldInclude(
return res;
}

export function flattenSelections(selection: SelectionNode): SelectionNode[] {
if (
!(selection as FieldNode).selectionSet ||
!((selection as FieldNode).selectionSet.selections.length > 0)
)
return [selection];

return [selection].concat(
(selection as FieldNode).selectionSet.selections
.map(selectionNode =>
[selectionNode].concat(flattenSelections(selectionNode)),
)
.reduce((selections, selected) => selections.concat(selected), []),
);
}

export function getDirectiveNames(doc: DocumentNode) {
// operation => [names of directives];
const directiveNames = doc.definitions
.filter(
(definition: OperationDefinitionNode) =>
definition.selectionSet && definition.selectionSet.selections,
)
// operation => [[Selection]]
.map(x => flattenSelections(x as any))
// [[Selection]] => [Selection]
.reduce((selections, selected) => selections.concat(selected), [])
// [Selection] => [Selection with Directives]
.filter(
(selection: SelectionNode) =>
selection.directives && selection.directives.length > 0,
)
// [Selection with Directives] => [[Directives]]
.map((selection: SelectionNode) => selection.directives)
// [[Directives]] => [Directives]
.reduce((directives, directive) => directives.concat(directive), [])
// [Directives] => [Name]
.map((directive: DirectiveNode) => directive.name.value);
return directiveNames;
const names: string[] = [];

visit(doc, {
Directive(node) {
names.push(node.name.value);
},
});

return names;
}

export function hasDirectives(names: string[], doc: DocumentNode) {
Expand Down

0 comments on commit 8c85dc9

Please sign in to comment.