Skip to content

Commit

Permalink
Fix circular dependencies
Browse files Browse the repository at this point in the history
Note: Typescript files with only types, are used only in compilation
phase and not going to be imported on compiled javascript.

Closes #1074
  • Loading branch information
Urigo committed Dec 22, 2016
1 parent 9895de4 commit 2e1e821
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 62 deletions.
5 changes: 4 additions & 1 deletion src/ApolloClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,13 @@ import {

import {
QueryManager,
} from './core/QueryManager';

import {
ApolloQueryResult,
ResultComparator,
ResultTransformer,
} from './core/QueryManager';
} from './core/types';

import {
ObservableQuery,
Expand Down
5 changes: 4 additions & 1 deletion src/core/ObservableQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,12 @@ import {

import {
QueryManager,
} from './QueryManager';

import {
ApolloQueryResult,
FetchType,
} from './QueryManager';
} from './types';

import { tryFunctionOrLogError } from '../util/errorHandling';

Expand Down
60 changes: 14 additions & 46 deletions src/core/QueryManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,27 @@ import forOwn from 'lodash/forOwn';
import isEqual from 'lodash/isEqual';
import assign from 'lodash/assign';

import {
ResultTransformer,
ResultComparator,
QueryListener,
ApolloQueryResult,
FetchType,
SubscriptionOptions,
} from './types';

import {
QueryStoreValue,
NetworkStatus,
} from '../queries/store';

import {
ApolloStore,
Store,
getDataWithOptimisticResults,
ApolloReducerConfig,
} from '../store';

import {
QueryStoreValue,
} from '../queries/store';

import {
checkDocument,
getQueryDefinition,
Expand Down Expand Up @@ -83,10 +93,6 @@ import {
Observable,
} from '../util/Observable';

import {
NetworkStatus,
} from '../queries/store';

import { tryFunctionOrLogError } from '../util/errorHandling';

import {
Expand All @@ -98,44 +104,6 @@ import { WatchQueryOptions } from './watchQueryOptions';

import { ObservableQuery } from './ObservableQuery';

export type QueryListener = (queryStoreValue: QueryStoreValue) => void;

export interface SubscriptionOptions {
document: Document;
variables?: { [key: string]: any };
};

export type ApolloQueryResult = {
data: any;
loading: boolean;
networkStatus: NetworkStatus;

// This type is different from the GraphQLResult type because it doesn't include errors.
// Those are thrown via the standard promise/observer catch mechanism.
};

// A result transformer is given the data that is to be returned from the store from a query or
// mutation, and can modify or observe it before the value is provided to your application.
//
// For watched queries, the transformer is only called when the data retrieved from the server is
// different from previous.
//
// If the transformer wants to mutate results (say, by setting the prototype of result data), it
// will likely need to be paired with a custom resultComparator. By default, Apollo performs a
// deep equality comparsion on results, and skips those that are considered equal - reducing
// re-renders.
export type ResultTransformer = (resultData: ApolloQueryResult) => ApolloQueryResult;

// Controls how Apollo compares two query results and considers their equality. Two equal results
// will not trigger re-renders.
export type ResultComparator = (result1: ApolloQueryResult, result2: ApolloQueryResult) => boolean;

export enum FetchType {
normal = 1,
refetch = 2,
poll = 3,
}

export class QueryManager {
public pollingTimers: {[queryId: string]: NodeJS.Timer | any}; //oddity in Typescript
public scheduler: QueryScheduler;
Expand Down
43 changes: 43 additions & 0 deletions src/core/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { Document } from 'graphql';
import {
QueryStoreValue,
NetworkStatus,
} from '../queries/store';

export interface SubscriptionOptions {
document: Document;
variables?: { [key: string]: any };
};

export type QueryListener = (queryStoreValue: QueryStoreValue) => void;

export type ApolloQueryResult = {
data: any;
loading: boolean;
networkStatus: NetworkStatus;

// This type is different from the GraphQLResult type because it doesn't include errors.
// Those are thrown via the standard promise/observer catch mechanism.
};

// A result transformer is given the data that is to be returned from the store from a query or
// mutation, and can modify or observe it before the value is provided to your application.
//
// For watched queries, the transformer is only called when the data retrieved from the server is
// different from previous.
//
// If the transformer wants to mutate results (say, by setting the prototype of result data), it
// will likely need to be paired with a custom resultComparator. By default, Apollo performs a
// deep equality comparsion on results, and skips those that are considered equal - reducing
// re-renders.
export type ResultTransformer = (resultData: ApolloQueryResult) => ApolloQueryResult;

// Controls how Apollo compares two query results and considers their equality. Two equal results
// will not trigger re-renders.
export type ResultComparator = (result1: ApolloQueryResult, result2: ApolloQueryResult) => boolean;

export enum FetchType {
normal = 1,
refetch = 2,
poll = 3,
}
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ import {

import {
ApolloQueryResult,
} from './core/QueryManager';
} from './core/types';

import {
toIdValue,
Expand Down
11 changes: 9 additions & 2 deletions src/optimistic-data/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import {
} from '../data/storeUtils';

import {
getDataWithOptimisticResults,
Store,
} from '../store';

Expand All @@ -29,6 +28,14 @@ export type OptimisticStore = {

const optimisticDefaultState: any[] = [];

export function getDataWithOptimisticResults(store: Store): NormalizedCache {
if (store.optimistic.length === 0) {
return store.data;
}
const patches = store.optimistic.map(opt => opt.data);
return assign({}, store.data, ...patches) as NormalizedCache;
}

export function optimistic(
previousState = optimisticDefaultState,
action: any,
Expand All @@ -46,7 +53,7 @@ export function optimistic(
extraReducers: action.extraReducers,
};

const fakeStore = assign({}, store, { optimistic: previousState }) as Store;
const fakeStore = assign({}, store, { optimistic: previousState });
const optimisticData = getDataWithOptimisticResults(fakeStore);
const fakeDataResultState = data(
optimisticData,
Expand Down
7 changes: 5 additions & 2 deletions src/scheduler/scheduler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,13 @@

import {
QueryManager,
QueryListener,
FetchType,
} from '../core/QueryManager';

import {
FetchType,
QueryListener,
} from '../core/types';

import { ObservableQuery } from '../core/ObservableQuery';

import { WatchQueryOptions } from '../core/watchQueryOptions';
Expand Down
11 changes: 2 additions & 9 deletions src/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ import {
import {
optimistic,
OptimisticStore,
getDataWithOptimisticResults,
} from './optimistic-data/store';
export { getDataWithOptimisticResults };

import {
ApolloAction,
Expand Down Expand Up @@ -175,17 +177,8 @@ export function createApolloStore({
);
}


export type ApolloReducerConfig = {
dataIdFromObject?: IdGetter;
mutationBehaviorReducers?: MutationBehaviorReducerMap;
customResolvers?: CustomResolverMap;
};

export function getDataWithOptimisticResults(store: Store): NormalizedCache {
if (store.optimistic.length === 0) {
return store.data;
}
const patches = store.optimistic.map(opt => opt.data);
return assign({}, store.data, ...patches) as NormalizedCache;
}

0 comments on commit 2e1e821

Please sign in to comment.