Skip to content

Commit

Permalink
Merge pull request #467 from apollostack/loading_state
Browse files Browse the repository at this point in the history
Added loading state tracking
  • Loading branch information
Sashko Stubailo authored Aug 2, 2016
2 parents 145588a + dcd2566 commit e65e1e8
Show file tree
Hide file tree
Showing 6 changed files with 133 additions and 20 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Expect active development and potentially significant breaking changes in the `0

### vNEXT
- Fixed issue with alias names in batched queries. [PR #493](https://github.com/apollostack/apollo-client/pull/493) and [Issue #490](https://github.com/apollostack/apollo-client/issues).
- Add loading state tracking within Apollo Client in order to simplify the handling of loading state within the view layers. [Issue #342](https://github.com/apollostack/apollo-client/issues/342) and [PR #467](https://github.com/apollostack/apollo-client/pull/467)

### v0.4.9

Expand Down
3 changes: 2 additions & 1 deletion src/QueryManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,7 @@ export class QueryManager {
returnPartialData: options.returnPartialData || options.noFetch,
fragmentMap: queryStoreValue.fragmentMap,
}),
loading: queryStoreValue.loading,
};

if (observer.next) {
Expand Down Expand Up @@ -760,7 +761,7 @@ export class QueryManager {

// return a chainable promise
this.removeFetchQueryPromise(requestId);
resolve({ data: resultFromStore });
resolve({ data: resultFromStore, loading: false });
}).catch((error: Error) => {
this.store.dispatch({
type: 'APOLLO_QUERY_ERROR',
Expand Down
7 changes: 4 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,10 @@ export {

export type ApolloQueryResult = {
data: any;
// Right now only has one property, but will later include loading state, and possibly other info
// This is different from the GraphQLResult type because it doesn't include errors - those are
// thrown via the standard promise/observer catch mechanism
loading: boolean;

// This type is different from the GraphQLResult type because it doesn't include errors.
// Those are thrown via the standard promise/observer catch mechanism.
}

// A map going from the name of a fragment to that fragment's definition.
Expand Down
118 changes: 114 additions & 4 deletions test/QueryManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2533,7 +2533,7 @@ describe('QueryManager', () => {
});

queryManager.fetchQuery('bad-id', { query, fragments }).then((result) => {
assert.deepEqual(result, { data });
assert.deepEqual(result.data, data);
done();
});
});
Expand Down Expand Up @@ -2741,7 +2741,7 @@ describe('QueryManager', () => {
reduxRootKey: 'apollo',
});
queryManager.query({ query }).then((result) => {
assert.deepEqual(result, { data });
assert.deepEqual(result.data, data);

queryManager.query({ query, forceFetch: true }).then(() => {
done(new Error('Returned a result when it was not supposed to.'));
Expand Down Expand Up @@ -2878,11 +2878,11 @@ describe('QueryManager', () => {
handle.subscribe({
next(result) {
timesFired += 1;
assert.deepEqual(result, { data });
assert.deepEqual(result.data, data);
},
});
queryManager.query({ query }).then((result) => {
assert.deepEqual(result, { data });
assert.deepEqual(result.data, data);
assert.equal(timesFired, 1);
done();
});
Expand Down Expand Up @@ -3170,6 +3170,116 @@ describe('QueryManager', () => {
done();
}, 120);
});

describe('loading state', () => {
it('should be passed as false if we are not watching a query', (done) => {
const query = gql`
query {
fortuneCookie
}`;
const data = {
fortuneCookie: 'Buy it',
};
const queryManager = new QueryManager({
networkInterface: mockNetworkInterface({
request: { query },
result: { data },
}),
store: createApolloStore(),
reduxRootKey: 'apollo',
});
queryManager.query({ query }).then((result) => {
assert(!result.loading);
assert.deepEqual(result.data, data);
done();
});
});

it('should be passed to the observer as true if we are returning partial data', () => {
const primeQuery = gql`
query {
fortuneCookie
}`;
const primeData = {
fortuneCookie: 'You must stick to your goal but rethink your approach',
};
const query = gql`
query {
fortuneCookie
author {
name
}
}`;
const diffQuery = gql`
query {
author {
name
}
}`;
const diffData = {
author: {
name: 'John',
},
};
const queryManager = new QueryManager({
networkInterface: mockNetworkInterface(
{
request: { query: diffQuery },
result: { data: diffData },
delay: 5,
},
{
request: { query: primeQuery },
result: { data: primeData },
}
),
store: createApolloStore(),
reduxRootKey: 'apollo',
});
queryManager.query({ query: primeQuery }).then((primeResult) => {
const handle = queryManager.watchQuery({ query, returnPartialData: true });

handle.subscribe({
next(result) {
assert(result.loading);
assert.deepEqual(result.data, { data: diffData } );
},
});
});
});

it('should be passed to the observer as false if we are returning all the data', () => {
const query = gql`
query {
author {
firstName
lastName
}
}`;
const data = {
author: {
firstName: 'John',
lastName: 'Smith',
},
};
const queryManager = new QueryManager({
networkInterface: mockNetworkInterface(
{
request: { query },
result: { data },
}
),
store: createApolloStore(),
reduxRootKey: 'apollo',
});
const handle = queryManager.watchQuery({ query, returnPartialData: false });
handle.subscribe({
next(result) {
assert(!result.loading);
},
});
});
});
});

function testDiffing(
Expand Down
20 changes: 10 additions & 10 deletions test/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ describe('client', () => {

return client.query({ query })
.then((result) => {
assert.deepEqual(result, { data });
assert.deepEqual(result.data, data);
});
});

Expand Down Expand Up @@ -257,7 +257,7 @@ describe('client', () => {

return client.query({ query })
.then((result) => {
assert.deepEqual(result, { data });
assert.deepEqual(result.data, data);
});
});

Expand Down Expand Up @@ -296,7 +296,7 @@ describe('client', () => {

return client.query({ query })
.then((result) => {
assert.deepEqual(result, { data });
assert.deepEqual(result.data, data);
});
});

Expand Down Expand Up @@ -376,7 +376,7 @@ describe('client', () => {

return client.query({ query })
.then((result) => {
assert.deepEqual(result, { data });
assert.deepEqual(result.data, data);
assert.deepEqual(initialState, client.store.getState());
});
});
Expand Down Expand Up @@ -424,7 +424,7 @@ describe('client', () => {

return client.query({ query })
.then((result) => {
assert.deepEqual(result, { data });
assert.deepEqual(result.data, data);
});
});
it('should return errors correctly for a single query', () => {
Expand Down Expand Up @@ -860,7 +860,7 @@ describe('client', () => {

return client.query({ query })
.then((result) => {
assert.deepEqual(result, { data });
assert.deepEqual(result.data, data);
assert.deepEqual(client.store.getState()['apollo'].data['1'],
{
id: '1',
Expand Down Expand Up @@ -891,7 +891,7 @@ describe('client', () => {

return client.query({ query })
.then((result) => {
assert.deepEqual(result, { data });
assert.deepEqual(result.data, data);
assert.deepEqual(store.getState()['apollo'].data['1'],
{
id: '1',
Expand Down Expand Up @@ -1260,7 +1260,7 @@ describe('client', () => {
}`);

client.query({ query: queryDoc, fragments: fragmentDefs }).then((result) => {
assert.deepEqual(result, { data });
assert.deepEqual(result.data, data);
done();
});
});
Expand Down Expand Up @@ -1346,7 +1346,7 @@ describe('client', () => {
const observer = client.watchQuery({ query: queryDoc, fragments: fragmentDefs });
observer.subscribe({
next(result) {
assert.deepEqual(result, { data });
assert.deepEqual(result.data, data);
done();
},
});
Expand Down Expand Up @@ -1392,7 +1392,7 @@ describe('client', () => {
{ query: queryDoc, pollInterval: 30, fragments: fragmentDefs});
const subscription = observer.subscribe({
next(result) {
assert.deepEqual(result, { data });
assert.deepEqual(result.data, data);
subscription.unsubscribe();
done();
},
Expand Down
4 changes: 2 additions & 2 deletions test/scheduler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ describe('QueryScheduler', () => {
let subscription = observableQuery.subscribe({
next(result) {
timesFired += 1;
assert.deepEqual(result, { data });
assert.deepEqual(result.data, data);
subscription.unsubscribe();
},
});
Expand Down Expand Up @@ -472,7 +472,7 @@ describe('QueryScheduler', () => {
const subscription = observable.subscribe({
next(result) {
timesFired += 1;
assert.deepEqual(result, { data });
assert.deepEqual(result.data, data);
subscription.unsubscribe();
assert.equal(Object.keys(scheduler.registeredQueries).length, 0);
},
Expand Down

0 comments on commit e65e1e8

Please sign in to comment.