apollo-link-ethereum
Advanced tools
Comparing version 0.0.10 to 0.0.11
@@ -189,2 +189,5 @@ (function (global, factory) { | ||
var value = object[key]; | ||
if (!value) { | ||
return accumulator; | ||
} | ||
if (value.promise && value.promise instanceof Promise) { | ||
@@ -203,2 +206,75 @@ if (value.error) { | ||
var resolverFactory = function (ethereumResolver, isSubscription) { | ||
var promises = []; | ||
var subscriptions = []; | ||
var contract = null; | ||
var contractDirectives = null; | ||
var resolver = function (fieldName, rootValue, args, context, info) { | ||
if (rootValue === void 0) { rootValue = {}; } | ||
var resultKey = info.resultKey; | ||
var aliasedNode = rootValue[resultKey]; // where data is stored for user aliases | ||
var preAliasingNode = rootValue[fieldName]; // where data is stored in canonical model | ||
var aliasNeeded = resultKey !== fieldName; | ||
console.log("resolverFactory for " + fieldName + " found aliases: " + aliasedNode + " " + preAliasingNode + " " + aliasNeeded); | ||
// if data already exists, return it! | ||
if (aliasedNode !== undefined || preAliasingNode !== undefined) { | ||
console.log("returning " + (aliasedNode || preAliasingNode)); | ||
return aliasedNode || preAliasingNode; | ||
} | ||
// otherwise, run the web3 resolver | ||
// debug(`resolver: `, fieldName, rootValue, args, context, info, contract) | ||
if (info.directives && info.directives.hasOwnProperty('contract')) { | ||
contract = fieldName; | ||
contractDirectives = info.directives.contract; | ||
} | ||
else if (contract) { | ||
var entry_1; | ||
if (isSubscription) { | ||
var observable_1 = ethereumResolver.subscribe(contract, contractDirectives, fieldName, args, info.directives); | ||
observable_1['fieldName'] = fieldName; | ||
entry_1 = { | ||
result: null, | ||
error: null | ||
}; | ||
var dataObservable = new apolloLink.Observable(function (observer) { | ||
console.log("subscribing data update " + fieldName); | ||
observable_1.subscribe({ | ||
next: function (data) { | ||
entry_1.result = data; | ||
observer.next(data); | ||
}, | ||
error: function (error) { | ||
entry_1.error = error; | ||
observer.error(error); | ||
} | ||
}); | ||
}); | ||
subscriptions.push(dataObservable); | ||
} | ||
else { | ||
entry_1 = promiseEntry(ethereumResolver.resolve(contract, contractDirectives, fieldName, args, info.directives)); | ||
promises.push(entry_1.promise); | ||
} | ||
return entry_1; | ||
} | ||
console.log("Returning alias for " + fieldName + "..."); | ||
return ( | ||
// Support nested fields | ||
(aliasNeeded ? aliasedNode : preAliasingNode) || | ||
{}); | ||
}; | ||
var resolverAfter = function (fieldName, rootValue, args, context, info) { | ||
if (rootValue === void 0) { rootValue = {}; } | ||
if (info.directives && info.directives.contract) { | ||
contract = null; | ||
} | ||
}; | ||
return { | ||
promises: promises, | ||
subscriptions: subscriptions, | ||
resolver: resolver, | ||
resolverAfter: resolverAfter | ||
}; | ||
}; | ||
var __extends = (undefined && undefined.__extends) || (function () { | ||
@@ -232,46 +308,8 @@ var extendStatics = function (d, b) { | ||
} | ||
var server = removeContractSetsFromDocument(query); | ||
var resolverFactory = function (promises) { | ||
var contract = null; | ||
var contractDirectives = null; | ||
var resolver = function (fieldName, rootValue, args, context, info) { | ||
if (rootValue === void 0) { rootValue = {}; } | ||
var resultKey = info.resultKey; | ||
var aliasedNode = rootValue[resultKey]; // where data is stored for user aliases | ||
var preAliasingNode = rootValue[fieldName]; // where data is stored in canonical model | ||
var aliasNeeded = resultKey !== fieldName; | ||
// if data already exists, return it! | ||
if (aliasedNode !== undefined || preAliasingNode !== undefined) { | ||
return aliasedNode || preAliasingNode; | ||
} | ||
// otherwise, run the web3 resolver | ||
// debug(`resolver: `, fieldName, rootValue, args, context, info, contract) | ||
if (info.directives && info.directives.hasOwnProperty('contract')) { | ||
contract = fieldName; | ||
contractDirectives = info.directives.contract; | ||
} | ||
else if (contract) { | ||
var entry = promiseEntry(_this.ethereumResolver.resolve(contract, contractDirectives, fieldName, args, info.directives)); | ||
promises.push(entry.promise); | ||
return entry; | ||
} | ||
return ( | ||
// Support nested fields | ||
(aliasNeeded ? aliasedNode : preAliasingNode) || | ||
{}); | ||
}; | ||
var resolverAfter = function (fieldName, rootValue, args, context, info) { | ||
if (rootValue === void 0) { rootValue = {}; } | ||
if (info.directives && info.directives.contract) { | ||
contract = null; | ||
} | ||
}; | ||
return { | ||
resolver: resolver, | ||
resolverAfter: resolverAfter | ||
}; | ||
}; | ||
if (server) | ||
operation.query = server; | ||
var obs = server && forward | ||
var nonContractQuery = removeContractSetsFromDocument(query); | ||
var defn = apolloUtilities.getMainDefinition(query); | ||
var isSubscription = defn.operation === 'subscription'; | ||
if (nonContractQuery) | ||
operation.query = nonContractQuery; | ||
var obs = nonContractQuery && forward | ||
? forward(operation) | ||
@@ -282,2 +320,3 @@ : apolloLink.Observable.of({ | ||
return new apolloLink.Observable(function (observer) { | ||
// console.log('Query operation name: ', operation.operationName) | ||
// Works around race condition between completion and graphql execution | ||
@@ -290,7 +329,8 @@ // finishing. If complete is called during the graphql call, we will | ||
obs.subscribe({ | ||
next: function (_a) { | ||
var data = _a.data, errors = _a.errors; | ||
next: function (args) { | ||
// console.trace() | ||
var data = args.data, errors = args.errors; | ||
console.log('NEXT!', args); | ||
var context = operation.getContext(); | ||
var promises = []; | ||
var _b = resolverFactory(promises), resolver = _b.resolver, resolverAfter = _b.resolverAfter; | ||
var _a = resolverFactory(_this.ethereumResolver, isSubscription), resolver = _a.resolver, resolverAfter = _a.resolverAfter, promises = _a.promises, subscriptions = _a.subscriptions; | ||
handlingNext = true; | ||
@@ -313,9 +353,26 @@ //data is from the server and provides the root value to this GraphQL resolution | ||
} | ||
Promise | ||
.all(promises) | ||
.then(promiseFinally) | ||
.catch(promiseFinally); | ||
if (isSubscription) { | ||
subscriptions.forEach(function (subscription) { | ||
console.log('subscribing...'); | ||
subscription.subscribe({ | ||
next: function (_) { | ||
observer.next({ | ||
data: nextData | ||
}); | ||
} | ||
}); | ||
}); | ||
} | ||
else { | ||
Promise | ||
.all(promises) | ||
.then(promiseFinally) | ||
.catch(promiseFinally); | ||
} | ||
}, | ||
error: observerErrorHandler, | ||
complete: function () { | ||
if (isSubscription) { | ||
return; | ||
} | ||
if (!handlingNext) { | ||
@@ -327,2 +384,5 @@ observer.complete(); | ||
}); | ||
return function () { | ||
observer.complete(); | ||
}; | ||
}); | ||
@@ -329,0 +389,0 @@ }; |
@@ -15,6 +15,7 @@ var __extends = (this && this.__extends) || (function () { | ||
import { Observable, ApolloLink } from 'apollo-link'; | ||
import { hasDirectives, } from 'apollo-utilities'; | ||
import { hasDirectives, getMainDefinition } from 'apollo-utilities'; | ||
import { removeContractSetsFromDocument } from './removeContractSetsFromDocument'; | ||
import { graphql } from './graphql-anywhere/graphql'; | ||
import { resolvePromises, promiseEntry } from './resolvePromises'; | ||
import { resolvePromises } from './resolvePromises'; | ||
import { resolverFactory } from './resolverFactory'; | ||
// Using some code taken from https://github.com/apollographql/apollo-link-state/blob/master/packages/apollo-link-state/src/index.ts | ||
@@ -35,46 +36,8 @@ var ContractLink = /** @class */ (function (_super) { | ||
} | ||
var server = removeContractSetsFromDocument(query); | ||
var resolverFactory = function (promises) { | ||
var contract = null; | ||
var contractDirectives = null; | ||
var resolver = function (fieldName, rootValue, args, context, info) { | ||
if (rootValue === void 0) { rootValue = {}; } | ||
var resultKey = info.resultKey; | ||
var aliasedNode = rootValue[resultKey]; // where data is stored for user aliases | ||
var preAliasingNode = rootValue[fieldName]; // where data is stored in canonical model | ||
var aliasNeeded = resultKey !== fieldName; | ||
// if data already exists, return it! | ||
if (aliasedNode !== undefined || preAliasingNode !== undefined) { | ||
return aliasedNode || preAliasingNode; | ||
} | ||
// otherwise, run the web3 resolver | ||
// debug(`resolver: `, fieldName, rootValue, args, context, info, contract) | ||
if (info.directives && info.directives.hasOwnProperty('contract')) { | ||
contract = fieldName; | ||
contractDirectives = info.directives.contract; | ||
} | ||
else if (contract) { | ||
var entry = promiseEntry(_this.ethereumResolver.resolve(contract, contractDirectives, fieldName, args, info.directives)); | ||
promises.push(entry.promise); | ||
return entry; | ||
} | ||
return ( | ||
// Support nested fields | ||
(aliasNeeded ? aliasedNode : preAliasingNode) || | ||
{}); | ||
}; | ||
var resolverAfter = function (fieldName, rootValue, args, context, info) { | ||
if (rootValue === void 0) { rootValue = {}; } | ||
if (info.directives && info.directives.contract) { | ||
contract = null; | ||
} | ||
}; | ||
return { | ||
resolver: resolver, | ||
resolverAfter: resolverAfter | ||
}; | ||
}; | ||
if (server) | ||
operation.query = server; | ||
var obs = server && forward | ||
var nonContractQuery = removeContractSetsFromDocument(query); | ||
var defn = getMainDefinition(query); | ||
var isSubscription = defn.operation === 'subscription'; | ||
if (nonContractQuery) | ||
operation.query = nonContractQuery; | ||
var obs = nonContractQuery && forward | ||
? forward(operation) | ||
@@ -85,2 +48,3 @@ : Observable.of({ | ||
return new Observable(function (observer) { | ||
// console.log('Query operation name: ', operation.operationName) | ||
// Works around race condition between completion and graphql execution | ||
@@ -93,7 +57,8 @@ // finishing. If complete is called during the graphql call, we will | ||
obs.subscribe({ | ||
next: function (_a) { | ||
var data = _a.data, errors = _a.errors; | ||
next: function (args) { | ||
// console.trace() | ||
var data = args.data, errors = args.errors; | ||
console.log('NEXT!', args); | ||
var context = operation.getContext(); | ||
var promises = []; | ||
var _b = resolverFactory(promises), resolver = _b.resolver, resolverAfter = _b.resolverAfter; | ||
var _a = resolverFactory(_this.ethereumResolver, isSubscription), resolver = _a.resolver, resolverAfter = _a.resolverAfter, promises = _a.promises, subscriptions = _a.subscriptions; | ||
handlingNext = true; | ||
@@ -116,9 +81,26 @@ //data is from the server and provides the root value to this GraphQL resolution | ||
} | ||
Promise | ||
.all(promises) | ||
.then(promiseFinally) | ||
.catch(promiseFinally); | ||
if (isSubscription) { | ||
subscriptions.forEach(function (subscription) { | ||
console.log('subscribing...'); | ||
subscription.subscribe({ | ||
next: function (_) { | ||
observer.next({ | ||
data: nextData | ||
}); | ||
} | ||
}); | ||
}); | ||
} | ||
else { | ||
Promise | ||
.all(promises) | ||
.then(promiseFinally) | ||
.catch(promiseFinally); | ||
} | ||
}, | ||
error: observerErrorHandler, | ||
complete: function () { | ||
if (isSubscription) { | ||
return; | ||
} | ||
if (!handlingNext) { | ||
@@ -130,2 +112,5 @@ observer.complete(); | ||
}); | ||
return function () { | ||
observer.complete(); | ||
}; | ||
}); | ||
@@ -132,0 +117,0 @@ }; |
@@ -0,3 +1,5 @@ | ||
import { Observable, FetchResult } from 'apollo-link'; | ||
export interface EthereumResolver { | ||
resolve(contractName: any, contractDirectives: any, fieldName: any, fieldArgs: any, fieldDirectives: any): Promise<any>; | ||
subscribe(contractName: any, contractDirectives: any, fieldName: any, fieldArgs: any, fieldDirectives: any): Observable<FetchResult>; | ||
} |
@@ -25,2 +25,5 @@ export function promiseEntry(promise) { | ||
var value = object[key]; | ||
if (!value) { | ||
return accumulator; | ||
} | ||
if (value.promise && value.promise instanceof Promise) { | ||
@@ -27,0 +30,0 @@ if (value.error) { |
{ | ||
"name": "apollo-link-ethereum", | ||
"version": "0.0.10", | ||
"version": "0.0.11", | ||
"main": "./dist/bundle.umd.js", | ||
@@ -34,3 +34,3 @@ "modules": "./lib/index.js", | ||
}, | ||
"gitHead": "96014e16411941e51860121e98be5fd4c9eb2e6e" | ||
"gitHead": "3db8eb1f5dab2b4cc99a5072e48c4efa75ad1ed0" | ||
} |
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
491212
39
988