apollo-link
Advanced tools
Comparing version 0.6.1-beta.4 to 0.6.1-beta.5
(function (global, factory) { | ||
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('zen-observable-ts'), require('apollo-utilities')) : | ||
typeof define === 'function' && define.amd ? define(['exports', 'zen-observable-ts', 'apollo-utilities'], factory) : | ||
(factory((global.apolloLinkCore = {}),global.Observable,global.apolloUtilities)); | ||
}(this, (function (exports,Observable,apolloUtilities) { 'use strict'; | ||
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('zen-observable-ts'), require('apollo-utilities'), require('graphql/language/printer')) : | ||
typeof define === 'function' && define.amd ? define(['exports', 'zen-observable-ts', 'apollo-utilities', 'graphql/language/printer'], factory) : | ||
(factory((global.apolloLinkCore = {}),global.Observable,global.apolloUtilities,global.graphql_language_printer)); | ||
}(this, (function (exports,Observable,apolloUtilities,graphql_language_printer) { 'use strict'; | ||
@@ -19,3 +19,3 @@ var Observable__default = Observable['default']; | ||
})(); | ||
var __assign$1 = (undefined && undefined.__assign) || Object.assign || function(t) { | ||
var __assign = (undefined && undefined.__assign) || Object.assign || function(t) { | ||
for (var s, i = 1, n = arguments.length; i < n; i++) { | ||
@@ -33,4 +33,4 @@ s = arguments[i]; | ||
'variables', | ||
'extensions', | ||
'context', | ||
'extensions', | ||
]; | ||
@@ -77,3 +77,8 @@ if (!operation.query) | ||
function transformOperation(operation) { | ||
var transformedOperation = __assign$1({}, operation); | ||
var transformedOperation = { | ||
variables: operation.variables || {}, | ||
extensions: operation.extensions || {}, | ||
operationName: operation.operationName, | ||
query: operation.query, | ||
}; | ||
if (!transformedOperation.operationName) { | ||
@@ -87,11 +92,31 @@ transformedOperation.operationName = | ||
} | ||
function createOperation(starting, operation) { | ||
var context = __assign({}, starting); | ||
var setContext = function (next) { | ||
if (typeof next === 'function') { | ||
context = next(context); | ||
} | ||
else { | ||
context = __assign({}, next); | ||
} | ||
}; | ||
var getContext = function () { return (__assign({}, context)); }; | ||
Object.defineProperty(operation, 'setContext', { | ||
enumerable: false, | ||
value: setContext, | ||
}); | ||
Object.defineProperty(operation, 'getContext', { | ||
enumerable: false, | ||
value: getContext, | ||
}); | ||
Object.defineProperty(operation, 'toKey', { | ||
enumerable: false, | ||
value: function () { return getKey(operation); }, | ||
}); | ||
return operation; | ||
} | ||
function getKey(operation) { | ||
return graphql_language_printer.print(operation.query) + "|" + JSON.stringify(operation.variables) + "|" + operation.operationName; | ||
} | ||
var __assign = (undefined && undefined.__assign) || Object.assign || function(t) { | ||
for (var s, i = 1, n = arguments.length; i < n; i++) { | ||
s = arguments[i]; | ||
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) | ||
t[p] = s[p]; | ||
} | ||
return t; | ||
}; | ||
var passthrough = function (op, forward) { return (forward ? forward(op) : Observable__default.of()); }; | ||
@@ -169,5 +194,3 @@ var toLink = function (handler) { | ||
function execute(link, operation) { | ||
var copy = __assign({ context: {}, variables: {}, extensions: {} }, operation); | ||
validateOperation(copy); | ||
return link.request(transformOperation(copy)) || Observable__default.of(); | ||
return (link.request(createOperation(operation.context, transformOperation(validateOperation(operation)))) || Observable__default.of()); | ||
} | ||
@@ -174,0 +197,0 @@ |
@@ -5,3 +5,3 @@ import Observable from 'zen-observable-ts'; | ||
export declare const from: (links: ApolloLink[]) => ApolloLink; | ||
export declare const split: (test: (op: GraphQLRequest) => boolean, left: ApolloLink | RequestHandler, right?: ApolloLink | RequestHandler) => ApolloLink; | ||
export declare const split: (test: (op: Operation) => boolean, left: ApolloLink | RequestHandler, right?: ApolloLink | RequestHandler) => ApolloLink; | ||
export declare const concat: (first: ApolloLink | RequestHandler, second: ApolloLink | RequestHandler) => ApolloLink; | ||
@@ -12,3 +12,3 @@ export declare class ApolloLink { | ||
static from: (links: ApolloLink[]) => ApolloLink; | ||
static split: (test: (op: GraphQLRequest) => boolean, left: ApolloLink | RequestHandler, right?: ApolloLink | RequestHandler) => ApolloLink; | ||
static split: (test: (op: Operation) => boolean, left: ApolloLink | RequestHandler, right?: ApolloLink | RequestHandler) => ApolloLink; | ||
split(test: (op: Operation) => boolean, left: ApolloLink | RequestHandler, right?: ApolloLink | RequestHandler): ApolloLink; | ||
@@ -15,0 +15,0 @@ concat(next: ApolloLink | RequestHandler): ApolloLink; |
@@ -1,11 +0,3 @@ | ||
var __assign = (this && this.__assign) || Object.assign || function(t) { | ||
for (var s, i = 1, n = arguments.length; i < n; i++) { | ||
s = arguments[i]; | ||
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) | ||
t[p] = s[p]; | ||
} | ||
return t; | ||
}; | ||
import Observable from 'zen-observable-ts'; | ||
import { validateOperation, isTerminating, LinkError, transformOperation, } from './linkUtils'; | ||
import { validateOperation, isTerminating, LinkError, transformOperation, createOperation, } from './linkUtils'; | ||
var passthrough = function (op, forward) { return (forward ? forward(op) : Observable.of()); }; | ||
@@ -84,6 +76,4 @@ var toLink = function (handler) { | ||
export function execute(link, operation) { | ||
var copy = __assign({ context: {}, variables: {}, extensions: {} }, operation); | ||
validateOperation(copy); | ||
return link.request(transformOperation(copy)) || Observable.of(); | ||
return (link.request(createOperation(operation.context, transformOperation(validateOperation(operation)))) || Observable.of()); | ||
} | ||
//# sourceMappingURL=link.js.map |
@@ -11,2 +11,4 @@ import Observable from 'zen-observable-ts'; | ||
export declare function makePromise<R>(observable: Observable<R>): Promise<R>; | ||
export declare function transformOperation(operation: GraphQLRequest): Operation; | ||
export declare function transformOperation(operation: GraphQLRequest): GraphQLRequest; | ||
export declare function createOperation(starting: any, operation: GraphQLRequest): Operation; | ||
export declare function getKey(operation: GraphQLRequest): string; |
@@ -20,2 +20,3 @@ var __extends = (this && this.__extends) || (function () { | ||
import { getOperationName } from 'apollo-utilities'; | ||
import { print } from 'graphql/language/printer'; | ||
export function validateOperation(operation) { | ||
@@ -26,4 +27,4 @@ var OPERATION_FIELDS = [ | ||
'variables', | ||
'extensions', | ||
'context', | ||
'extensions', | ||
]; | ||
@@ -71,3 +72,8 @@ if (!operation.query) | ||
export function transformOperation(operation) { | ||
var transformedOperation = __assign({}, operation); | ||
var transformedOperation = { | ||
variables: operation.variables || {}, | ||
extensions: operation.extensions || {}, | ||
operationName: operation.operationName, | ||
query: operation.query, | ||
}; | ||
if (!transformedOperation.operationName) { | ||
@@ -81,2 +87,30 @@ transformedOperation.operationName = | ||
} | ||
export function createOperation(starting, operation) { | ||
var context = __assign({}, starting); | ||
var setContext = function (next) { | ||
if (typeof next === 'function') { | ||
context = next(context); | ||
} | ||
else { | ||
context = __assign({}, next); | ||
} | ||
}; | ||
var getContext = function () { return (__assign({}, context)); }; | ||
Object.defineProperty(operation, 'setContext', { | ||
enumerable: false, | ||
value: setContext, | ||
}); | ||
Object.defineProperty(operation, 'getContext', { | ||
enumerable: false, | ||
value: getContext, | ||
}); | ||
Object.defineProperty(operation, 'toKey', { | ||
enumerable: false, | ||
value: function () { return getKey(operation); }, | ||
}); | ||
return operation; | ||
} | ||
export function getKey(operation) { | ||
return print(operation.query) + "|" + JSON.stringify(operation.variables) + "|" + operation.operationName; | ||
} | ||
//# sourceMappingURL=linkUtils.js.map |
@@ -21,6 +21,3 @@ var __extends = (this && this.__extends) || (function () { | ||
SetContextLink.prototype.request = function (operation, forward) { | ||
if (!operation.context) { | ||
operation.context = {}; | ||
} | ||
operation.context = this.setContext(operation.context); | ||
operation.setContext(this.setContext(operation.getContext())); | ||
return forward(operation); | ||
@@ -27,0 +24,0 @@ }; |
@@ -0,3 +1,4 @@ | ||
import gql from 'graphql-tag'; | ||
import { execute } from '../link'; | ||
var sampleQuery = "\nquery SampleQuery {\n stub{\n id\n }\n}\n"; | ||
var sampleQuery = (_a = ["\n query SampleQuery {\n stub {\n id\n }\n }\n"], _a.raw = ["\n query SampleQuery {\n stub {\n id\n }\n }\n"], gql(_a)); | ||
export function checkCalls(calls, results) { | ||
@@ -31,2 +32,3 @@ if (calls === void 0) { calls = []; } | ||
} | ||
var _a; | ||
//# sourceMappingURL=testingUtils.js.map |
import Observable from 'zen-observable-ts'; | ||
import { ExecutionResult, DocumentNode } from 'graphql'; | ||
export interface GraphQLRequest { | ||
query: string | DocumentNode; | ||
query: DocumentNode; | ||
variables?: Record<string, any>; | ||
@@ -10,3 +10,11 @@ operationName?: string; | ||
} | ||
export declare type Operation = GraphQLRequest; | ||
export interface Operation { | ||
query: DocumentNode; | ||
variables: Record<string, any>; | ||
operationName: string; | ||
extensions: Record<string, any>; | ||
setContext: (context: Record<string, any>) => Record<string, any>; | ||
getContext: () => Record<string, any>; | ||
toKey: () => string; | ||
} | ||
export declare type FetchResult<C = Record<string, any>, E = Record<string, any>> = ExecutionResult & { | ||
@@ -13,0 +21,0 @@ extensions?: E; |
{ | ||
"name": "apollo-link", | ||
"version": "0.6.1-beta.4", | ||
"version": "0.6.1-beta.5", | ||
"description": "Flexible, lightweight transport layer for GraphQL", | ||
@@ -26,3 +26,4 @@ "author": "Evans Hauser <evanshauser@gmail.com>", | ||
"scripts": { | ||
"build:browser": "browserify ./lib/bundle.umd.js -o=./lib/bundle.js --i apollo-utilities --i zen-observable-ts && npm run minify:browser", | ||
"build:browser": | ||
"browserify ./lib/bundle.umd.js -o=./lib/bundle.js --i apollo-utilities --i graphql --i zen-observable-ts && npm run minify:browser", | ||
"build": "tsc -p .", | ||
@@ -32,4 +33,6 @@ "bundle": "rollup -c", | ||
"filesize": "npm run build && npm run build:browser", | ||
"lint": "tslint --type-check -p tsconfig.json -c ../../tslint.json src/*.ts", | ||
"minify:browser": "uglifyjs -c -m -o ./lib/bundle.min.js -- ./lib/bundle.js", | ||
"lint": | ||
"tslint --type-check -p tsconfig.json -c ../../tslint.json src/*.ts", | ||
"minify:browser": | ||
"uglifyjs -c -m -o ./lib/bundle.min.js -- ./lib/bundle.js", | ||
"postbuild": "npm run bundle", | ||
@@ -43,3 +46,4 @@ "prebuild": "npm run clean", | ||
"apollo-utilities": "^0.2.0-beta.0", | ||
"zen-observable-ts": "^0.4.3-beta.3" | ||
"graphql": "^0.11.3", | ||
"zen-observable-ts": "^0.4.3-beta.4" | ||
}, | ||
@@ -50,3 +54,2 @@ "devDependencies": { | ||
"browserify": "^14.4.0", | ||
"graphql": "^0.11.3", | ||
"graphql-tag": "^2.4.2", | ||
@@ -66,9 +69,4 @@ "jest": "^21.1.0", | ||
"testRegex": "(/__tests__/.*|\\.(test|spec))\\.(ts|tsx|js)$", | ||
"moduleFileExtensions": [ | ||
"ts", | ||
"tsx", | ||
"js", | ||
"json" | ||
] | ||
"moduleFileExtensions": ["ts", "tsx", "js", "json"] | ||
} | ||
} |
import Observable from 'zen-observable-ts'; | ||
import gql from 'graphql-tag'; | ||
import { print } from 'graphql/language/printer'; | ||
@@ -22,5 +23,5 @@ import { execute, ApolloLink, from, split, concat } from '../link'; | ||
const returnOne = new SetContextLink(setContext); | ||
const link = returnOne.concat((operation, forward) => | ||
Observable.of({ data: { count: operation.context.add } }), | ||
); | ||
const link = returnOne.concat((operation, forward) => { | ||
return Observable.of({ data: { count: operation.getContext().add } }); | ||
}); | ||
@@ -36,3 +37,5 @@ testLinkResults({ | ||
const returnOne = new SetContextLink(setContext); | ||
const mock = new MockLink(op => Observable.of({ data: op.context.add })); | ||
const mock = new MockLink(op => | ||
Observable.of({ data: op.getContext().add }), | ||
); | ||
const link = returnOne.concat(mock); | ||
@@ -53,3 +56,3 @@ | ||
new Observable(observer => { | ||
observer.next({ data: op.context.add }); | ||
observer.next({ data: op.getContext().add }); | ||
observer.error(error); | ||
@@ -70,12 +73,7 @@ }), | ||
const mock = new MockLink((op, forward) => { | ||
let _op = { | ||
...op, | ||
context: { | ||
add: op.context.add + 2, | ||
}, | ||
}; | ||
return forward(_op); | ||
op.setContext(({ add }) => ({ add: add + 2 })); | ||
return forward(op); | ||
}); | ||
const link = returnOne.concat(mock).concat(op => { | ||
return Observable.of({ data: op.context.add }); | ||
return Observable.of({ data: op.getContext().add }); | ||
}); | ||
@@ -93,3 +91,3 @@ | ||
const mock = new MockLink((op, forward) => | ||
Observable.of({ data: op.context.add }), | ||
Observable.of({ data: op.getContext().add }), | ||
); | ||
@@ -99,8 +97,5 @@ | ||
.concat((operation, forward) => { | ||
operation = { | ||
...operation, | ||
context: { | ||
add: operation.context.add + 2, | ||
}, | ||
}; | ||
operation.setContext({ | ||
add: operation.getContext().add + 2, | ||
}); | ||
return forward(operation); | ||
@@ -120,11 +115,8 @@ }) | ||
.concat((operation, forward) => { | ||
operation = { | ||
...operation, | ||
context: { | ||
add: operation.context.add + 2, | ||
}, | ||
}; | ||
operation.setContext({ | ||
add: operation.getContext().add + 2, | ||
}); | ||
return forward(operation); | ||
}) | ||
.concat((op, forward) => Observable.of({ data: op.context.add })); | ||
.concat((op, forward) => Observable.of({ data: op.getContext().add })); | ||
testLinkResults({ | ||
@@ -140,12 +132,9 @@ link, | ||
const mock1 = new MockLink((operation, forward) => { | ||
operation = { | ||
...operation, | ||
context: { | ||
add: operation.context.add + 2, | ||
}, | ||
}; | ||
operation.setContext({ | ||
add: operation.getContext().add + 2, | ||
}); | ||
return forward(operation); | ||
}); | ||
const mock2 = new MockLink((op, forward) => | ||
Observable.of({ data: op.context.add }), | ||
Observable.of({ data: op.getContext().add }), | ||
); | ||
@@ -164,15 +153,12 @@ | ||
const mock1 = new MockLink((operation, forward) => { | ||
operation = { | ||
...operation, | ||
context: { | ||
add: operation.context.add + 2, | ||
}, | ||
}; | ||
operation.setContext({ | ||
add: operation.getContext().add + 2, | ||
}); | ||
return forward(operation); | ||
}); | ||
const mock2 = new MockLink((op, forward) => | ||
Observable.of({ data: op.context.add + 2 }), | ||
Observable.of({ data: op.getContext().add + 2 }), | ||
); | ||
const mock3 = new MockLink((op, forward) => | ||
Observable.of({ data: op.context.add + 3 }), | ||
Observable.of({ data: op.getContext().add + 3 }), | ||
); | ||
@@ -198,9 +184,9 @@ const link = returnOne.concat(mock1); | ||
const link1 = returnOne.concat((operation, forward) => | ||
Observable.of({ data: operation.context.add + 1 }), | ||
Observable.of({ data: operation.getContext().add + 1 }), | ||
); | ||
const link2 = returnOne.concat((operation, forward) => | ||
Observable.of({ data: operation.context.add + 2 }), | ||
Observable.of({ data: operation.getContext().add + 2 }), | ||
); | ||
const link = returnOne.split( | ||
operation => operation.context.add === 1, | ||
operation => operation.getContext().add === 1, | ||
link1, | ||
@@ -229,3 +215,3 @@ link2, | ||
new MockLink((operation, forward) => | ||
Observable.of({ data: operation.context.add + 1 }), | ||
Observable.of({ data: operation.getContext().add + 1 }), | ||
), | ||
@@ -235,7 +221,7 @@ ); | ||
new MockLink((operation, forward) => | ||
Observable.of({ data: operation.context.add + 2 }), | ||
Observable.of({ data: operation.getContext().add + 2 }), | ||
), | ||
); | ||
const link = returnOne.split( | ||
operation => operation.context.add === 1, | ||
operation => operation.getContext().add === 1, | ||
link1, | ||
@@ -263,11 +249,11 @@ link2, | ||
const link1 = returnOne.concat((operation, forward) => | ||
Observable.of({ data: operation.context.add + 1 }), | ||
Observable.of({ data: operation.getContext().add + 1 }), | ||
); | ||
const link2 = returnOne.concat( | ||
new MockLink((operation, forward) => | ||
Observable.of({ data: operation.context.add + 2 }), | ||
Observable.of({ data: operation.getContext().add + 2 }), | ||
), | ||
); | ||
const link = returnOne.split( | ||
operation => operation.context.add === 1, | ||
operation => operation.getContext().add === 1, | ||
link1, | ||
@@ -296,13 +282,15 @@ link2, | ||
.split( | ||
operation => operation.context.test, | ||
operation => operation.getContext().test, | ||
(operation, forward) => { | ||
operation.context.add++; | ||
operation.setContext(({ add }) => ({ add: add + 1 })); | ||
return forward(operation); | ||
}, | ||
(operation, forward) => { | ||
operation.context.add += 2; | ||
operation.setContext(({ add }) => ({ add: add + 2 })); | ||
return forward(operation); | ||
}, | ||
) | ||
.concat(operation => Observable.of({ data: operation.context.add })); | ||
.concat(operation => | ||
Observable.of({ data: operation.getContext().add }), | ||
); | ||
@@ -329,3 +317,3 @@ testLinkResults({ | ||
const link = start.split( | ||
operation => operation.context.test, | ||
operation => operation.getContext().test, | ||
operation => | ||
@@ -424,20 +412,2 @@ Observable.of({ | ||
it('should accept sting query and pass string to link', () => { | ||
const operation1 = { | ||
...uniqueOperation, | ||
query: '1234', | ||
}; | ||
const operation2 = { | ||
...uniqueOperation, | ||
query: '1234', | ||
}; | ||
const stub = jest.fn(); | ||
const chain = ApolloLink.from([new MockLink(stub)]); | ||
execute(chain, operation1); | ||
expect(stub).toBeCalledWith({ ...operation2, variables: {} }); | ||
}); | ||
it('should accept AST query and pass AST to link', () => { | ||
@@ -454,3 +424,8 @@ const astOperation = { | ||
expect(stub).toBeCalledWith({ ...astOperation, variables: {} }); | ||
expect(stub).toBeCalledWith({ | ||
query: sampleQuery, | ||
operationName: 'SampleQuery', | ||
variables: {}, | ||
extensions: {}, | ||
}); | ||
}); | ||
@@ -468,3 +443,4 @@ | ||
expect({ | ||
...uniqueOperation, | ||
extensions: {}, | ||
operationName: 'SampleQuery', | ||
query: sampleQuery, | ||
@@ -556,8 +532,8 @@ variables: {}, | ||
it('should chain together a function with links', done => { | ||
const add1 = (operation: Operation, forward: NextLink) => { | ||
operation.context.num++; | ||
const add1 = new ApolloLink((operation: Operation, forward: NextLink) => { | ||
operation.setContext(({ num }) => ({ num: num + 1 })); | ||
return forward(operation); | ||
}; | ||
}); | ||
const add1Link = new MockLink((operation, forward) => { | ||
operation.context.num++; | ||
operation.setContext(({ num }) => ({ num: num + 1 })); | ||
return forward(operation); | ||
@@ -572,3 +548,5 @@ }); | ||
add1Link, | ||
operation => Observable.of({ data: operation.context }), | ||
new ApolloLink(operation => | ||
Observable.of({ data: operation.getContext() }), | ||
), | ||
]); | ||
@@ -587,3 +565,3 @@ testLinkResults({ | ||
const link = split( | ||
operation => operation.context.test, | ||
operation => operation.getContext().test, | ||
(operation, forward) => Observable.of({ data: { count: 1 } }), | ||
@@ -612,3 +590,3 @@ ); | ||
const link = ApolloLink.split( | ||
operation => operation.context.test, | ||
operation => operation.getContext().test, | ||
(operation, forward) => Observable.of({ data: { count: 1 } }), | ||
@@ -638,3 +616,3 @@ (operation, forward) => Observable.of({ data: { count: 2 } }), | ||
const link = ApolloLink.split( | ||
operation => operation.context.test, | ||
operation => operation.getContext().test, | ||
(operation, forward) => Observable.of({ data: { count: 1 } }), | ||
@@ -666,3 +644,3 @@ new MockLink((operation, forward) => | ||
const link = ApolloLink.split( | ||
operation => operation.context.test, | ||
operation => operation.getContext().test, | ||
(operation, forward) => Observable.of({ data: { count: 1 } }), | ||
@@ -695,3 +673,3 @@ new MockLink((operation, forward) => | ||
const link = ApolloLink.split( | ||
operation => operation.context.test, | ||
operation => operation.getContext().test, | ||
(operation, forward) => | ||
@@ -722,3 +700,3 @@ forward(operation).map(data => ({ | ||
const link = ApolloLink.split( | ||
operation => operation.context.test, | ||
operation => operation.getContext().test, | ||
operation => Observable.of({ data: { count: 2 } }), | ||
@@ -788,3 +766,3 @@ ).concat(operation => Observable.of({ data: { count: 1 } })); | ||
const link = new SetContextLink(() => context).split( | ||
op => op.context.test, | ||
op => op.getContext().test, | ||
() => Observable.of(), | ||
@@ -806,16 +784,21 @@ () => null, | ||
it('should set a default context, variable, query and operationName on a copy of operation', done => { | ||
const operation = { query: '1234' }; | ||
const link = ApolloLink.from([ | ||
op => { | ||
expect(operation['operationName']).toBeUndefined(); | ||
expect(operation['variables']).toBeUndefined(); | ||
expect(operation['context']).toBeUndefined(); | ||
expect(operation['extensions']).toBeUndefined(); | ||
expect(op['operationName']).toBeDefined(); | ||
expect(op['variables']).toBeDefined(); | ||
expect(op['context']).toBeDefined(); | ||
expect(op['extensions']).toBeDefined(); | ||
return Observable.of(); | ||
}, | ||
]); | ||
const operation = { | ||
query: gql` | ||
{ | ||
id | ||
} | ||
`, | ||
}; | ||
const link = new ApolloLink(op => { | ||
expect(operation['operationName']).toBeUndefined(); | ||
expect(operation['variables']).toBeUndefined(); | ||
expect(operation['context']).toBeUndefined(); | ||
expect(operation['extensions']).toBeUndefined(); | ||
expect(op['operationName']).toBeDefined(); | ||
expect(op['variables']).toBeDefined(); | ||
expect(op['context']).toBeUndefined(); | ||
expect(op['extensions']).toBeDefined(); | ||
expect(op.toKey()).toBeDefined(); | ||
return Observable.of(); | ||
}); | ||
@@ -938,5 +921,5 @@ execute(link, operation).subscribe({ | ||
ApolloLink.from([ | ||
(operation, forward) => forward(operation), | ||
operation => Observable.of({ data }), | ||
(operation, forward) => forward(operation), | ||
new ApolloLink((operation, forward) => forward(operation)), | ||
new ApolloLink(operation => Observable.of({ data })), | ||
new ApolloLink((operation, forward) => forward(operation)), | ||
]); | ||
@@ -953,9 +936,9 @@ expect(warningStub).toHaveBeenCalledTimes(1); | ||
ApolloLink.from([ | ||
(operation, forward) => forward(operation), | ||
(operation, forward) => forward(operation), | ||
new ApolloLink((operation, forward) => forward(operation)), | ||
new ApolloLink((operation, forward) => forward(operation)), | ||
terminatingLink, | ||
(operation, forward) => forward(operation), | ||
(operation, forward) => forward(operation), | ||
operation => Observable.of({ data }), | ||
(operation, forward) => forward(operation), | ||
new ApolloLink((operation, forward) => forward(operation)), | ||
new ApolloLink((operation, forward) => forward(operation)), | ||
new ApolloLink(operation => Observable.of({ data })), | ||
new ApolloLink((operation, forward) => forward(operation)), | ||
]); | ||
@@ -966,1 +949,33 @@ expect(warningStub).toHaveBeenCalledTimes(4); | ||
}); | ||
describe('execute', () => { | ||
it('transforms an opearation with context into something serlizable', done => { | ||
const query = gql` | ||
{ | ||
id | ||
} | ||
`; | ||
const link = new ApolloLink(operation => { | ||
const str = JSON.stringify({ | ||
...operation, | ||
query: print(operation.query), | ||
}); | ||
expect(str).toBe( | ||
JSON.stringify({ | ||
variables: { id: 1 }, | ||
extensions: { cache: true }, | ||
operationName: null, | ||
query: print(operation.query), | ||
}), | ||
); | ||
return Observable.of(); | ||
}); | ||
const noop = () => {}; | ||
execute(link, { | ||
query, | ||
variables: { id: 1 }, | ||
extensions: { cache: true }, | ||
}).subscribe(noop, noop, done); | ||
}); | ||
}); |
@@ -16,2 +16,3 @@ import Observable from 'zen-observable-ts'; | ||
transformOperation, | ||
createOperation, | ||
} from './linkUtils'; | ||
@@ -92,4 +93,2 @@ | ||
// XXX add in type support for context manipulation | ||
// XXX remove mutability of context | ||
export class ApolloLink { | ||
@@ -128,6 +127,10 @@ constructor(request?: RequestHandler) { | ||
): Observable<FetchResult> { | ||
const copy = { context: {}, variables: {}, extensions: {}, ...operation }; | ||
validateOperation(copy); | ||
return link.request(transformOperation(copy)) || Observable.of(); | ||
return ( | ||
link.request( | ||
createOperation( | ||
operation.context, | ||
transformOperation(validateOperation(operation)), | ||
), | ||
) || Observable.of() | ||
); | ||
} |
import { getOperationName } from 'apollo-utilities'; | ||
import Observable from 'zen-observable-ts'; | ||
import { print } from 'graphql/language/printer'; | ||
@@ -12,4 +13,4 @@ import { GraphQLRequest, Operation } from './types'; | ||
'variables', | ||
'extensions', | ||
'context', | ||
'extensions', | ||
]; | ||
@@ -57,4 +58,9 @@ if (!operation.query) throw new Error('ApolloLink requires a query'); | ||
export function transformOperation(operation: GraphQLRequest): Operation { | ||
const transformedOperation: Operation = { ...operation }; | ||
export function transformOperation(operation: GraphQLRequest): GraphQLRequest { | ||
const transformedOperation: GraphQLRequest = { | ||
variables: operation.variables || {}, | ||
extensions: operation.extensions || {}, | ||
operationName: operation.operationName, | ||
query: operation.query, | ||
}; | ||
@@ -71,1 +77,41 @@ // best guess at an operation name | ||
} | ||
export function createOperation( | ||
starting: any, | ||
operation: GraphQLRequest, | ||
): Operation { | ||
let context = { ...starting }; | ||
const setContext = next => { | ||
if (typeof next === 'function') { | ||
context = next(context); | ||
} else { | ||
context = { ...next }; | ||
} | ||
}; | ||
const getContext = () => ({ ...context }); | ||
Object.defineProperty(operation, 'setContext', { | ||
enumerable: false, | ||
value: setContext, | ||
}); | ||
Object.defineProperty(operation, 'getContext', { | ||
enumerable: false, | ||
value: getContext, | ||
}); | ||
Object.defineProperty(operation, 'toKey', { | ||
enumerable: false, | ||
value: () => getKey(operation), | ||
}); | ||
return operation as Operation; | ||
} | ||
export function getKey(operation: GraphQLRequest) { | ||
// XXX we're assuming here that variables will be serialized in the same order. | ||
// that might not always be true | ||
return `${print(operation.query)}|${JSON.stringify( | ||
operation.variables, | ||
)}|${operation.operationName}`; | ||
} |
@@ -20,8 +20,5 @@ import { Operation, NextLink, FetchResult } from '../types'; | ||
): Observable<FetchResult> { | ||
if (!operation.context) { | ||
operation.context = {}; | ||
} | ||
operation.context = this.setContext(operation.context); | ||
operation.setContext(this.setContext(operation.getContext())); | ||
return forward(operation); | ||
} | ||
} |
@@ -0,9 +1,10 @@ | ||
import gql from 'graphql-tag'; | ||
import { execute, ApolloLink } from '../link'; | ||
const sampleQuery = ` | ||
query SampleQuery { | ||
stub{ | ||
id | ||
const sampleQuery = gql` | ||
query SampleQuery { | ||
stub { | ||
id | ||
} | ||
} | ||
} | ||
`; | ||
@@ -10,0 +11,0 @@ |
@@ -5,3 +5,3 @@ import Observable from 'zen-observable-ts'; | ||
export interface GraphQLRequest { | ||
query: string | DocumentNode; | ||
query: DocumentNode; | ||
variables?: Record<string, any>; | ||
@@ -13,4 +13,11 @@ operationName?: string; | ||
// backwards compat | ||
export type Operation = GraphQLRequest; | ||
export interface Operation { | ||
query: DocumentNode; | ||
variables: Record<string, any>; | ||
operationName: string; | ||
extensions: Record<string, any>; | ||
setContext: (context: Record<string, any>) => Record<string, any>; | ||
getContext: () => Record<string, any>; | ||
toKey: () => string; | ||
} | ||
@@ -17,0 +24,0 @@ export type FetchResult< |
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
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
No README
QualityPackage does not have a README. This may indicate a failed publish or a low quality package.
Found 1 instance in 1 package
86515
11
41
1829
0
21
0
3
+ Addedgraphql@^0.11.3
+ Addedgraphql@0.11.7(transitive)
+ Addediterall@1.1.3(transitive)