Skip to content
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

### vNEXT

- Made `mutate()` and `query()` methods to return `Observable` instead of `Promise` ([PR #140](https://github.com/apollostack/angular2-apollo/pull/140))
- Define types of options and results (of `watchQuery`, `query`, `mutate`) ([PR #145](https://github.com/apollostack/angular2-apollo/pull/145))

### v0.7.0
Expand Down
9 changes: 5 additions & 4 deletions src/Angular2Apollo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { FragmentDefinition } from 'graphql';
import { ApolloQueryObservable } from './ApolloQueryObservable';

import 'rxjs/add/observable/from';
import 'rxjs/add/observable/fromPromise';

export interface MutateOptions {
mutation: Document;
Expand Down Expand Up @@ -36,12 +37,12 @@ export class Angular2Apollo {
return new ApolloQueryObservable(rxify(this.client.watchQuery)(options));
}

public query(options: WatchQueryOptions): Promise<ApolloQueryResult> {
return this.client.query(options);
public query(options: WatchQueryOptions): Observable<ApolloQueryResult> {
return Observable.fromPromise(this.client.query(options));
}

public mutate(options: MutateOptions): Promise<ApolloQueryResult> {
return this.client.mutate(options);
public mutate(options: MutateOptions): Observable<ApolloQueryResult> {
return Observable.fromPromise(this.client.mutate(options));
}

public subscribe(options: any): Observable<any> {
Expand Down
36 changes: 25 additions & 11 deletions tests/Angular2Apollo.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ describe('angular2Apollo', () => {
expect(client.watchQuery).toHaveBeenCalledWith(options);
});

it('should be able to use obserable variable', (done) => {
it('should be able to use obserable variable', (done: jest.DoneCallback) => {
const variables = {
foo: new Subject(),
};
Expand Down Expand Up @@ -109,7 +109,7 @@ describe('angular2Apollo', () => {
}, 200);
});

it('should be able to use obserable variables', (done) => {
it('should be able to use obserable variables', (done: jest.DoneCallback) => {
const variables = {
foo: new Subject(),
bar: new Subject(),
Expand Down Expand Up @@ -139,7 +139,7 @@ describe('angular2Apollo', () => {
}, 200);
});

it('should be able to refetch', (done) => {
it('should be able to refetch', (done: jest.DoneCallback) => {
const variables = { foo: 'foo' };
const options = { query, variables, returnPartialData: true };

Expand Down Expand Up @@ -173,28 +173,42 @@ describe('angular2Apollo', () => {
});

describe('query()', () => {
it('should be called with the same options', () => {
it('should be called with the same options', (done: jest.DoneCallback) => {
const options = {query: '', variables: {}};
const promise = new Promise((resolve) => {
resolve('query');
});

spyOn(client, 'query').and.returnValue('query');
spyOn(client, 'query').and.returnValue(promise);

const result = angular2Apollo.query(options);

expect(client.query).toHaveBeenCalledWith(options);
expect(result).toEqual('query');

result.subscribe(r => {
expect(r).toEqual('query');
done();
});
});
});

describe('mutate()', () => {
it('should be called with the same options', () => {
it('should be called with the same options', (done: jest.DoneCallback) => {
const options = {mutation: '', variables: {}};
const promise = new Promise((resolve) => {
resolve('mutation');
});

spyOn(client, 'mutate').and.returnValue('mutate');
spyOn(client, 'mutate').and.returnValue(promise);

const result = angular2Apollo.mutate(options);

expect(client.mutate).toHaveBeenCalledWith(options);
expect(result).toEqual('mutate');

result.subscribe(r => {
expect(r).toEqual('mutation');
done();
});
});
});

Expand All @@ -207,7 +221,7 @@ describe('angular2Apollo', () => {
}).toThrowError('subscriptions');
});

it('should be called with the same options and return Observable', (done) => {
it('should be called with the same options and return Observable', (done: jest.DoneCallback) => {
const options = {query: '', variables: {}};

spyOn(client, 'subscribe').and.returnValue(['subscription']);
Expand All @@ -222,7 +236,7 @@ describe('angular2Apollo', () => {
done();
},
error(error) {
done(new Error('should not be called'));
done.fail('should not be called');
},
});
});
Expand Down