Skip to content

Commit a71015d

Browse files
authored
Tree-shaking with apollo-client and apollo-client-rxjs (#206)
* feat: tree-shaking with apollo-client and apollo-client-rxjs * chore(rollup): suppress few warnings * chore: latest apollo-client * chore: update yarn.lock
1 parent 2cc3751 commit a71015d

File tree

8 files changed

+88
-94
lines changed

8 files changed

+88
-94
lines changed

examples/hello-world/package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
"@angular/platform-server": "^2.3.1",
2525
"@angular/router": "^3.3.1",
2626
"angular2-apollo": "file:../../",
27-
"apollo-client": "~0.6.0",
27+
"apollo-client": "~0.7.0",
2828
"core-js": "^2.4.1",
2929
"graphql-tag": "^1.1.2",
3030
"rxjs": "^5.0.1",
@@ -57,6 +57,6 @@
5757
"rollup-plugin-uglify": "^1.0.1",
5858
"ts-node": "1.2.1",
5959
"tslint": "^4.2.0",
60-
"typescript": "~2.0.6"
60+
"typescript": "~2.1.0"
6161
}
6262
}

examples/hello-world/src/app/app.component.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ export class AppComponent implements OnInit, AfterViewInit {
9090
},
9191
})
9292
.toPromise()
93-
.then(({ data }: ApolloQueryResult) => {
93+
.then(({ data }: ApolloQueryResult<any>) => {
9494
console.log('got data', data);
9595

9696
// get new data

package.json

+7-6
Original file line numberDiff line numberDiff line change
@@ -22,22 +22,22 @@
2222
"test-only": "jest",
2323
"test-watch": "jest --watch",
2424
"remap": "remap-istanbul -i coverage/coverage-final.json -t lcovonly -o coverage/lcov.info",
25-
"lint": "tslint src/*.ts && tslint tests/*.ts ",
25+
"lint": "tslint src/*.ts && tslint tests/*.ts",
2626
"build": "./node_modules/.bin/ngc -p tsconfig.json",
2727
"build-test": "./node_modules/.bin/ngc -p tsconfig.test.json",
2828
"postbuild": "npm run bundle",
2929
"bundle": "rollup -c",
3030
"clean": "rimraf build/* coverage/*",
31-
"prepublish": "npm run clean && npm run build",
31+
"prepublishs": "npm run clean && npm run build",
3232
"commitmsg": "validate-commit-msg"
3333
},
3434
"peerDependencies": {
3535
"@angular/core": "^2.0.0",
36-
"apollo-client": "^0.6.0",
36+
"apollo-client": "^0.7.0",
3737
"rxjs": "^5.0.0-beta.12 || ^5.0.0-rc.1 || ^5.0.0"
3838
},
3939
"dependencies": {
40-
"apollo-client-rxjs": "~0.3.0"
40+
"apollo-client-rxjs": "~0.4.0"
4141
},
4242
"devDependencies": {
4343
"@angular/common": "^2.3.1",
@@ -49,7 +49,8 @@
4949
"@angular/platform-server": "^2.3.1",
5050
"@types/jest": "^15.1.32",
5151
"@types/lodash": "^4.14.34",
52-
"apollo-client": "^0.6.0",
52+
"@types/node": "^6.0.59",
53+
"apollo-client": "^0.7.1",
5354
"graphql": "^0.8.2",
5455
"graphql-tag": "^1.1.2",
5556
"husky": "^0.12.0",
@@ -60,7 +61,7 @@
6061
"rollup": "^0.39.2",
6162
"rxjs": "^5.0.0",
6263
"tslint": "^4.2.0",
63-
"typescript": "~2.0.6",
64+
"typescript": "~2.1.4",
6465
"validate-commit-msg": "^2.8.2",
6566
"zone.js": "^0.7.1"
6667
},

rollup.config.js

+17-1
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,24 @@ export default {
33
dest: 'build/bundles/apollo.umd.js',
44
format: 'umd',
55
moduleName: 'ng.apollo',
6+
onwarn,
67
globals: {
78
'@angular/core': 'ng.core',
8-
'rxjs/Observable': 'Rx'
9+
'rxjs/Observable': 'Rx',
10+
'rxjs/observable/from': 'Rx.Observable',
11+
'rxjs/observable/fromPromise': 'Rx.Observable',
12+
'apollo-client-rxjs': 'apollo.rxjs',
13+
'apollo-client': 'apollo',
14+
}
15+
}
16+
17+
function onwarn(message) {
18+
const suppressed = [
19+
'UNRESOLVED_IMPORT',
20+
'THIS_IS_UNDEFINED'
21+
];
22+
23+
if (!suppressed.find(code => message.code === code)) {
24+
return console.warn(message.message);
925
}
1026
}

src/Angular2Apollo.ts

+8-9
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,12 @@ import { OpaqueToken, Injectable, Inject } from '@angular/core';
22
import { rxify } from 'apollo-client-rxjs';
33
import { ApolloClient, ApolloQueryResult, WatchQueryOptions, MutationOptions, SubscriptionOptions } from 'apollo-client';
44
import { Observable } from 'rxjs/Observable';
5+
import { from } from 'rxjs/observable/from';
6+
import { fromPromise } from 'rxjs/observable/fromPromise';
57
import { FragmentDefinitionNode } from 'graphql';
68

79
import { ApolloQueryObservable } from './ApolloQueryObservable';
810

9-
import 'rxjs/add/observable/from';
10-
import 'rxjs/add/observable/fromPromise';
11-
1211
export interface DeprecatedWatchQueryOptions extends WatchQueryOptions {
1312
fragments?: FragmentDefinitionNode[];
1413
}
@@ -22,16 +21,16 @@ export class Angular2Apollo {
2221
@Inject(ApolloClientInstance) private client: ApolloClient,
2322
) {}
2423

25-
public watchQuery(options: DeprecatedWatchQueryOptions): ApolloQueryObservable<ApolloQueryResult> {
24+
public watchQuery<T>(options: DeprecatedWatchQueryOptions): ApolloQueryObservable<ApolloQueryResult<T>> {
2625
return new ApolloQueryObservable(rxify(this.client.watchQuery)(options));
2726
}
2827

29-
public query(options: DeprecatedWatchQueryOptions): Observable<ApolloQueryResult> {
30-
return Observable.fromPromise(this.client.query(options));
28+
public query<T>(options: DeprecatedWatchQueryOptions): Observable<ApolloQueryResult<T>> {
29+
return fromPromise(this.client.query(options));
3130
}
3231

33-
public mutate(options: MutationOptions): Observable<ApolloQueryResult> {
34-
return Observable.fromPromise(this.client.mutate(options));
32+
public mutate<T>(options: MutationOptions): Observable<ApolloQueryResult<T>> {
33+
return fromPromise(this.client.mutate(options));
3534
}
3635

3736
public subscribe(options: SubscriptionOptions): Observable<any> {
@@ -40,7 +39,7 @@ export class Angular2Apollo {
4039
throw new Error(`Your version of ApolloClient doesn't support subscriptions`);
4140
}
4241

43-
return Observable.from(this.client.subscribe(options));
42+
return from(this.client.subscribe(options));
4443
}
4544

4645
public getClient(): ApolloClient {

tests/ApolloModule.spec.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ import { ApolloModule, SelectPipe, Angular2Apollo } from '../src';
66
import { ApolloClientWrapper, ApolloClientInstance } from '../src/Angular2Apollo';
77

88
describe('ApolloModule', () => {
9-
let metadata: any = ApolloModule['decorators'][0]['args'][0];
9+
const annotations: any[] = Reflect.getMetadata('annotations', ApolloModule);
10+
const metadata: any = annotations[0]; //ApolloModule['decorators'][0]['args'][0];
1011

1112
it('should contain SelectPipe in declarations', () => {
1213
expect(include(metadata.declarations, SelectPipe)).toBe(true);

tests/_mocks.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { NetworkInterface, BatchedNetworkInterface, Request } from 'apollo-client/transport/networkInterface';
1+
import { NetworkInterface, BatchedNetworkInterface, Request } from 'apollo-client/lib/src/transport/networkInterface';
22
import { ApolloClient } from 'apollo-client';
33
import { ExecutionResult, DocumentNode } from 'graphql';
44
import { print } from 'graphql-tag/printer';

0 commit comments

Comments
 (0)