apollo-client
Advanced tools
Comparing version 0.4.7 to 0.4.8
@@ -21,2 +21,5 @@ import { NormalizedCache } from './store'; | ||
}): DiffResult; | ||
export declare function handleFragmentErrors(fragmentErrors: { | ||
[typename: string]: Error; | ||
}): void; | ||
export declare function diffSelectionSetAgainstStore({selectionSet, store, rootId, throwOnMissingField, variables, fragmentMap}: { | ||
@@ -23,0 +26,0 @@ selectionSet: SelectionSet; |
@@ -10,2 +10,3 @@ "use strict"; | ||
var directives_1 = require('../queries/directives'); | ||
var errors_1 = require('../errors'); | ||
function diffQueryAgainstStore(_a) { | ||
@@ -35,2 +36,15 @@ var store = _a.store, query = _a.query, variables = _a.variables; | ||
exports.diffFragmentAgainstStore = diffFragmentAgainstStore; | ||
function handleFragmentErrors(fragmentErrors) { | ||
var typenames = Object.keys(fragmentErrors); | ||
if (typenames.length === 0) { | ||
return; | ||
} | ||
var errorTypes = typenames.filter(function (typename) { | ||
return (fragmentErrors[typename] !== null); | ||
}); | ||
if (errorTypes.length === Object.keys(fragmentErrors).length) { | ||
throw fragmentErrors[errorTypes[0]]; | ||
} | ||
} | ||
exports.handleFragmentErrors = handleFragmentErrors; | ||
function diffSelectionSetAgainstStore(_a) { | ||
@@ -46,4 +60,7 @@ var selectionSet = _a.selectionSet, store = _a.store, rootId = _a.rootId, _b = _a.throwOnMissingField, throwOnMissingField = _b === void 0 ? false : _b, variables = _a.variables, fragmentMap = _a.fragmentMap; | ||
var missingFields = []; | ||
var fragmentErrors = {}; | ||
selectionSet.selections.forEach(function (selection) { | ||
var missingFieldPushed = false; | ||
var fieldResult; | ||
var fieldIsMissing; | ||
function pushMissingField(missingField) { | ||
@@ -55,5 +72,5 @@ if (!missingFieldPushed) { | ||
} | ||
var included = directives_1.shouldInclude(selection, variables); | ||
if (storeUtils_1.isField(selection)) { | ||
var includeField = directives_1.shouldInclude(selection, variables); | ||
var _a = diffFieldAgainstStore({ | ||
var diffResult = diffFieldAgainstStore({ | ||
field: selection, | ||
@@ -65,4 +82,6 @@ throwOnMissingField: throwOnMissingField, | ||
fragmentMap: fragmentMap, | ||
included: includeField, | ||
}), fieldResult = _a.result, fieldIsMissing = _a.isMissing; | ||
included: included, | ||
}); | ||
fieldIsMissing = diffResult.isMissing; | ||
fieldResult = diffResult.result; | ||
var resultFieldKey = storeUtils_1.resultKeyNameFromField(selection); | ||
@@ -72,3 +91,3 @@ if (fieldIsMissing) { | ||
} | ||
if (includeField && fieldResult !== undefined) { | ||
if (included && fieldResult !== undefined) { | ||
result[resultFieldKey] = fieldResult; | ||
@@ -78,16 +97,34 @@ } | ||
else if (storeUtils_1.isInlineFragment(selection)) { | ||
var _b = diffSelectionSetAgainstStore({ | ||
selectionSet: selection.selectionSet, | ||
throwOnMissingField: throwOnMissingField, | ||
variables: variables, | ||
rootId: rootId, | ||
store: store, | ||
fragmentMap: fragmentMap, | ||
}), fieldResult = _b.result, fieldIsMissing = _b.isMissing; | ||
if (fieldIsMissing) { | ||
pushMissingField(selection); | ||
var typename = selection.typeCondition.name.value; | ||
if (included) { | ||
try { | ||
var diffResult = diffSelectionSetAgainstStore({ | ||
selectionSet: selection.selectionSet, | ||
throwOnMissingField: throwOnMissingField, | ||
variables: variables, | ||
rootId: rootId, | ||
store: store, | ||
fragmentMap: fragmentMap, | ||
}); | ||
fieldIsMissing = diffResult.isMissing; | ||
fieldResult = diffResult.result; | ||
if (fieldIsMissing) { | ||
pushMissingField(selection); | ||
} | ||
else { | ||
assign(result, fieldResult); | ||
} | ||
if (!fragmentErrors[typename]) { | ||
fragmentErrors[typename] = null; | ||
} | ||
} | ||
catch (e) { | ||
if (e.extraInfo && e.extraInfo.isFieldError) { | ||
fragmentErrors[typename] = e; | ||
} | ||
else { | ||
throw e; | ||
} | ||
} | ||
} | ||
else { | ||
assign(result, fieldResult); | ||
} | ||
} | ||
@@ -99,18 +136,39 @@ else { | ||
} | ||
var _c = diffSelectionSetAgainstStore({ | ||
selectionSet: fragment.selectionSet, | ||
throwOnMissingField: throwOnMissingField, | ||
variables: variables, | ||
rootId: rootId, | ||
store: store, | ||
fragmentMap: fragmentMap, | ||
}), fieldResult = _c.result, fieldIsMissing = _c.isMissing; | ||
if (fieldIsMissing) { | ||
pushMissingField(selection); | ||
var typename = fragment.typeCondition.name.value; | ||
if (included) { | ||
try { | ||
var diffResult = diffSelectionSetAgainstStore({ | ||
selectionSet: fragment.selectionSet, | ||
throwOnMissingField: throwOnMissingField, | ||
variables: variables, | ||
rootId: rootId, | ||
store: store, | ||
fragmentMap: fragmentMap, | ||
}); | ||
fieldIsMissing = diffResult.isMissing; | ||
fieldResult = diffResult.result; | ||
if (fieldIsMissing) { | ||
pushMissingField(selection); | ||
} | ||
else { | ||
assign(result, fieldResult); | ||
} | ||
if (!fragmentErrors[typename]) { | ||
fragmentErrors[typename] = null; | ||
} | ||
} | ||
catch (e) { | ||
if (e.extraInfo && e.extraInfo.isFieldError) { | ||
fragmentErrors[typename] = e; | ||
} | ||
else { | ||
throw e; | ||
} | ||
} | ||
} | ||
else { | ||
assign(result, fieldResult); | ||
} | ||
} | ||
}); | ||
if (throwOnMissingField) { | ||
handleFragmentErrors(fragmentErrors); | ||
} | ||
var isMissing; | ||
@@ -149,3 +207,8 @@ var missingSelectionSets; | ||
if (throwOnMissingField && included) { | ||
throw new Error("Can't find field " + storeFieldKey + " on object " + JSON.stringify(storeObj) + ".\nPerhaps you want to use the `returnPartialData` option?"); | ||
throw new errors_1.ApolloError({ | ||
errorMessage: "Can't find field " + storeFieldKey + " on object " + JSON.stringify(storeObj) + ".\nPerhaps you want to use the `returnPartialData` option?", | ||
extraInfo: { | ||
isFieldError: true, | ||
}, | ||
}); | ||
} | ||
@@ -152,0 +215,0 @@ return { |
import { SelectionSet, Document } from 'graphql'; | ||
import { FragmentMap } from '../queries/getFromAST'; | ||
import { NormalizedCache } from './store'; | ||
export declare function readQueryFromStore({store, query, variables, returnPartialData}: { | ||
export declare function readQueryFromStore({store, query, variables, returnPartialData, fragmentMap}: { | ||
store: NormalizedCache; | ||
@@ -9,2 +9,3 @@ query: Document; | ||
returnPartialData?: boolean; | ||
fragmentMap?: FragmentMap; | ||
}): Object; | ||
@@ -11,0 +12,0 @@ export declare function readFragmentFromStore({store, fragment, rootId, variables, returnPartialData}: { |
@@ -5,3 +5,3 @@ "use strict"; | ||
function readQueryFromStore(_a) { | ||
var store = _a.store, query = _a.query, variables = _a.variables, returnPartialData = _a.returnPartialData; | ||
var store = _a.store, query = _a.query, variables = _a.variables, returnPartialData = _a.returnPartialData, fragmentMap = _a.fragmentMap; | ||
var queryDef = getFromAST_1.getQueryDefinition(query); | ||
@@ -14,2 +14,3 @@ return readSelectionSetFromStore({ | ||
returnPartialData: returnPartialData, | ||
fragmentMap: fragmentMap, | ||
}); | ||
@@ -16,0 +17,0 @@ } |
@@ -12,3 +12,3 @@ import { FragmentMap } from '../queries/getFromAST'; | ||
}): NormalizedCache; | ||
export declare function writeQueryToStore({result, query, store, variables, dataIdFromObject}: { | ||
export declare function writeQueryToStore({result, query, store, variables, dataIdFromObject, fragmentMap}: { | ||
result: Object; | ||
@@ -19,2 +19,3 @@ query: Document; | ||
dataIdFromObject?: IdGetter; | ||
fragmentMap?: FragmentMap; | ||
}): NormalizedCache; | ||
@@ -21,0 +22,0 @@ export declare function writeSelectionSetToStore({result, dataId, selectionSet, store, variables, dataIdFromObject, fragmentMap}: { |
@@ -10,2 +10,3 @@ "use strict"; | ||
var store_1 = require('./store'); | ||
var diffAgainstStore_1 = require('./diffAgainstStore'); | ||
var directives_1 = require('../queries/directives'); | ||
@@ -34,3 +35,3 @@ var errors_1 = require('../errors'); | ||
function writeQueryToStore(_a) { | ||
var result = _a.result, query = _a.query, _b = _a.store, store = _b === void 0 ? {} : _b, variables = _a.variables, _c = _a.dataIdFromObject, dataIdFromObject = _c === void 0 ? null : _c; | ||
var result = _a.result, query = _a.query, _b = _a.store, store = _b === void 0 ? {} : _b, variables = _a.variables, _c = _a.dataIdFromObject, dataIdFromObject = _c === void 0 ? null : _c, fragmentMap = _a.fragmentMap; | ||
var queryDefinition = getFromAST_1.getQueryDefinition(query); | ||
@@ -44,2 +45,3 @@ return writeSelectionSetToStore({ | ||
dataIdFromObject: dataIdFromObject, | ||
fragmentMap: fragmentMap, | ||
}); | ||
@@ -53,12 +55,23 @@ } | ||
} | ||
var fragmentErrors = {}; | ||
selectionSet.selections.forEach(function (selection) { | ||
var included = directives_1.shouldInclude(selection, variables); | ||
if (storeUtils_1.isField(selection)) { | ||
var resultFieldKey = storeUtils_1.resultKeyNameFromField(selection); | ||
var value = result[resultFieldKey]; | ||
var included = directives_1.shouldInclude(selection, variables); | ||
if (isUndefined(value) && included) { | ||
throw new Error("Can't find field " + resultFieldKey + " on result object " + dataId + "."); | ||
throw new errors_1.ApolloError({ | ||
errorMessage: "Can't find field " + resultFieldKey + " on result object " + dataId + ".", | ||
extraInfo: { | ||
isFieldError: true, | ||
}, | ||
}); | ||
} | ||
if (!isUndefined(value) && !included) { | ||
throw new Error("Found extra field " + resultFieldKey + " on result object " + dataId + "."); | ||
throw new errors_1.ApolloError({ | ||
errorMessage: "Found extra field " + resultFieldKey + " on result object " + dataId + ".", | ||
extraInfo: { | ||
isFieldError: true, | ||
}, | ||
}); | ||
} | ||
@@ -78,11 +91,27 @@ if (!isUndefined(value)) { | ||
else if (storeUtils_1.isInlineFragment(selection)) { | ||
writeSelectionSetToStore({ | ||
result: result, | ||
selectionSet: selection.selectionSet, | ||
store: store, | ||
variables: variables, | ||
dataId: dataId, | ||
dataIdFromObject: dataIdFromObject, | ||
fragmentMap: fragmentMap, | ||
}); | ||
var typename = selection.typeCondition.name.value; | ||
if (included) { | ||
try { | ||
writeSelectionSetToStore({ | ||
result: result, | ||
selectionSet: selection.selectionSet, | ||
store: store, | ||
variables: variables, | ||
dataId: dataId, | ||
dataIdFromObject: dataIdFromObject, | ||
fragmentMap: fragmentMap, | ||
}); | ||
if (!fragmentErrors[typename]) { | ||
fragmentErrors[typename] = null; | ||
} | ||
} | ||
catch (e) { | ||
if (e.extraInfo && e.extraInfo.isFieldError) { | ||
fragmentErrors[typename] = e; | ||
} | ||
else { | ||
throw e; | ||
} | ||
} | ||
} | ||
} | ||
@@ -94,13 +123,30 @@ else { | ||
} | ||
writeSelectionSetToStore({ | ||
result: result, | ||
selectionSet: fragment.selectionSet, | ||
store: store, | ||
variables: variables, | ||
dataId: dataId, | ||
dataIdFromObject: dataIdFromObject, | ||
fragmentMap: fragmentMap, | ||
}); | ||
var typename = fragment.typeCondition.name.value; | ||
if (included) { | ||
try { | ||
writeSelectionSetToStore({ | ||
result: result, | ||
selectionSet: fragment.selectionSet, | ||
store: store, | ||
variables: variables, | ||
dataId: dataId, | ||
dataIdFromObject: dataIdFromObject, | ||
fragmentMap: fragmentMap, | ||
}); | ||
if (!fragmentErrors[typename]) { | ||
fragmentErrors[typename] = null; | ||
} | ||
} | ||
catch (e) { | ||
if (e.extraInfo && e.extraInfo.isFieldError) { | ||
fragmentErrors[typename] = e; | ||
} | ||
else { | ||
throw e; | ||
} | ||
} | ||
} | ||
} | ||
}); | ||
diffAgainstStore_1.handleFragmentErrors(fragmentErrors); | ||
return store; | ||
@@ -107,0 +153,0 @@ } |
@@ -6,8 +6,10 @@ import { GraphQLError } from 'graphql'; | ||
networkError: Error; | ||
constructor({graphQLErrors, networkError, errorMessage}: { | ||
extraInfo: any; | ||
constructor({graphQLErrors, networkError, errorMessage, extraInfo}: { | ||
graphQLErrors?: GraphQLError[]; | ||
networkError?: Error; | ||
errorMessage?: string; | ||
extraInfo?: any; | ||
}); | ||
private generateErrorMessage(); | ||
} |
@@ -10,6 +10,7 @@ "use strict"; | ||
function ApolloError(_a) { | ||
var graphQLErrors = _a.graphQLErrors, networkError = _a.networkError, errorMessage = _a.errorMessage; | ||
var graphQLErrors = _a.graphQLErrors, networkError = _a.networkError, errorMessage = _a.errorMessage, extraInfo = _a.extraInfo; | ||
_super.call(this, errorMessage); | ||
this.graphQLErrors = graphQLErrors; | ||
this.networkError = networkError; | ||
this.stack = new Error().stack; | ||
if (!errorMessage) { | ||
@@ -21,2 +22,3 @@ this.generateErrorMessage(); | ||
} | ||
this.extraInfo = extraInfo; | ||
} | ||
@@ -23,0 +25,0 @@ ApolloError.prototype.generateErrorMessage = function () { |
import 'whatwg-fetch'; | ||
import { GraphQLResult, Document } from 'graphql'; | ||
import { MiddlewareInterface } from './middleware'; | ||
import { AfterwareInterface } from './afterware'; | ||
export interface Request { | ||
@@ -27,3 +28,5 @@ debugName?: string; | ||
_middlewares: MiddlewareInterface[]; | ||
_afterwares: AfterwareInterface[]; | ||
use(middlewares: MiddlewareInterface[]): any; | ||
useAfter(afterwares: AfterwareInterface[]): any; | ||
} | ||
@@ -34,4 +37,8 @@ export interface RequestAndOptions { | ||
} | ||
export interface ResponseAndOptions { | ||
response: IResponse; | ||
options: RequestInit; | ||
} | ||
export declare function addQueryMerging(networkInterface: NetworkInterface): BatchedNetworkInterface; | ||
export declare function printRequest(request: Request): PrintedRequest; | ||
export declare function createNetworkInterface(uri: string, opts?: RequestInit): HTTPNetworkInterface; |
@@ -44,2 +44,3 @@ "use strict"; | ||
var _middlewares = []; | ||
var _afterwares = []; | ||
function applyMiddlewares(_a) { | ||
@@ -67,2 +68,24 @@ var _this = this; | ||
} | ||
function applyAfterwares(_a) { | ||
var _this = this; | ||
var response = _a.response, options = _a.options; | ||
return new Promise(function (resolve, reject) { | ||
var queue = function (funcs, scope) { | ||
var next = function () { | ||
if (funcs.length > 0) { | ||
var f = funcs.shift(); | ||
f.applyAfterware.apply(scope, [{ response: response, options: options }, next]); | ||
} | ||
else { | ||
resolve({ | ||
response: response, | ||
options: options, | ||
}); | ||
} | ||
}; | ||
next(); | ||
}; | ||
queue(_afterwares.slice(), _this); | ||
}); | ||
} | ||
function fetchFromRemoteEndpoint(_a) { | ||
@@ -86,2 +109,9 @@ var request = _a.request, options = _a.options; | ||
}).then(fetchFromRemoteEndpoint) | ||
.then(function (response) { | ||
applyAfterwares({ | ||
response: response, | ||
options: options, | ||
}); | ||
return response; | ||
}) | ||
.then(function (result) { return result.json(); }) | ||
@@ -108,2 +138,12 @@ .then(function (payload) { | ||
} | ||
function useAfter(afterwares) { | ||
afterwares.map(function (afterware) { | ||
if (typeof afterware.applyAfterware === 'function') { | ||
_afterwares.push(afterware); | ||
} | ||
else { | ||
throw new Error('Afterware must implement the applyAfterware function'); | ||
} | ||
}); | ||
} | ||
return addQueryMerging({ | ||
@@ -113,4 +153,6 @@ _uri: _uri, | ||
_middlewares: _middlewares, | ||
_afterwares: _afterwares, | ||
query: query, | ||
use: use, | ||
useAfter: useAfter, | ||
}); | ||
@@ -117,0 +159,0 @@ } |
{ | ||
"name": "apollo-client", | ||
"version": "0.4.7", | ||
"version": "0.4.8", | ||
"description": "A simple yet functional GraphQL client.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
"use strict"; | ||
var assign = require('lodash.assign'); | ||
var countBy = require('lodash.countby'); | ||
@@ -94,6 +95,7 @@ var identity = require('lodash.identity'); | ||
checkDocument(queryDoc); | ||
queryDoc.definitions = queryDoc.definitions.concat(fragments); | ||
return queryDoc; | ||
return assign({}, queryDoc, { | ||
definitions: queryDoc.definitions.concat(fragments), | ||
}); | ||
} | ||
exports.addFragmentsToDocument = addFragmentsToDocument; | ||
//# sourceMappingURL=getFromAST.js.map |
@@ -30,6 +30,9 @@ "use strict"; | ||
exports.addTypenameToSelectionSet = addTypenameToSelectionSet; | ||
function traverseSelectionSet(selectionSet, queryTransformers) { | ||
function traverseSelectionSet(selectionSet, queryTransformers, isRoot) { | ||
if (isRoot === void 0) { isRoot = false; } | ||
if (selectionSet && selectionSet.selections) { | ||
queryTransformers.forEach(function (transformer) { | ||
transformer(selectionSet); | ||
if (!isRoot) { | ||
transformer(selectionSet); | ||
} | ||
selectionSet.selections.forEach(function (selection) { | ||
@@ -47,3 +50,8 @@ if (selection.kind === 'Field' || selection.kind === 'InlineFragment') { | ||
docClone.definitions.forEach(function (definition) { | ||
traverseSelectionSet(definition.selectionSet, queryTransformers); | ||
if (definition.kind === 'OperationDefinition') { | ||
traverseSelectionSet(definition.selectionSet, queryTransformers, true); | ||
} | ||
else { | ||
traverseSelectionSet(definition.selectionSet, queryTransformers); | ||
} | ||
}); | ||
@@ -50,0 +58,0 @@ return docClone; |
@@ -86,19 +86,28 @@ "use strict"; | ||
}); | ||
return this.networkInterface.query(request) | ||
.then(function (result) { | ||
_this.store.dispatch({ | ||
type: 'APOLLO_MUTATION_RESULT', | ||
result: result, | ||
mutationId: mutationId, | ||
resultBehaviors: resultBehaviors.concat(_this.collectResultBehaviorsFromUpdateQueries(updateQueries, result)), | ||
return new Promise(function (resolve, reject) { | ||
_this.networkInterface.query(request) | ||
.then(function (result) { | ||
if (result.errors) { | ||
reject(new errors_1.ApolloError({ | ||
graphQLErrors: result.errors, | ||
})); | ||
} | ||
_this.store.dispatch({ | ||
type: 'APOLLO_MUTATION_RESULT', | ||
result: result, | ||
mutationId: mutationId, | ||
resultBehaviors: resultBehaviors.concat(_this.collectResultBehaviorsFromUpdateQueries(updateQueries, result)), | ||
}); | ||
resolve(result); | ||
}) | ||
.catch(function (err) { | ||
_this.store.dispatch({ | ||
type: 'APOLLO_MUTATION_ERROR', | ||
error: err, | ||
mutationId: mutationId, | ||
}); | ||
reject(new errors_1.ApolloError({ | ||
networkError: err, | ||
})); | ||
}); | ||
return result; | ||
}) | ||
.catch(function (err) { | ||
_this.store.dispatch({ | ||
type: 'APOLLO_MUTATION_ERROR', | ||
error: err, | ||
mutationId: mutationId, | ||
}); | ||
return Promise.reject(err); | ||
}); | ||
@@ -420,7 +429,2 @@ }; | ||
_this.removeFetchQueryPromise(requestId); | ||
if (result.errors) { | ||
reject(new errors_1.ApolloError({ | ||
graphQLErrors: result.errors, | ||
})); | ||
} | ||
return result; | ||
@@ -450,5 +454,2 @@ }).then(function () { | ||
_this.removeFetchQueryPromise(requestId); | ||
reject(new errors_1.ApolloError({ | ||
networkError: error, | ||
})); | ||
}); | ||
@@ -455,0 +456,0 @@ }); |
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
Sorry, the diff of this file is not supported yet
247424
91
3655