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

Refactoring fetchQueryOverInterface within QueryManager #498

Merged
merged 9 commits into from
Aug 3, 2016
78 changes: 53 additions & 25 deletions src/QueryManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
createFragmentMap,
getOperationName,
addFragmentsToDocument,
FragmentMap,
} from './queries/getFromAST';

import {
Expand All @@ -38,6 +39,7 @@ import {
import {
GraphQLResult,
Document,
OperationDefinition,
FragmentDefinition,
// We need to import this here to allow TypeScript to include it in the definition file even
// though we don't use it. https://github.com/Microsoft/TypeScript/issues/5711
Expand Down Expand Up @@ -602,39 +604,65 @@ export class QueryManager {
return resultBehaviors;
}

// Takes a set of WatchQueryOptions and transforms the query document
// accordingly. Specifically, it does the following:
// 1. Adds the fragments to the document
// 2. Applies the queryTransformer (if there is one defined)
// 3. Creates a fragment map out of all of the fragment definitions within the query
// document.
// 4. Returns the final query document and the fragment map associated with the
// query.
private transformQueryDocument(options: WatchQueryOptions): {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there some way we can make this return less stuff? For example, document, definition, and selection set seem a bit redundant.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I'm still working on the refactor. The final stuff that it will return will probably only be the document and nothing else.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, I spoke too soon

queryDoc: Document,
fragmentMap: FragmentMap,
queryDef: OperationDefinition,
queryString: string,
querySS: SelectionSetWithRoot,
} {
const {
query,
fragments = [],
} = options;
let queryDoc = addFragmentsToDocument(query, fragments);

// Apply the query transformer if one has been provided
if (this.queryTransformer) {
queryDoc = applyTransformers(queryDoc, [ this.queryTransformer ]);
}

const queryDef = getQueryDefinition(queryDoc);
return {
queryDoc,
fragmentMap: createFragmentMap(getFragmentDefinitions(queryDoc)),
queryDef,
queryString: print(queryDoc),
querySS: {
id: 'ROOT_QUERY',
typeName: 'Query',
selectionSet: queryDef.selectionSet,
} as SelectionSetWithRoot,
};
}

private fetchQueryOverInterface(
queryId: string,
options: WatchQueryOptions,
network: NetworkInterface
): Promise<ApolloQueryResult> {
const {
query,
variables,
forceFetch = false,
returnPartialData = false,
noFetch = false,
fragments = [],
} = options;

let queryDoc = addFragmentsToDocument(query, fragments);
// Apply the query transformer if one has been provided.
if (this.queryTransformer) {
queryDoc = applyTransformers(queryDoc, [this.queryTransformer]);
}

// Add the fragments passed in into the query and then create the fragment map
const queryFragmentMap = createFragmentMap(getFragmentDefinitions(queryDoc));
const queryDef = getQueryDefinition(queryDoc);
const queryString = print(queryDoc);

// Parse the query passed in -- this could also be done by a build plugin or tagged
// template string
const querySS = {
id: 'ROOT_QUERY',
typeName: 'Query',
selectionSet: queryDef.selectionSet,
} as SelectionSetWithRoot;

const {
queryDoc,
fragmentMap,
queryDef,
queryString,
querySS,
} = this.transformQueryDocument(options);
// If we don't use diffing, then these will be the same as the original query, other than
// the queryTransformer that could have been applied.
let minimizedQueryString = queryString;
Expand All @@ -652,7 +680,7 @@ export class QueryManager {
throwOnMissingField: false,
rootId: querySS.id,
variables,
fragmentMap: queryFragmentMap,
fragmentMap,
});

initialResult = result;
Expand All @@ -662,7 +690,7 @@ export class QueryManager {
missingSelectionSets,
variableDefinitions: queryDef.variableDefinitions,
name: queryDef.name,
fragmentMap: queryFragmentMap,
fragmentMap,
});
const diffedQueryDef = getQueryDefinition(diffedQuery);

Expand Down Expand Up @@ -695,7 +723,7 @@ export class QueryManager {
returnPartialData: returnPartialData || noFetch,
queryId,
requestId,
fragmentMap: queryFragmentMap,
fragmentMap,
});

if (! minimizedQuery || returnPartialData || noFetch) {
Expand Down Expand Up @@ -752,7 +780,7 @@ export class QueryManager {
selectionSet: querySS.selectionSet,
variables,
returnPartialData: returnPartialData || noFetch,
fragmentMap: queryFragmentMap,
fragmentMap,
});
// ensure multiple errors don't get thrown
/* tslint:disable */
Expand Down