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 2, 2019
1 parent 068b076 commit fb539c5
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 39 deletions.
18 changes: 18 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,24 @@
GraphQL AST's are no longer being directly modified. <br/>
[@hwillson](https://github.com/hwillson) in [#4233](https://github.com/apollographql/apollo-client/pull/4233)

- 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 Cache In-Memory (vNext)

- Export the optimism `wrap` function using ES2015 export syntax, instead of
Expand Down
50 changes: 11 additions & 39 deletions packages/apollo-utilities/src/directives.ts
Original file line number Diff line number Diff line change
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 fb539c5

Please sign in to comment.