Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't add duplicate field names #323

Merged
merged 2 commits into from
Jun 28, 2016
Merged
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Expect active development and potentially significant breaking changes in the `0

- Move out GraphQL query parsing into a new package [`graphql-tag`](https://github.com/apollostack/graphql-tag) with a backcompat shim for `apollo-client/gql`. [Issue #312](https://github.com/apollostack/apollo-client/issues/312) [PR #313](https://github.com/apollostack/apollo-client/pull/313)
- Added `ssrMode` (to disable `forceFetch` queries completely) and `ssrForceFetchDelay` (to disable it for a short time period). This is for server-side rendering -- on the server it doesn't make sense to force fetch (you just want a single snapshot of data, not changing data), and when you first re-render on the client, the server's data is up to date, so there's no need to re-fetch. [Issue #298](https://github.com/apollostack/apollo-client/issues/298) [PR #309](https://github.com/apollostack/apollo-client/pull/309)
- `addTypename` query transform now doesn't add extra `__typename` fields where they are already present. [PR #323](https://github.com/apollostack/apollo-client/pull/323)

### v0.3.20

Expand Down
36 changes: 22 additions & 14 deletions src/queries/queryTransform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@ import {
import cloneDeep = require('lodash.clonedeep');

// A QueryTransformer takes a SelectionSet and transforms it in someway (in place).
export type QueryTransformer = (queryPiece: SelectionSet) => void
export type QueryTransformer = (selectionSet: SelectionSet) => void

// Adds a field with a given name to every node in the AST recursively.
// Note: this mutates the AST passed in.
export function addFieldToSelectionSet(fieldName: string, queryPiece: SelectionSet) {
if (queryPiece == null || queryPiece.selections == null) {
return queryPiece;
export function addFieldToSelectionSet(fieldName: string, selectionSet: SelectionSet) {
if (selectionSet == null || selectionSet.selections == null) {
return selectionSet;
}

const typenameFieldAST: Field = {
const fieldAst: Field = {
kind: 'Field',
alias: null,
name: {
Expand All @@ -26,23 +26,31 @@ export function addFieldToSelectionSet(fieldName: string, queryPiece: SelectionS
},
};

queryPiece.selections.map((child) => {
// We use type assertions to make sure the child isn't a FragmentSpread because
let alreadyHasThisField = false;
selectionSet.selections.map((selection) => {
// We use type assertions to make sure the selection isn't a FragmentSpread because
// that can't have a selectionSet.
if (child.kind === 'Field' || child.kind === 'InlineFragment') {
addTypenameToSelectionSet((child as (Field | InlineFragment)).selectionSet);
if (selection.kind === 'Field' || selection.kind === 'InlineFragment') {
addTypenameToSelectionSet((selection as (Field | InlineFragment)).selectionSet);
}

if (selection.kind === 'Field' && (selection as Field).name.value === fieldName) {
alreadyHasThisField = true;
}
});

// Add the typename to this particular node's children
queryPiece.selections.push(typenameFieldAST);
return queryPiece;
if (! alreadyHasThisField) {
// Add the typename to this particular node's children
selectionSet.selections.push(fieldAst);
}

return selectionSet;
}

// Adds typename fields to every node in the AST recursively.
// Note: This muates the AST passed in.
export function addTypenameToSelectionSet(queryPiece: SelectionSet) {
return addFieldToSelectionSet('__typename', queryPiece);
export function addTypenameToSelectionSet(selectionSet: SelectionSet) {
return addFieldToSelectionSet('__typename', selectionSet);
}

// Add typename field to the root query node (i.e. OperationDefinition). Returns a new
Expand Down
42 changes: 40 additions & 2 deletions test/queryTransform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ describe('query transforms', () => {
}
}
}
`;
`;
const queryDef = getQueryDefinition(testQuery);
const queryRes = addTypenameToSelectionSet(queryDef.selectionSet);

Expand All @@ -39,7 +39,45 @@ describe('query transforms', () => {
}
__typename
}
__typename}`);
__typename
}
`);
const expectedQueryStr = print(expectedQuery);

assert.equal(expectedQueryStr, modifiedQueryStr);
});

it('should not add duplicates', () => {
let testQuery = gql`
query {
author {
name {
firstName
lastName
__typename
}
}
}
`;
const queryDef = getQueryDefinition(testQuery);
const queryRes = addTypenameToSelectionSet(queryDef.selectionSet);

// GraphQL print the parsed, updated query and replace the trailing
// newlines.
const modifiedQueryStr = print(queryRes);
const expectedQuery = getQueryDefinition(gql`
query {
author {
name {
firstName
lastName
__typename
}
__typename
}
__typename
}
`);
const expectedQueryStr = print(expectedQuery);

assert.equal(expectedQueryStr, modifiedQueryStr);
Expand Down