Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
- removed circular dependency in data/store.ts
- added "ApolloClient" to the named exports to make it compatible with Angular2 AOT compile
- moved dev @types to devDependencies otherwise they potentially brake projects that are importing apollo-client
  • Loading branch information
Chris Metz committed Oct 15, 2016
1 parent d27305a commit d79a74d
Show file tree
Hide file tree
Showing 16 changed files with 61 additions and 50 deletions.
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@
"whatwg-fetch": "^1.0.0"
},
"devDependencies": {
"@types/chai": "^3.4.32",
"@types/chai-as-promised": "0.0.28",
"@types/mocha": "^2.2.31",
"browserify": "^13.0.0",
"chai": "^3.5.0",
"chai-as-promised": "^6.0.0",
Expand Down Expand Up @@ -90,11 +93,8 @@
},
"optionalDependencies": {
"@types/async": "^2.0.31",
"@types/chai": "^3.4.32",
"@types/chai-as-promised": "0.0.28",
"@types/isomorphic-fetch": "0.0.30",
"@types/lodash": "^4.14.34",
"@types/mocha": "^2.2.31",
"@types/node": "^6.0.38",
"@types/promises-a-plus": "0.0.26",
"@types/redux": "^3.5.29",
Expand Down
2 changes: 1 addition & 1 deletion src/core/QueryManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import {

import {
NormalizedCache,
} from '../data/store';
} from '../data/storeUtils';

import {
createStoreReducer,
Expand Down
2 changes: 1 addition & 1 deletion src/data/mutationResults.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {
NormalizedCache,
StoreObject,
} from './store';
} from './storeUtils';

import {
Document,
Expand Down
2 changes: 1 addition & 1 deletion src/data/readFromStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
NormalizedCache,
isJsonValue,
isIdValue,
} from './store';
} from './storeUtils';

import {
storeKeyNameFromFieldNameAndArgs,
Expand Down
2 changes: 1 addition & 1 deletion src/data/replaceQueryResults.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {
NormalizedCache,
} from './store';
} from './storeUtils';

import {
writeResultToStore,
Expand Down
2 changes: 1 addition & 1 deletion src/data/resultReducers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {

import {
NormalizedCache,
} from './store';
} from './storeUtils';

import {
ApolloReducer,
Expand Down
34 changes: 1 addition & 33 deletions src/data/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import {

import {
graphQLResultHasError,
NormalizedCache,
} from './storeUtils';

import {
Expand All @@ -39,39 +40,6 @@ import {
replaceQueryResults,
} from './replaceQueryResults';

/**
* This is a normalized representation of the Apollo query result cache. Briefly, it consists of
* a flatten representation of query result trees.
*/
export interface NormalizedCache {
[dataId: string]: StoreObject;
}

export interface StoreObject {
__typename?: string;
[storeFieldKey: string]: StoreValue;
}

export interface IdValue {
type: 'id';
id: string;
generated: boolean;
}

export interface JsonValue {
type: 'json';
json: any;
}

export type StoreValue = number | string | string[] | IdValue | JsonValue | void;

export function isIdValue(idObject: StoreValue): idObject is IdValue {
return (isObject(idObject) && (idObject as (IdValue | JsonValue)).type === 'id');
}

export function isJsonValue(jsonObject: StoreValue): jsonObject is JsonValue {
return (isObject(jsonObject) && (jsonObject as (IdValue | JsonValue)).type === 'json');
}

export function data(
previousState: NormalizedCache = {},
Expand Down
40 changes: 38 additions & 2 deletions src/data/storeUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import {
Name,
} from 'graphql';

import isObject = require('lodash.isobject');

function isStringValue(value: Value): value is StringValue {
return value.kind === 'StringValue';
}
Expand All @@ -34,7 +36,7 @@ function isVariable(value: Value): value is Variable {
return value.kind === 'Variable';
}

function isObject(value: Value): value is ObjectValue {
function isGraphObject(value: Value): value is ObjectValue {
return value.kind === 'ObjectValue';
}

Expand All @@ -49,7 +51,7 @@ function valueToObjectRepresentation(argObj: Object, name: Name, value: Value, v
} else if (isBooleanValue(value) || isStringValue(value)) {
(argObj as any)[name.value] = value.value;

} else if (isObject(value)) {
} else if (isGraphObject(value)) {
const nestedArgObj = {};
value.fields.map((obj) => valueToObjectRepresentation(nestedArgObj, obj.name, obj.value, variables));
(argObj as any)[name.value] = nestedArgObj;
Expand Down Expand Up @@ -114,3 +116,37 @@ export function isInlineFragment(selection: Selection): selection is InlineFragm
export function graphQLResultHasError(result: GraphQLResult) {
return result.errors && result.errors.length;
}

/**
* This is a normalized representation of the Apollo query result cache. Briefly, it consists of
* a flatten representation of query result trees.
*/
export interface NormalizedCache {
[dataId: string]: StoreObject;
}

export interface StoreObject {
__typename?: string;
[storeFieldKey: string]: StoreValue;
}

export interface IdValue {
type: 'id';
id: string;
generated: boolean;
}

export interface JsonValue {
type: 'json';
json: any;
}

export type StoreValue = number | string | string[] | IdValue | JsonValue | void;

export function isIdValue(idObject: StoreValue): idObject is IdValue {
return (isObject(idObject) && (idObject as (IdValue | JsonValue)).type === 'id');
}

export function isJsonValue(jsonObject: StoreValue): jsonObject is JsonValue {
return (isObject(jsonObject) && (jsonObject as (IdValue | JsonValue)).type === 'json');
}
2 changes: 1 addition & 1 deletion src/data/writeToStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import {
StoreObject,
IdValue,
isIdValue,
} from './store';
} from './storeUtils';

import {
IdGetter,
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ export {
MutationQueryReducersMap,
Subscription,
ApolloStore,
ApolloClient
};

export default ApolloClient;
5 changes: 4 additions & 1 deletion src/optimistic-data/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@ import {

import {
data,
NormalizedCache,
} from '../data/store';

import {
NormalizedCache,
} from '../data/storeUtils';

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

import {
data,
NormalizedCache,
} from './data/store';

import {
NormalizedCache,
} from './data/storeUtils';

import {
queries,
QueryStore,
Expand Down
2 changes: 1 addition & 1 deletion test/mutationResults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { assert } from 'chai';
import mockNetworkInterface from './mocks/mockNetworkInterface';
import ApolloClient, { addTypename } from '../src';
import { MutationBehaviorReducerArgs, MutationBehavior, cleanArray } from '../src/data/mutationResults';
import { NormalizedCache, StoreObject } from '../src/data/store';
import { NormalizedCache, StoreObject } from '../src/data/storeUtils';
import { isMutationResultAction, isQueryResultAction } from '../src/actions';

import assign = require('lodash.assign');
Expand Down
2 changes: 1 addition & 1 deletion test/optimistic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const { assert } = chai;
import mockNetworkInterface from './mocks/mockNetworkInterface';
import ApolloClient, { addTypename, createFragment } from '../src';
import { MutationBehaviorReducerArgs, MutationBehavior, MutationQueryReducersMap } from '../src/data/mutationResults';
import { NormalizedCache, StoreObject } from '../src/data/store';
import { NormalizedCache, StoreObject } from '../src/data/storeUtils';
import { addFragmentsToDocument } from '../src/queries/getFromAST';

import assign = require('lodash.assign');
Expand Down
2 changes: 1 addition & 1 deletion test/readFromStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
import {
NormalizedCache,
StoreObject,
} from '../src/data/store';
} from '../src/data/storeUtils';

import gql from 'graphql-tag';

Expand Down
2 changes: 1 addition & 1 deletion test/writeToStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {

import {
NormalizedCache,
} from '../src/data/store';
} from '../src/data/storeUtils';

import {
Selection,
Expand Down

0 comments on commit d79a74d

Please sign in to comment.