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

First pass at disabling force-fetch in SSR contexts #309

Merged
merged 4 commits into from
Jun 27, 2016
Merged
Show file tree
Hide file tree
Changes from 2 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
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,13 @@
"pretty-bytes": "^3.0.1",
"remap-istanbul": "^0.5.1",
"request-promise": "^2.0.1",
"rxjs": "^5.0.0-beta.7",
"sinon": "^1.17.4",
"source-map-support": "^0.4.0",
"swapi-graphql": "^0.0.4",
"tslint": "3.7.3",
"typescript": "^1.8.9",
"typings": "^1.0.0",
"uglify-js": "^2.6.2",
"rxjs": "^5.0.0-beta.7"
"uglify-js": "^2.6.2"
}
}
13 changes: 13 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ export default class ApolloClient {
public reducerConfig: ApolloReducerConfig;
public queryTransformer: QueryTransformer;
public shouldBatch: boolean;
public shouldForceFetch: boolean;

constructor({
networkInterface,
Expand All @@ -63,20 +64,28 @@ export default class ApolloClient {
dataIdFromObject,
queryTransformer,
shouldBatch = false,
ssrMode = false,
ssrForceFetchDelay = 0,
}: {
networkInterface?: NetworkInterface,
reduxRootKey?: string,
initialState?: any,
dataIdFromObject?: IdGetter,
queryTransformer?: QueryTransformer,
shouldBatch?: boolean,
ssrMode?: boolean,
ssrForceFetchDelay?: number
} = {}) {
this.reduxRootKey = reduxRootKey ? reduxRootKey : 'apollo';
this.initialState = initialState ? initialState : {};
this.networkInterface = networkInterface ? networkInterface :
createNetworkInterface('/graphql');
this.queryTransformer = queryTransformer;
this.shouldBatch = shouldBatch;
this.shouldForceFetch = !(ssrMode || ssrForceFetchDelay > 0);
if (ssrForceFetchDelay) {
setTimeout(() => this.shouldForceFetch = true, ssrForceFetchDelay);
}

this.reducerConfig = {
dataIdFromObject,
Expand All @@ -86,12 +95,16 @@ export default class ApolloClient {
public watchQuery = (options: WatchQueryOptions): ObservableQuery => {
this.initStore();

options.forceFetch = this.shouldForceFetch && options.forceFetch;

return this.queryManager.watchQuery(options);
};

public query = (options: WatchQueryOptions): Promise<GraphQLResult> => {
this.initStore();

options.forceFetch = this.shouldForceFetch && options.forceFetch;

return this.queryManager.query(options);
};

Expand Down
100 changes: 100 additions & 0 deletions test/client.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as chai from 'chai';
const { assert } = chai;
import * as sinon from 'sinon';

import ApolloClient from '../src';

Expand Down Expand Up @@ -879,4 +880,103 @@ describe('client', () => {
});
});
});

describe.only('forceFetch', () => {
Copy link
Contributor

Choose a reason for hiding this comment

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

What does describe.only do?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Whoops, just runs those tests.

const query = gql`
query number {
myNumber {
n
}
}
`;

const firstFetch = {
myNumber: {
n: 1,
},
};
const secondFetch = {
myNumber: {
n: 2,
},
};


let networkInterface;
let clock;
beforeEach(() => {
networkInterface = mockNetworkInterface({
request: { query },
result: { data: firstFetch },
}, {
request: { query },
result: { data: secondFetch },
});
});

afterEach(() => {
if (clock) {
clock.reset();
}
});

it('forces the query to rerun', () => {
const client = new ApolloClient({
networkInterface,
});

// Run a query first to initialize the store
return client.query({ query })
// then query for real
.then(() => client.query({ query, forceFetch: true }))
.then((result) => {
assert.deepEqual(result.data, { myNumber: { n: 2 } });
});
});

it('can be disabled with ssrMode', () => {
const client = new ApolloClient({
networkInterface,
ssrMode: true,
});

// Run a query first to initialize the store
return client.query({ query })
// then query for real
.then(() => client.query({ query, forceFetch: true }))
.then((result) => {
assert.deepEqual(result.data, { myNumber: { n: 1 } });
});
});

it('can temporarily be disabled with ssrForceFetchDelay', () => {
clock = sinon.useFakeTimers();

const client = new ApolloClient({
networkInterface,
ssrForceFetchDelay: 100,
});

// Run a query first to initialize the store
const outerPromise = client.query({ query })
// then query for real
.then(() => {
const promise = client.query({ query, forceFetch: true });
clock.tick(0);
return promise;
})
.then((result) => {
assert.deepEqual(result.data, { myNumber: { n: 1 } });
clock.tick(100);
const promise = client.query({ query, forceFetch: true });
clock.tick(0);
return promise;
})
.then((result) => {
assert.deepEqual(result.data, { myNumber: { n: 2 } });
});
clock.tick(0);
return outerPromise;
});
});
});
1 change: 1 addition & 0 deletions typings/browser/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@
/// <reference path="globals/promises-a-plus/index.d.ts" />
/// <reference path="globals/redux/index.d.ts" />
/// <reference path="modules/lodash/index.d.ts" />
/// <reference path="modules/sinon/index.d.ts" />
Loading