Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add basic interoperability with other Observable implementations #196

Merged
merged 1 commit into from
May 10, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@
"lodash.isobject": "^3.0.2",
"lodash.isstring": "^4.0.1",
"lodash.isundefined": "^3.0.1",
"redux": "^3.3.1"
"redux": "^3.3.1",
"symbol-observable": "^0.2.4"
},
"devDependencies": {
"async": "^1.5.2",
Expand Down Expand Up @@ -77,6 +78,7 @@
"tslint": "^3.6.0",
"typescript": "^1.8.9",
"typings": "^0.7.9",
"uglify-js": "^2.6.2"
"uglify-js": "^2.6.2",
"rxjs": "^5.0.0-beta.7"
}
}
6 changes: 6 additions & 0 deletions src/util/Observable.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// This simplified polyfill attempts to follow the ECMAScript Observable proposal.
// See https://github.com/zenparsing/es-observable

import * as $$observable from 'symbol-observable'

export type CleanupFunction = () => void;
export type SubscriberFunction<T> = (observer: Observer<T>) => (Subscription | CleanupFunction);

Expand All @@ -15,6 +17,10 @@ export class Observable<T> {
this.subscriberFunction = subscriberFunction;
}

public [$$observable]() {
return this
}

public subscribe(observer: Observer<T>): Subscription {
let subscriptionOrCleanupFunction = this.subscriberFunction(observer);

Expand Down
55 changes: 55 additions & 0 deletions test/QueryManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ import {
Document,
} from 'graphql';

import * as Rx from 'rxjs';

import assign = require('lodash.assign');

import mockNetworkInterface from './mocks/mockNetworkInterface';

describe('QueryManager', () => {
Expand Down Expand Up @@ -391,6 +395,57 @@ describe('QueryManager', () => {
done();
});

it('supports interoperability with other Observable implementations like RxJS', (done) => {
const query = gql`
query people {
allPeople(first: 1) {
people {
name
}
}
}
`;

const data = {
allPeople: {
people: [
{
name: 'Luke Skywalker',
},
],
},
};

const networkInterface = mockNetworkInterface(
{
request: { query },
result: { data },
}
);

const queryManager = new QueryManager({
networkInterface,
store: createApolloStore(),
reduxRootKey: 'apollo',
});

const handle = queryManager.watchQuery({
query,
});

const observable = Rx.Observable.from(handle);

observable
.map(result => (assign({ fromRx: true }, result)))
.subscribe({
next(result) {
const expectedResult = assign({ fromRx: true }, result);
assert.deepEqual(result, expectedResult);
done();
},
});
});

it('allows you to refetch queries', (done) => {
const query = gql`
query fetchLuke($id: String) {
Expand Down