Skip to content
This repository has been archived by the owner on Apr 13, 2023. It is now read-only.

Update to apollo client 0.4.0 #105

Merged
merged 4 commits into from
Jul 12, 2016
Merged
Show file tree
Hide file tree
Changes from 3 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
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@
"peerDependencies": {
"react": "0.14.x || 15.* || ^15.0.0-rc",
"redux": "^2.0.0 || ^3.0.0",
"apollo-client": "^0.1.0 || ^0.2.0 || ^0.3.0"
"apollo-client": "^0.1.0 || ^0.2.0 || ^0.3.0 || ^0.4.0"
},
"devDependencies": {
"apollo-client": "^0.3.0",
"apollo-client": "^0.4.0",
"browserify": "^13.0.0",
"chai": "^3.5.0",
"chai-as-promised": "^5.2.0",
Expand Down
53 changes: 31 additions & 22 deletions src/connect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,14 @@ import {
Store,
} from 'redux';

import ApolloClient, { readQueryFromStore } from 'apollo-client';
import ApolloClient, {
readQueryFromStore,
} from 'apollo-client';

import {
ObservableQuery,
QuerySubscription,
} from 'apollo-client/QueryManager';

import {
GraphQLResult,
Expand Down Expand Up @@ -131,7 +138,8 @@ export default function connect(opts?: ConnectOptions) {
private previousQueries: Object;

// request / action storage
private queryHandles: any;
private queryObservables: { [queryKey: string]: ObservableQuery };
private querySubscriptions: { [queryKey: string]: QuerySubscription };
private mutations: any;

// calculated switches to control rerenders
Expand Down Expand Up @@ -164,6 +172,8 @@ export default function connect(opts?: ConnectOptions) {

this.data = {};
this.mutations = {};
this.queryObservables = {};
this.querySubscriptions = {};
}

componentWillMount() {
Expand Down Expand Up @@ -254,33 +264,31 @@ export default function connect(opts?: ConnectOptions) {
const { watchQuery, reduxRootKey } = this.client;
const { store } = this;

const queryHandles = mapQueriesToProps({
const queryOptions = mapQueriesToProps({
state: store.getState(),
ownProps: props,
});

const oldQueries = assign({}, this.previousQueries);
this.previousQueries = assign({}, queryHandles);
this.previousQueries = assign({}, queryOptions);

Copy link
Contributor Author

Choose a reason for hiding this comment

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

As far as I know, this line was not correct - it was assigning query options to the same object we use to hold the observables/handles/subscriptions.

// don't re run queries if nothing has changed
if (isEqual(oldQueries, queryHandles)) {
if (isEqual(oldQueries, queryOptions)) {
return false;
} else if (oldQueries) {
// unsubscribe from previous queries
this.unsubcribeAllQueries();
}

if (isObject(queryHandles) && Object.keys(queryHandles).length) {
this.queryHandles = queryHandles;

for (const key in queryHandles) {
if (!queryHandles.hasOwnProperty(key)) {
if (isObject(queryOptions) && Object.keys(queryOptions).length) {
for (const key in queryOptions) {
if (!queryOptions.hasOwnProperty(key)) {
continue;
}

const { query, variables, forceFetch } = queryHandles[key];
const { query, variables, forceFetch } = queryOptions[key];

const handle = watchQuery(queryHandles[key]);
const observableQuery = watchQuery(queryOptions[key]);

// rudimentary way to manually check cache
let queryData = defaultQueryData as any;
Expand All @@ -303,24 +311,24 @@ export default function connect(opts?: ConnectOptions) {

this.data[key] = queryData;

this.handleQueryData(handle, key);
this.handleQueryData(observableQuery, key);
}
}
return true;
}

unsubcribeAllQueries() {
if (this.queryHandles) {
for (const key in this.queryHandles) {
if (!this.queryHandles.hasOwnProperty(key)) {
if (this.querySubscriptions) {
for (const key in this.querySubscriptions) {
if (!this.querySubscriptions.hasOwnProperty(key)) {
continue;
}
this.queryHandles[key].unsubscribe();
this.querySubscriptions[key].unsubscribe();
}
}
}

handleQueryData(handle: any, key: string) {
handleQueryData(observableQuery: ObservableQuery, key: string) {
// bind each handle to updating and rerendering when data
// has been recieved
let refetch,
Expand Down Expand Up @@ -387,14 +395,15 @@ export default function connect(opts?: ConnectOptions) {
}
};

this.queryHandles[key] = handle.subscribe({
this.queryObservables[key] = observableQuery;
this.querySubscriptions[key] = observableQuery.subscribe({
next: forceRender,
error(errors) { forceRender({ errors }); },
});

refetch = createBoundRefetch(key, this.queryHandles[key].refetch);
startPolling = this.queryHandles[key].startPolling;
stopPolling = this.queryHandles[key].stopPolling;
refetch = createBoundRefetch(key, this.queryObservables[key].refetch);
startPolling = this.queryObservables[key].startPolling;
stopPolling = this.queryObservables[key].stopPolling;

this.data[key] = assign(this.data[key], {
refetch,
Expand Down
5 changes: 3 additions & 2 deletions typings/modules/apollo-client/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -274,11 +274,12 @@ import { Observable, Observer, Subscription } from '~apollo-client/util/Observab
export class ObservableQuery extends Observable<GraphQLResult> {
subscribe(observer: Observer<GraphQLResult>): QuerySubscription;
result(): Promise<GraphQLResult>;
}
export interface QuerySubscription extends Subscription {
refetch(variables?: any): Promise<GraphQLResult>;
stopPolling(): void;
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 couldn't figure out how to get typings to do the right thing here, so I edited manually to get things to pass. How do we regenerate the types here?

startPolling(pollInterval: number): void;
}
export interface QuerySubscription extends Subscription {

}
export interface WatchQueryOptions {
query: Document;
Expand Down