From 61878892494381acc41056e1e69d00fbf3d57167 Mon Sep 17 00:00:00 2001 From: Kamil Kisiela Date: Mon, 31 Oct 2016 22:11:05 +0100 Subject: [PATCH 1/3] feat(Angular2Apollo): mutate and query as Observables --- src/Angular2Apollo.ts | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/Angular2Apollo.ts b/src/Angular2Apollo.ts index b96476840..fa94ea31a 100644 --- a/src/Angular2Apollo.ts +++ b/src/Angular2Apollo.ts @@ -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; @@ -36,12 +37,12 @@ export class Angular2Apollo { return new ApolloQueryObservable(rxify(this.client.watchQuery)(options)); } - public query(options: WatchQueryOptions): Promise { - return this.client.query(options); + public query(options: WatchQueryOptions): Observable { + return Observable.fromPromise(this.client.query(options)); } - public mutate(options: MutateOptions): Promise { - return this.client.mutate(options); + public mutate(options: MutateOptions): Observable { + return Observable.fromPromise(this.client.mutate(options)); } public subscribe(options: any): Observable { From 4be620ccbf2e719e4e84f344a4ba26fe2715318d Mon Sep 17 00:00:00 2001 From: Kamil Kisiela Date: Tue, 1 Nov 2016 11:03:51 +0100 Subject: [PATCH 2/3] test(Angular2Apollo): update query and mutate --- tests/Angular2Apollo.spec.ts | 36 +++++++++++++++++++++++++----------- 1 file changed, 25 insertions(+), 11 deletions(-) diff --git a/tests/Angular2Apollo.spec.ts b/tests/Angular2Apollo.spec.ts index 7be1420c2..14c748424 100644 --- a/tests/Angular2Apollo.spec.ts +++ b/tests/Angular2Apollo.spec.ts @@ -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(), }; @@ -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(), @@ -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 }; @@ -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(); + }); }); }); @@ -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']); @@ -222,7 +236,7 @@ describe('angular2Apollo', () => { done(); }, error(error) { - done(new Error('should not be called')); + done.fail('should not be called'); }, }); }); From c61fc6d5eb47988064b3c9a5ed05f8fad0ca6e91 Mon Sep 17 00:00:00 2001 From: Kamil Kisiela Date: Tue, 1 Nov 2016 11:07:35 +0100 Subject: [PATCH 3/3] docs(Angular2Apollo): Add recent changes to changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1b8bfc507..57ccbbd1a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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