Skip to content
This repository has been archived by the owner on Aug 20, 2020. It is now read-only.

Remove @connection directives #61

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/ExtractGQL.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import {
} from './common';

import {
addTypenameTransformer,
addTypenameTransformer, removeConnectionDirectivesTransformer,
} from './queryTransformers';

import _ = require('lodash');
Expand Down Expand Up @@ -355,6 +355,8 @@ export const main = (argv: YArgsv) => {
console.log('Using the add-typename query transformer.');
queryTransformers.push(addTypenameTransformer);
}
// To be consistent with apollo-client, remove @connection directives
queryTransformers.push(removeConnectionDirectivesTransformer);

const options: ExtractGQLOptions = {
inputFilePath,
Expand Down
35 changes: 35 additions & 0 deletions src/queryTransformers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import {
isFragmentDefinition,
} from './extractFromAST';

import _ = require('lodash');

// TODO Most of this implementation has been copped from here:
// https://github.com/apollostack/apollo-client/blob/master/src/queries/queryTransform.ts
//
Expand Down Expand Up @@ -67,3 +69,36 @@ export const addTypenameTransformer: QueryTransformer = (doc: DocumentNode) => {

return docClone;
};

function removeConnectionDirectivesFromSelectionSet(
selectionSet: SelectionSetNode,
) {
if (selectionSet.selections) {
selectionSet.selections.forEach(selection => {
if (selection.kind === 'Field') {
if (selection.directives) {
selection.directives = selection.directives.filter(
directive => _.get(directive, 'name.value') !== 'connection'
);
}
if (selection.selectionSet) {
removeConnectionDirectivesFromSelectionSet(selection.selectionSet);
}
}
});
}
}

export const removeConnectionDirectivesTransformer: QueryTransformer = (
doc: DocumentNode,
) => {
const docClone = JSON.parse(JSON.stringify(doc));

docClone.definitions.forEach((definition: DefinitionNode) => {
removeConnectionDirectivesFromSelectionSet(
(definition as OperationDefinitionNode).selectionSet
);
});

return docClone;
};
65 changes: 65 additions & 0 deletions test/queryTransformers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {

import {
addTypenameTransformer,
removeConnectionDirectivesTransformer,
} from '../src/queryTransformers';

describe('query transformers', () => {
Expand Down Expand Up @@ -85,4 +86,68 @@ describe('query transformers', () => {

});
});

describe('typename query transformer', () => {
const assertTransform = (inputQuery: DocumentNode, expected: DocumentNode) => {
assert.equal(
print(removeConnectionDirectivesTransformer(inputQuery)),
print(expected),
);
};

it('should remove @connection from all the levels of a simple query', () => {
assertTransform(
gql`
query author {
firstName @connection(key: "nickame")
lastName @connection(key: "surname")
}`,
gql`
query author {
firstName
lastName
}`,
);
});

it('should remove @connection from all the levels of a nested query', () => {
assertTransform(
gql`
query author {
firstName @connection(key: "nickame") {
suffix @connection(key: "somethingLikeJr") {
character @connection(key: "c")
symbol @connection(key: "s")
}
}
lastName @connection(key: "surname")
}`,
gql`
query author {
firstName {
suffix {
character
symbol
}
}
lastName
}`,
);
});

it('should remove @connection while ignoring other directives', () => {
assertTransform(
gql`
query author {
firstName @connection(key: "nickame")
lastName @format(key: "surname")
}`,
gql`
query author {
firstName
lastName @format(key: "surname")
}`,
);
});
});
});