apollo-client
Advanced tools
Comparing version 0.3.1 to 0.3.2
@@ -5,6 +5,6 @@ import { NormalizedCache } from './store'; | ||
import { SelectionSet, Document } from 'graphql'; | ||
export interface QueryDiffResult { | ||
export interface DiffResult { | ||
result: any; | ||
missingSelectionSets: SelectionSetWithRoot[]; | ||
mergeUp: boolean; | ||
isMissing?: 'true'; | ||
missingSelectionSets?: SelectionSetWithRoot[]; | ||
} | ||
@@ -16,3 +16,3 @@ export declare function diffQueryAgainstStore({store, query, variables, dataIdFromObject}: { | ||
dataIdFromObject?: IdGetter; | ||
}): QueryDiffResult; | ||
}): DiffResult; | ||
export declare function diffFragmentAgainstStore({store, fragment, rootId, variables, dataIdFromObject}: { | ||
@@ -24,3 +24,3 @@ store: NormalizedCache; | ||
dataIdFromObject?: IdGetter; | ||
}): QueryDiffResult; | ||
}): DiffResult; | ||
export declare function diffSelectionSetAgainstStore({selectionSet, store, rootId, throwOnMissingField, variables, dataIdFromObject}: { | ||
@@ -30,5 +30,5 @@ selectionSet: SelectionSet; | ||
rootId: string; | ||
throwOnMissingField: Boolean; | ||
throwOnMissingField: boolean; | ||
variables: Object; | ||
dataIdFromObject?: IdGetter; | ||
}): QueryDiffResult; | ||
}): DiffResult; |
@@ -6,2 +6,3 @@ "use strict"; | ||
var has = require('lodash.has'); | ||
var assign = require('lodash.assign'); | ||
var storeUtils_1 = require('./storeUtils'); | ||
@@ -41,125 +42,66 @@ var getFromAST_1 = require('../queries/getFromAST'); | ||
var result = {}; | ||
var missingSelectionSets = []; | ||
var missingFields = []; | ||
var storeObj = store[rootId] || {}; | ||
var missingFieldPushed = false; | ||
function pushMissingField(missingField) { | ||
if (!missingFieldPushed) { | ||
missingFields.push(missingField); | ||
missingFieldPushed = true; | ||
} | ||
} | ||
selectionSet.selections.forEach(function (selection) { | ||
if (selection.kind !== 'Field') { | ||
throw new Error('Only fields supported so far, not fragments.'); | ||
} | ||
var field = selection; | ||
var storeFieldKey = storeUtils_1.storeKeyNameFromField(field, variables); | ||
var resultFieldKey = storeUtils_1.resultKeyNameFromField(field); | ||
var missingFieldPushed = false; | ||
function pushMissingField(missingField) { | ||
if (!missingFieldPushed) { | ||
missingFields.push(missingField); | ||
missingFieldPushed = true; | ||
if (storeUtils_1.isField(selection)) { | ||
var _a = diffFieldAgainstStore({ | ||
field: selection, | ||
throwOnMissingField: throwOnMissingField, | ||
variables: variables, | ||
rootId: rootId, | ||
store: store, | ||
dataIdFromObject: dataIdFromObject, | ||
}), fieldResult = _a.result, fieldIsMissing = _a.isMissing; | ||
if (fieldIsMissing) { | ||
pushMissingField(selection); | ||
} | ||
} | ||
if (!has(storeObj, storeFieldKey)) { | ||
if (throwOnMissingField) { | ||
throw new Error("Can't find field " + storeFieldKey + " on object " + storeObj + "."); | ||
else { | ||
var resultFieldKey = storeUtils_1.resultKeyNameFromField(selection); | ||
result[resultFieldKey] = fieldResult; | ||
} | ||
missingFields.push(field); | ||
return; | ||
} | ||
var storeValue = storeObj[storeFieldKey]; | ||
if (!field.selectionSet) { | ||
result[resultFieldKey] = storeValue; | ||
return; | ||
} | ||
if (isNull(storeValue)) { | ||
result[resultFieldKey] = null; | ||
return; | ||
} | ||
if (isArray(storeValue)) { | ||
result[resultFieldKey] = storeValue.map(function (id) { | ||
if (isNull(id)) { | ||
return null; | ||
} | ||
var itemDiffResult = diffSelectionSetAgainstStore({ | ||
store: store, | ||
throwOnMissingField: throwOnMissingField, | ||
rootId: id, | ||
selectionSet: field.selectionSet, | ||
variables: variables, | ||
dataIdFromObject: dataIdFromObject, | ||
}); | ||
if (!itemDiffResult.mergeUp) { | ||
itemDiffResult.missingSelectionSets.forEach(function (itemSelectionSet) { return missingSelectionSets.push(itemSelectionSet); }); | ||
} | ||
else { | ||
pushMissingField(field); | ||
} | ||
return itemDiffResult.result; | ||
}); | ||
return; | ||
} | ||
if (isString(storeValue)) { | ||
var subObjDiffResult = diffSelectionSetAgainstStore({ | ||
store: store, | ||
else if (storeUtils_1.isInlineFragment(selection)) { | ||
var _b = diffSelectionSetAgainstStore({ | ||
selectionSet: selection.selectionSet, | ||
throwOnMissingField: throwOnMissingField, | ||
rootId: storeValue, | ||
selectionSet: field.selectionSet, | ||
variables: variables, | ||
rootId: rootId, | ||
store: store, | ||
dataIdFromObject: dataIdFromObject, | ||
}); | ||
if (!subObjDiffResult.mergeUp) { | ||
subObjDiffResult.missingSelectionSets.forEach(function (subObjSelectionSet) { return missingSelectionSets.push(subObjSelectionSet); }); | ||
}), fieldResult = _b.result, fieldIsMissing = _b.isMissing; | ||
if (fieldIsMissing) { | ||
pushMissingField(selection); | ||
} | ||
else { | ||
pushMissingField(field); | ||
assign(result, fieldResult); | ||
} | ||
result[resultFieldKey] = subObjDiffResult.result; | ||
return; | ||
} | ||
throw new Error('Unexpected number value in the store where the query had a subselection.'); | ||
else { | ||
throw new Error('Named fragments not yet supported.'); | ||
} | ||
}); | ||
var mergeUp = false; | ||
var isMissing; | ||
var missingSelectionSets; | ||
if (missingFields.length) { | ||
if (dataIdFromObject) { | ||
var id = dataIdFromObject(storeObj); | ||
if (typeof id !== 'string' && rootId !== 'ROOT_QUERY') { | ||
throw new Error("Can't generate query to refetch object " + rootId + ", since it doesn't have a string id."); | ||
} | ||
var typeName = void 0; | ||
if (rootId === 'ROOT_QUERY') { | ||
typeName = 'Query'; | ||
} | ||
else if (!storeObj.__typename) { | ||
throw new Error("Can't generate query to refetch object " + rootId + ", since __typename wasn't in the store."); | ||
} | ||
else { | ||
typeName = storeObj.__typename; | ||
} | ||
missingSelectionSets.push({ | ||
id: rootId, | ||
typeName: typeName, | ||
selectionSet: { | ||
kind: 'SelectionSet', | ||
selections: missingFields, | ||
}, | ||
}); | ||
} | ||
else if (rootId === 'ROOT_QUERY') { | ||
if (rootId === 'ROOT_QUERY') { | ||
var typeName = 'Query'; | ||
missingSelectionSets.push({ | ||
id: rootId, | ||
typeName: typeName, | ||
selectionSet: { | ||
kind: 'SelectionSet', | ||
selections: missingFields, | ||
missingSelectionSets = [ | ||
{ | ||
id: rootId, | ||
typeName: typeName, | ||
selectionSet: { | ||
kind: 'SelectionSet', | ||
selections: missingFields, | ||
}, | ||
}, | ||
}); | ||
]; | ||
} | ||
else { | ||
mergeUp = true; | ||
missingSelectionSets.push({ | ||
id: 'CANNOT_REFETCH', | ||
typeName: 'CANNOT_REFETCH', | ||
selectionSet: { | ||
kind: 'SelectionSet', | ||
selections: missingFields, | ||
}, | ||
}); | ||
isMissing = 'true'; | ||
} | ||
@@ -169,7 +111,66 @@ } | ||
result: result, | ||
isMissing: isMissing, | ||
missingSelectionSets: missingSelectionSets, | ||
mergeUp: mergeUp, | ||
}; | ||
} | ||
exports.diffSelectionSetAgainstStore = diffSelectionSetAgainstStore; | ||
function diffFieldAgainstStore(_a) { | ||
var field = _a.field, throwOnMissingField = _a.throwOnMissingField, variables = _a.variables, rootId = _a.rootId, store = _a.store, dataIdFromObject = _a.dataIdFromObject; | ||
var storeObj = store[rootId] || {}; | ||
var storeFieldKey = storeUtils_1.storeKeyNameFromField(field, variables); | ||
if (!has(storeObj, storeFieldKey)) { | ||
if (throwOnMissingField) { | ||
throw new Error("Can't find field " + storeFieldKey + " on object " + storeObj + "."); | ||
} | ||
return { | ||
isMissing: 'true', | ||
}; | ||
} | ||
var storeValue = storeObj[storeFieldKey]; | ||
if (!field.selectionSet) { | ||
return { | ||
result: storeValue, | ||
}; | ||
} | ||
if (isNull(storeValue)) { | ||
return { | ||
result: null, | ||
}; | ||
} | ||
if (isArray(storeValue)) { | ||
var isMissing_1; | ||
var result = storeValue.map(function (id) { | ||
if (isNull(id)) { | ||
return null; | ||
} | ||
var itemDiffResult = diffSelectionSetAgainstStore({ | ||
store: store, | ||
throwOnMissingField: throwOnMissingField, | ||
rootId: id, | ||
selectionSet: field.selectionSet, | ||
variables: variables, | ||
dataIdFromObject: dataIdFromObject, | ||
}); | ||
if (itemDiffResult.isMissing) { | ||
isMissing_1 = 'true'; | ||
} | ||
return itemDiffResult.result; | ||
}); | ||
return { | ||
result: result, | ||
isMissing: isMissing_1, | ||
}; | ||
} | ||
if (isString(storeValue)) { | ||
return diffSelectionSetAgainstStore({ | ||
store: store, | ||
throwOnMissingField: throwOnMissingField, | ||
rootId: storeValue, | ||
selectionSet: field.selectionSet, | ||
variables: variables, | ||
dataIdFromObject: dataIdFromObject, | ||
}); | ||
} | ||
throw new Error('Unexpected number value in the store where the query had a subselection.'); | ||
} | ||
//# sourceMappingURL=diffAgainstStore.js.map |
@@ -5,3 +5,3 @@ "use strict"; | ||
var assign = require('lodash.assign'); | ||
var resultUtils_1 = require('./resultUtils'); | ||
var storeUtils_1 = require('./storeUtils'); | ||
function data(previousState, action, queries, mutations, config) { | ||
@@ -16,3 +16,3 @@ if (previousState === void 0) { previousState = {}; } | ||
} | ||
if (!resultUtils_1.graphQLResultHasError(action.result)) { | ||
if (!storeUtils_1.graphQLResultHasError(action.result)) { | ||
var queryStoreValue = queries[action.queryId]; | ||
@@ -19,0 +19,0 @@ var clonedState = assign({}, previousState); |
@@ -1,3 +0,6 @@ | ||
import { Field } from 'graphql'; | ||
import { Field, InlineFragment, Selection, GraphQLResult } from 'graphql'; | ||
export declare function storeKeyNameFromField(field: Field, variables?: Object): string; | ||
export declare function resultKeyNameFromField(field: Field): string; | ||
export declare function isField(selection: Selection): selection is Field; | ||
export declare function isInlineFragment(selection: Selection): selection is InlineFragment; | ||
export declare function graphQLResultHasError(result: GraphQLResult): number; |
@@ -41,2 +41,14 @@ "use strict"; | ||
exports.resultKeyNameFromField = resultKeyNameFromField; | ||
function isField(selection) { | ||
return selection.kind === 'Field'; | ||
} | ||
exports.isField = isField; | ||
function isInlineFragment(selection) { | ||
return selection.kind === 'InlineFragment'; | ||
} | ||
exports.isInlineFragment = isInlineFragment; | ||
function graphQLResultHasError(result) { | ||
return result.errors && result.errors.length; | ||
} | ||
exports.graphQLResultHasError = graphQLResultHasError; | ||
//# sourceMappingURL=storeUtils.js.map |
@@ -47,3 +47,3 @@ "use strict"; | ||
selectionSet.selections.forEach(function (selection) { | ||
if (isField(selection)) { | ||
if (storeUtils_1.isField(selection)) { | ||
var resultFieldKey = storeUtils_1.resultKeyNameFromField(selection); | ||
@@ -63,3 +63,3 @@ var value = result[resultFieldKey]; | ||
} | ||
else if (isInlineFragment(selection)) { | ||
else if (storeUtils_1.isInlineFragment(selection)) { | ||
writeSelectionSetToStore({ | ||
@@ -145,8 +145,2 @@ result: result, | ||
} | ||
function isField(selection) { | ||
return selection.kind === 'Field'; | ||
} | ||
function isInlineFragment(selection) { | ||
return selection.kind === 'InlineFragment'; | ||
} | ||
//# sourceMappingURL=writeToStore.js.map |
{ | ||
"name": "apollo-client", | ||
"version": "0.3.1", | ||
"version": "0.3.2", | ||
"description": "A simple yet functional GraphQL client.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
"use strict"; | ||
var actions_1 = require('../actions'); | ||
var resultUtils_1 = require('../data/resultUtils'); | ||
var storeUtils_1 = require('../data/storeUtils'); | ||
var assign = require('lodash.assign'); | ||
@@ -32,3 +32,3 @@ function queries(previousState, action) { | ||
var newState = assign({}, previousState); | ||
var resultHasGraphQLErrors = resultUtils_1.graphQLResultHasError(action.result); | ||
var resultHasGraphQLErrors = storeUtils_1.graphQLResultHasError(action.result); | ||
newState[action.queryId] = assign({}, previousState[action.queryId], { | ||
@@ -35,0 +35,0 @@ loading: false, |
@@ -179,3 +179,3 @@ "use strict"; | ||
initialResult = result; | ||
if (missingSelectionSets.length) { | ||
if (missingSelectionSets && missingSelectionSets.length) { | ||
var diffedQueryDef = queryPrinting_1.queryDefinition({ | ||
@@ -182,0 +182,0 @@ missingSelectionSets: missingSelectionSets, |
@@ -7,10 +7,2 @@ "use strict"; | ||
exports.printQueryForMissingData = printQueryForMissingData; | ||
var idField = { | ||
kind: 'Field', | ||
alias: null, | ||
name: { | ||
kind: 'Name', | ||
value: 'id', | ||
}, | ||
}; | ||
function printQueryFromDefinition(queryDef) { | ||
@@ -38,8 +30,3 @@ var queryDocumentAst = { | ||
} | ||
return nodeSelection({ | ||
alias: "__node_" + ii, | ||
id: missingSelectionSet.id, | ||
typeName: missingSelectionSet.typeName, | ||
selectionSet: missingSelectionSet.selectionSet, | ||
}); | ||
throw new Error('Only root query selections supported.'); | ||
}); | ||
@@ -59,56 +46,2 @@ return { | ||
exports.queryDefinition = queryDefinition; | ||
function nodeSelection(_a) { | ||
var id = _a.id, typeName = _a.typeName, selectionSet = _a.selectionSet, alias = _a.alias; | ||
var aliasNode = alias ? { | ||
kind: 'Name', | ||
value: alias, | ||
} : null; | ||
return { | ||
kind: 'Field', | ||
alias: aliasNode, | ||
name: { | ||
kind: 'Name', | ||
value: 'node', | ||
}, | ||
arguments: [ | ||
{ | ||
kind: 'Argument', | ||
name: { | ||
kind: 'Name', | ||
value: 'id', | ||
}, | ||
value: { | ||
kind: 'StringValue', | ||
value: id, | ||
}, | ||
}, | ||
], | ||
directives: [], | ||
selectionSet: { | ||
kind: 'SelectionSet', | ||
selections: [ | ||
idField, | ||
inlineFragmentSelection({ | ||
typeName: typeName, | ||
selectionSet: selectionSet, | ||
}), | ||
], | ||
}, | ||
}; | ||
} | ||
function inlineFragmentSelection(_a) { | ||
var typeName = _a.typeName, selectionSet = _a.selectionSet; | ||
return { | ||
kind: 'InlineFragment', | ||
typeCondition: { | ||
kind: 'NamedType', | ||
name: { | ||
kind: 'Name', | ||
value: typeName, | ||
}, | ||
}, | ||
directives: [], | ||
selectionSet: selectionSet, | ||
}; | ||
} | ||
//# sourceMappingURL=queryPrinting.js.map |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
104578
61
1621