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

[WIP] TypeScript #175

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
83b3047
chore(deps): add ember-cli-typescript
buschtoens Oct 14, 2018
a8095fa
chore(deps): add @ember-decorators/service
buschtoens Oct 14, 2018
46554fb
chore(deps): add @ember-decorators/object
buschtoens Oct 14, 2018
777a5fc
chore(tsconfig): enable `experimentalDecorators`
buschtoens Oct 14, 2018
0e75983
chore(tsconfig): make fetch import work
buschtoens Oct 14, 2018
d891ca7
refactor: first stab at a TS conversion
buschtoens Oct 14, 2018
53e9a27
fix(deps): upgrade to latest and reatest typescript
buschtoens Dec 5, 2018
e44e5f8
fix: some new semantics
buschtoens Dec 5, 2018
2a75ca0
test(services/apollo): fix broken `clientOptions` override test
buschtoens Dec 6, 2018
e6ebfb0
chore: fix code-based type errors
buschtoens Dec 6, 2018
20364f8
fix(services/apollo): pass args for init
buschtoens Dec 17, 2018
1eec57b
chore(deps): upgrade to the latest and greatest
buschtoens Dec 17, 2018
54da71c
chore(deps): re-add common-tags
buschtoens Dec 17, 2018
e4ea5b0
chore(deps): update types for @ember/object
buschtoens Dec 17, 2018
ba3553f
refactor: PoC for .graphql imports
buschtoens Dec 17, 2018
a30500b
chore(deps): add ember-cli-typescript
buschtoens Oct 14, 2018
bd86b24
chore(deps): add @ember-decorators/service
buschtoens Oct 14, 2018
632dffe
chore(deps): add @ember-decorators/object
buschtoens Oct 14, 2018
5a2a046
chore(tsconfig): enable `experimentalDecorators`
buschtoens Oct 14, 2018
6120d0f
chore(tsconfig): make fetch import work
buschtoens Oct 14, 2018
202e200
refactor: first stab at a TS conversion
buschtoens Oct 14, 2018
872981a
fix(deps): upgrade to latest and reatest typescript
buschtoens Dec 5, 2018
248f470
fix: some new semantics
buschtoens Dec 5, 2018
0b19eb1
test(services/apollo): fix broken `clientOptions` override test
buschtoens Dec 6, 2018
b04da24
chore: fix code-based type errors
buschtoens Dec 6, 2018
2af17cc
fix(services/apollo): pass args for init
buschtoens Dec 17, 2018
c784091
chore(deps): upgrade to the latest and greatest
buschtoens Dec 17, 2018
86a7b78
refactor: PoC for .graphql imports
buschtoens Dec 17, 2018
dcd631f
Update yarn.lock after rebase
josemarluedke Jan 11, 2019
9cfa421
Merge branch 'refactor/140-typescript' into refactor/140-typescript
josemarluedke Jan 11, 2019
c0f1a0e
Move graphql to peerDependencies
josemarluedke Jan 11, 2019
4140e12
refactor(types): remove rest params in framework hooks for TS
buschtoens Jan 13, 2019
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
82 changes: 44 additions & 38 deletions addon/apollo/query-manager.js → addon/apollo/query-manager.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,26 @@
import { inject as service } from '@ember/service';
import EmberObject from '@ember/object';
import { alias } from '@ember/object/computed';
import { A } from '@ember/array';
import { service } from '@ember-decorators/service';
import EmberObject, { get } from '@ember/object';
import { alias } from '@ember-decorators/object/computed';
import {
ApolloClient,
MutationOptions,
QueryOptions,
WatchQueryOptions,
} from 'apollo-client';
import ApolloService, { ResultKey } from 'ember-apollo-client/services/apollo';

export default EmberObject.extend({
apollo: service(),
apolloClient: alias('apollo.client'),
type Subscription = ZenObservable.Subscription;

activeSubscriptions: null,
interface ActiveSubscription {
subscription: Subscription;
stale: boolean;
}

init() {
this._super(...arguments);
this.set('activeSubscriptions', A([]));
},
export default class QueryManager extends EmberObject {
@service apollo!: ApolloService;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the QueryManager guaranteed to only be instantiated through ApolloService#create QueryManager? In that case we should remove the @service injection here and make apollo a constructor parameter.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can likely also drop the extends EmberObject bit.

@alias('apollo.client') apolloClient!: ApolloClient<any>;

private activeSubscriptions: ActiveSubscription[] = [];

/**
* Executes a mutation on the Apollo service. The resolved object will
Expand All @@ -24,9 +32,9 @@ export default EmberObject.extend({
* @return {!Promise}
* @public
*/
mutate(opts, resultKey) {
return this.get('apollo').mutate(opts, resultKey);
},
mutate(opts: MutationOptions, resultKey: ResultKey) {
return get(this, 'apollo').mutate(opts, resultKey);
}

/**
* Executes a single `query` on the Apollo service. The resolved object will
Expand All @@ -38,9 +46,9 @@ export default EmberObject.extend({
* @return {!Promise}
* @public
*/
query(opts, resultKey) {
return this.get('apollo').query(opts, resultKey);
},
query(opts: QueryOptions, resultKey: ResultKey) {
return get(this, 'apollo').query(opts, resultKey);
}

/**
* Executes a `watchQuery` on the Apollo service. If updated data for this
Expand All @@ -56,21 +64,21 @@ export default EmberObject.extend({
* @return {!Promise}
* @public
*/
watchQuery(opts, resultKey) {
return this.get('apollo').managedWatchQuery(this, opts, resultKey);
},
watchQuery(opts: WatchQueryOptions, resultKey: ResultKey) {
return get(this, 'apollo').managedWatchQuery(this, opts, resultKey);
}

/**
* Tracks a subscription in the list of active subscriptions, which will all be
* cancelled when `unsubcribeAll` is called.
* cancelled when `unsubscribeAll` is called.
*
* @method trackSubscription
* @param {!Object} subscription The Apollo Client Subscription to be tracked for future unsubscription.
* @param {!Object} subscription The Apollo Client Subscription to be tracked for future unsubscribing.
* @private
*/
trackSubscription(subscription) {
this.get('activeSubscriptions').pushObject({ subscription, stale: false });
},
trackSubscription(subscription: Subscription) {
this.activeSubscriptions.push({ subscription, stale: false });
}

/**
* Marks all tracked subscriptions as being stale, such that they will be
Expand All @@ -80,11 +88,10 @@ export default EmberObject.extend({
* @private
*/
markSubscriptionsStale() {
let subscriptions = this.get('activeSubscriptions');
subscriptions.forEach(subscription => {
for (const subscription of this.activeSubscriptions) {
subscription.stale = true;
});
},
}
}

/**
* Unsubscribes from all actively tracked subscriptions initiated by calls to
Expand All @@ -98,12 +105,11 @@ export default EmberObject.extend({
* @public
*/
unsubscribeAll(onlyStale = false) {
let subscriptions = this.get('activeSubscriptions');
subscriptions.forEach(subscription => {
if (!onlyStale || subscription.stale) {
subscription.subscription.unsubscribe();
for (const { stale, subscription } of this.activeSubscriptions) {
if (!onlyStale || stale) {
subscription.unsubscribe();
}
});
this.set('activeSubscriptions', A([]));
},
});
}
this.activeSubscriptions = [];
}
}
6 changes: 6 additions & 0 deletions addon/graphql.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
declare module '*.graphql' {
import { DocumentNode } from 'graphql';
const defaultDocument: DocumentNode;

export default defaultDocument;
}
11 changes: 0 additions & 11 deletions addon/index.js

This file was deleted.

19 changes: 19 additions & 0 deletions addon/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { ObservableQuery } from 'apollo-client';

export {
default as ComponentQueryManager,
} from 'ember-apollo-client/-private/mixins/component-query-manager';
export {
default as ObjectQueryManager,
} from 'ember-apollo-client/-private/mixins/object-query-manager';
export {
default as RouteQueryManager,
} from 'ember-apollo-client/-private/mixins/route-query-manager';

export const apolloObservableKey = '_apolloObservable';

export function getObservable(queryResult: {
[apolloObservableKey]: ObservableQuery;
}): ObservableQuery {
return queryResult[apolloObservableKey];
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I reckon that this is actually what was intended here.

In the original index.js this was done:

https://github.com/bgentry/ember-apollo-client/blob/98c5880830358d0506f5cc95ac23469ff69be7f7/addon/index.js#L5-L9

}
Loading