Skip to content

Commit

Permalink
Merge pull request #40 from apollostack/methods-observable
Browse files Browse the repository at this point in the history
Methods moved to an observable apollographql/apollo-client#362
  • Loading branch information
kamilkisiela authored Jul 16, 2016
2 parents 6207b54 + a2a7ba7 commit 3143269
Show file tree
Hide file tree
Showing 10 changed files with 127 additions and 107 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
### vNEXT

- Passing all the options of mutation in `Apollo` decorator [PR #39](https://github.com/apollostack/angular2-apollo/pull/39)
- Added support for `apollo-client` breaking change that moves methods to query's observable ([PR #40](https://github.com/apollostack/angular2-apollo/pull/40))

### v0.3.0

Expand Down
1 change: 1 addition & 0 deletions examples/hello-world/.meteor/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
dev_bundle
local
1 change: 0 additions & 1 deletion examples/hello-world/.meteor/packages
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,3 @@ ecmascript # Enable ECMAScript2015+ syntax in app code
autopublish # Publish all data to the clients (for prototyping)
insecure # Allow all DB writes from clients (for prototyping)
angular2-compilers
barbatus:angular2-runtime
2 changes: 1 addition & 1 deletion examples/hello-world/.meteor/release
Original file line number Diff line number Diff line change
@@ -1 +1 @@
[email protected].1
[email protected].4.4
143 changes: 72 additions & 71 deletions examples/hello-world/.meteor/versions
Original file line number Diff line number Diff line change
@@ -1,71 +1,72 @@
[email protected]
[email protected]_1
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
barbatus:[email protected]_2
barbatus:[email protected]
barbatus:[email protected]
barbatus:[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]_1
barbatus:[email protected]
barbatus:[email protected]_1
barbatus:[email protected]
barbatus:[email protected]
barbatus:[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]_1
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
urigo:[email protected]
[email protected]
[email protected]
[email protected]
43 changes: 20 additions & 23 deletions examples/hello-world/client/main.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import 'reflect-metadata';
import 'rxjs';
import 'zone.js/dist/zone';
import 'zone.js/dist/long-stack-trace-zone';

import {
bootstrap,
} from '@angular/platform-browser-dynamic';

import {
Component,
Injectable,
} from '@angular/core';

import {
Expand All @@ -15,24 +19,25 @@ import ApolloClient, {
createNetworkInterface,
} from 'apollo-client';

import gql from 'apollo-client/gql';
import gql from 'graphql-tag';

import {
GraphQLResult,
} from 'graphql';
ApolloQueryResult,
} from 'apollo-client';

import template from './main.html';

const client = new ApolloClient({
networkInterface: createNetworkInterface('/graphql'),
});

@Component({
selector: 'app',
templateUrl: 'client/main.html',
template,
})
@Injectable()
@Apollo({
client,
queries(state: any) {
queries(component: Main) {
return {
data: {
query: gql`
Expand All @@ -48,12 +53,12 @@ const client = new ApolloClient({
}
`,
variables: {
name: state.nameFilter,
name: component.nameFilter,
},
},
};
},
mutations(state: any) {
mutations(component: Main) {
return {
addUser: (firstName: string) => ({
mutation: gql`
Expand All @@ -76,7 +81,7 @@ const client = new ApolloClient({
`,
variables: {
firstName,
lastName: state.lastName,
lastName: component.lastName,
},
}),
};
Expand All @@ -87,26 +92,18 @@ class Main {
public firstName: string;
public lastName: string;
public nameFilter: string;
public addUser: (firstName: string) => Promise<GraphQLResult>;
public addUser: (firstName: string) => Promise<ApolloQueryResult>;

public newUser(firstName: string) {
this.addUser(firstName)
.then((graphQLResult: GraphQLResult) => {
const { errors, data } = graphQLResult;

if (data) {
console.log('got data', data);
}

if (errors) {
console.log('got some GraphQL execution errors', errors);
}
.then(({ data }: ApolloQueryResult) => {
console.log('got data', data);

// get new data
this.data.refetch();
})
.catch((error: any) => {
console.log('there was an error sending the query', error);
.catch((errors: any) => {
console.log('there was an error sending the query', errors);
});
}
}
Expand Down
3 changes: 2 additions & 1 deletion examples/hello-world/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@
"@angular/platform-browser": "^2.0.0-rc.1",
"@angular/platform-browser-dynamic": "^2.0.0-rc.1",
"angular2-apollo": "file:../../",
"apollo-client": "^0.3.12",
"apollo-client": "^0.4.1",
"casual-browserify": "^1.5.2",
"es6-shim": "^0.35.1",
"express": "^4.13.4",
"fs.extra": "^1.3.2",
"graphql": "^0.5.0",
"graphql-tag": "^0.1.9",
"graphql-tools": "^0.3.8",
"http-proxy-middleware": "^0.14.0",
"meteor-node-stubs": "~0.2.0",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"homepage": "https://github.com/apollostack/angular2-apollo#readme",
"dependencies": {
"@angular/core": "^2.0.0-rc.1",
"apollo-client": "^0.3.0",
"apollo-client": "^0.3.0 || ^0.4.0",
"graphql-tag": "^0.1.7",
"lodash": "^4.11.1"
},
Expand Down
36 changes: 28 additions & 8 deletions src/apolloDecorator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ interface ApolloHandleOptions extends ApolloOptions {
class ApolloHandle {
private lastQueryVariables: Object = {};
private queryHandles: Object = {};
private querySubscriptions: Object = {};

private component;
private client;
Expand Down Expand Up @@ -65,7 +66,7 @@ class ApolloHandle {
}

public unsubscribe(queryName?: string): void {
const allQueries = this.getAllQueries();
const allQueries = this.getAllQueriesSubs();

if (allQueries) {
if (queryName) {
Expand Down Expand Up @@ -93,8 +94,16 @@ class ApolloHandle {
return this.queryHandles[name];
}

private getAllQueries() {
return this.queryHandles;
private setQuerySub(name, sub): void {
this.querySubscriptions[name] = sub;
}

private getQuerySub(name) {
return this.querySubscriptions[name];
}

private getAllQueriesSubs() {
return this.querySubscriptions;
}

/**
Expand Down Expand Up @@ -148,6 +157,15 @@ class ApolloHandle {
};
}

// XXX https://github.com/apollostack/apollo-client/pull/362
private backcompat(queryName: string, method: string, args?) {
if (this.getQuerySub(queryName)[method]) {
return this.getQuerySub(queryName)[method](...args);
}

return this.getQuery(queryName)[method](...args);
}

private subscribe(queryName: string, obs: any) {
this.component[queryName] = {
errors: null,
Expand All @@ -160,17 +178,19 @@ class ApolloHandle {
assign(this.component[queryName], {
errors,
loading: false,
unsubscribe: () => this.getQuery(queryName).unsubscribe(),
refetch: (...args) => this.getQuery(queryName).refetch(...args),
stopPolling: () => this.getQuery(queryName).stopPolling(),
startPolling: (...args) => this.getQuery(queryName).startPolling(...args),
unsubscribe: () => this.getQuerySub(queryName).unsubscribe(),
refetch: (...args) => this.backcompat(queryName, 'refetch', args),
stopPolling: () => this.backcompat(queryName, 'stopPolling'),
startPolling: (...args) => this.backcompat(queryName, 'startPolling', args),
}, changed ? data : {});
};

// we don't want to have multiple subscriptions
this.unsubscribe(queryName);

this.setQuery(queryName, obs.subscribe({
this.setQuery(queryName, obs);

this.setQuerySub(queryName, obs.subscribe({
next: setQuery,
error(errors) {
setQuery({ errors });
Expand Down
2 changes: 1 addition & 1 deletion tests/apolloDecorator/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ describe('Apollo - decorator - queries()', () => {
setTimeout(() => {
expect(typeof component.data.unsubscribe).toEqual('function');

const spy = spyOn(component.__apolloHandle.getQuery('data'), 'unsubscribe');
const spy = spyOn(component.__apolloHandle.getQuerySub('data'), 'unsubscribe');
component.data.unsubscribe();

expect(spy).toHaveBeenCalled();
Expand Down

0 comments on commit 3143269

Please sign in to comment.