Skip to content

Commit

Permalink
added test for FragmentSpread when adding typename fields to query
Browse files Browse the repository at this point in the history
  • Loading branch information
Poincare committed May 20, 2016
1 parent a5495a0 commit 2709b7b
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 23 deletions.
2 changes: 2 additions & 0 deletions src/QueryManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
Request,
} from './networkInterface';


import forOwn = require('lodash.forown');
import assign = require('lodash.assign');

Expand Down Expand Up @@ -48,6 +49,7 @@ export class ObservableQuery extends Observable<GraphQLResult> {
return super.subscribe(observer) as QuerySubscription;
}


public result(): Promise<GraphQLResult> {
return new Promise((resolve, reject) => {
const subscription = this.subscribe({
Expand Down
16 changes: 7 additions & 9 deletions src/queries/queryTransform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import cloneDeep = require('lodash.clonedeep');
// Adds typename fields to every node in the AST recursively. Returns a copy of the entire
// AST with the typename fields added.
// Note: This muates the AST passed in.
export function addTypenameFields(queryPiece: SelectionSet) {
export function addTypenameToSelectionSet(queryPiece: SelectionSet) {
if (queryPiece == null || queryPiece.selections == null) {
return queryPiece;
}
Expand All @@ -24,11 +24,11 @@ export function addTypenameFields(queryPiece: SelectionSet) {
},
};

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

Expand All @@ -39,10 +39,8 @@ export function addTypenameFields(queryPiece: SelectionSet) {

// Add typename field to the root query node (i.e. OperationDefinition). Returns a new
// query tree.
export function addTypenameToRoot(queryDef: OperationDefinition): OperationDefinition {
let queryClone = cloneDeep(queryDef);
this.addTypenameFields(queryClone.selectionSet);
export function addTypenameToQuery(queryDef: OperationDefinition): OperationDefinition {
const queryClone = cloneDeep(queryDef);
this.addTypenameToSelectionSet(queryClone.selectionSet);
return queryClone;
}


54 changes: 40 additions & 14 deletions test/queryTransform.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {
addTypenameFields,
addTypenameToRoot,
addTypenameToSelectionSet,
addTypenameToQuery,
} from '../src/queries/queryTransform';
import { getQueryDefinition } from '../src/queries/getFromAST';
import { print } from 'graphql/language/printer';
Expand All @@ -17,14 +17,15 @@ describe('query transforms', () => {
lastName
}
}
}`;
}
`;
const queryDef = getQueryDefinition(testQuery);
const queryRes = addTypenameFields(queryDef.selectionSet);
const queryRes = addTypenameToSelectionSet(queryDef.selectionSet);

// GraphQL print the parsed, updated query and replace the trailing
// newlines.
const modifiedQueryStr = print(queryRes).replace(/\n$/, '');
const expectedQuery = gql`
const modifiedQueryStr = print(queryRes);
const expectedQuery = getQueryDefinition(gql`
query {
author {
name {
Expand All @@ -34,8 +35,8 @@ describe('query transforms', () => {
}
__typename
}
__typename}`;
const expectedQueryStr = print(expectedQuery).replace(/\n$/, '');
__typename}`);
const expectedQueryStr = print(expectedQuery);

assert.equal(expectedQueryStr, modifiedQueryStr);
});
Expand All @@ -45,14 +46,14 @@ describe('query transforms', () => {
query {
testString
}`;
const expectedQuery = gql`
const expectedQuery = getQueryDefinition(gql`
query {
testString
__typename
}`;
const modifiedQuery = addTypenameToRoot(getQueryDefinition(testQuery));
const modifiedQueryStr = print(modifiedQuery).replace(/\n$/, '');
const expectedQueryStr = print(expectedQuery).replace(/\n$/, '');
}`);
const modifiedQuery = addTypenameToQuery(getQueryDefinition(testQuery));
const modifiedQueryStr = print(modifiedQuery);
const expectedQueryStr = print(expectedQuery);
assert.equal(expectedQueryStr, modifiedQueryStr);
});

Expand All @@ -65,9 +66,34 @@ describe('query transforms', () => {
}
}`;
const expectedQueryStr = print(testQuery);
addTypenameToRoot(getQueryDefinition(testQuery));
addTypenameToQuery(getQueryDefinition(testQuery));

//make sure that producing the modified query has not changed the original query
assert.equal(expectedQueryStr, print(testQuery));
});

it('should not screw up on a FragmentSpread within the query AST', () => {
const testQuery = getQueryDefinition(gql`
query withFragments {
user(id: 4) {
friends(first: 10) {
...friendFields
}
}
}`);
const expectedQuery = getQueryDefinition(gql`
query withFragments {
user(id: 4) {
friends(first: 10) {
...friendFields
__typename
}
__typename
}
__typename
}`);
const modifiedQuery = addTypenameToQuery(testQuery);
assert.equal(print(expectedQuery), print(modifiedQuery));
});

});

0 comments on commit 2709b7b

Please sign in to comment.