-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
Local fetch option in WatchQueryOptions #385
Changes from 5 commits
c202cf1
ec8fb1d
dc72ad0
f6d88f7
2e997d7
d394072
2bc13b8
be9d17d
13d8e5b
978419b
4cb8bfa
469f02a
64c45cf
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -118,6 +118,7 @@ export interface WatchQueryOptions { | |
variables?: { [key: string]: any }; | ||
forceFetch?: boolean; | ||
returnPartialData?: boolean; | ||
noFetch?: boolean; | ||
pollInterval?: number; | ||
fragments?: FragmentDefinition[]; | ||
} | ||
|
@@ -308,6 +309,7 @@ export class QueryManager { | |
selectionSet: queryStoreValue.query.selectionSet, | ||
variables: queryStoreValue.variables, | ||
returnPartialData: options.returnPartialData, | ||
noFetch: options.noFetch, | ||
fragmentMap: queryStoreValue.fragmentMap, | ||
}); | ||
|
||
|
@@ -355,6 +357,7 @@ export class QueryManager { | |
this.queryListenerForObserver(options, observer) | ||
); | ||
|
||
|
||
return retQuerySubscription; | ||
}; | ||
|
||
|
@@ -364,7 +367,7 @@ export class QueryManager { | |
|
||
// Use the same options as before, but with new variables and forceFetch true | ||
return this.fetchQuery(queryId, assign(options, { | ||
forceFetch: true, | ||
forceFetch: options.noFetch ? false : true, | ||
variables, | ||
}) as WatchQueryOptions); | ||
}; | ||
|
@@ -379,7 +382,7 @@ export class QueryManager { | |
this.pollingTimers[queryId] = setInterval(() => { | ||
const pollingOptions = assign({}, options) as WatchQueryOptions; | ||
// subsequent fetches from polling always reqeust new data | ||
pollingOptions.forceFetch = true; | ||
pollingOptions.forceFetch = options.noFetch ? false : true; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of doing this, we should not start the polling timer. I think if someone passed |
||
this.fetchQuery(queryId, pollingOptions); | ||
}, pollInterval); | ||
}; | ||
|
@@ -520,6 +523,7 @@ export class QueryManager { | |
variables, | ||
forceFetch = false, | ||
returnPartialData = false, | ||
noFetch = false, | ||
fragments = [], | ||
} = options; | ||
|
||
|
@@ -566,7 +570,7 @@ export class QueryManager { | |
|
||
initialResult = result; | ||
|
||
if (missingSelectionSets && missingSelectionSets.length) { | ||
if (missingSelectionSets && missingSelectionSets.length && !noFetch) { | ||
const diffedQuery = queryDocument({ | ||
missingSelectionSets, | ||
variableDefinitions: queryDef.variableDefinitions, | ||
|
@@ -602,12 +606,13 @@ export class QueryManager { | |
variables, | ||
forceFetch, | ||
returnPartialData, | ||
noFetch, | ||
queryId, | ||
requestId, | ||
fragmentMap: queryFragmentMap, | ||
}); | ||
|
||
if (! minimizedQuery || returnPartialData) { | ||
if (! minimizedQuery || returnPartialData || noFetch) { | ||
this.store.dispatch({ | ||
type: 'APOLLO_QUERY_RESULT_CLIENT', | ||
result: { | ||
|
@@ -666,6 +671,7 @@ export class QueryManager { | |
selectionSet: querySS.selectionSet, | ||
variables, | ||
returnPartialData: returnPartialData, | ||
noFetch: noFetch, | ||
fragmentMap: queryFragmentMap, | ||
}); | ||
// ensure multiple errors don't get thrown | ||
|
@@ -701,6 +707,9 @@ export class QueryManager { | |
|
||
private startQuery(queryId: string, options: WatchQueryOptions, listener: QueryListener) { | ||
this.queryListeners[queryId] = listener; | ||
if (options.noFetch) { | ||
options.pollInterval = null; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If someone passes |
||
} | ||
this.fetchQuery(queryId, options); | ||
|
||
if (options.pollInterval) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,11 +26,13 @@ export function readQueryFromStore({ | |
query, | ||
variables, | ||
returnPartialData, | ||
noFetch, | ||
}: { | ||
store: NormalizedCache, | ||
query: Document, | ||
variables?: Object, | ||
returnPartialData?: boolean, | ||
noFetch?: boolean, | ||
}): Object { | ||
const queryDef = getQueryDefinition(query); | ||
|
||
|
@@ -40,6 +42,7 @@ export function readQueryFromStore({ | |
selectionSet: queryDef.selectionSet, | ||
variables, | ||
returnPartialData, | ||
noFetch, | ||
}); | ||
} | ||
|
||
|
@@ -49,12 +52,14 @@ export function readFragmentFromStore({ | |
rootId, | ||
variables, | ||
returnPartialData, | ||
noFetch, | ||
}: { | ||
store: NormalizedCache, | ||
fragment: Document, | ||
rootId: string, | ||
variables?: Object, | ||
returnPartialData?: boolean, | ||
noFetch?: boolean, | ||
}): Object { | ||
const fragmentDef = getFragmentDefinition(fragment); | ||
|
||
|
@@ -64,6 +69,7 @@ export function readFragmentFromStore({ | |
selectionSet: fragmentDef.selectionSet, | ||
variables, | ||
returnPartialData, | ||
noFetch, | ||
}); | ||
} | ||
|
||
|
@@ -73,13 +79,15 @@ export function readSelectionSetFromStore({ | |
selectionSet, | ||
variables, | ||
returnPartialData = false, | ||
noFetch = false, | ||
fragmentMap, | ||
}: { | ||
store: NormalizedCache, | ||
rootId: string, | ||
selectionSet: SelectionSet, | ||
variables: Object, | ||
returnPartialData?: boolean, | ||
noFetch?: boolean, | ||
fragmentMap?: FragmentMap, | ||
}): Object { | ||
const { | ||
|
@@ -88,7 +96,7 @@ export function readSelectionSetFromStore({ | |
selectionSet, | ||
rootId, | ||
store, | ||
throwOnMissingField: !returnPartialData, | ||
throwOnMissingField: !returnPartialData && !noFetch, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we need to pass through |
||
variables, | ||
fragmentMap, | ||
}); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -967,6 +967,150 @@ describe('QueryManager', () => { | |
}); | ||
}); | ||
|
||
it('supports noFetch fetching only cached data', () => { | ||
const primeQuery = gql` | ||
query primeQuery { | ||
luke: people_one(id: 1) { | ||
name | ||
} | ||
} | ||
`; | ||
|
||
const complexQuery = gql` | ||
query complexQuery { | ||
luke: people_one(id: 1) { | ||
name | ||
} | ||
vader: people_one(id: 4) { | ||
name | ||
} | ||
} | ||
`; | ||
|
||
const data1 = { | ||
luke: { | ||
name: 'Luke Skywalker', | ||
}, | ||
}; | ||
|
||
const data2 = { | ||
luke: { | ||
name: 'Luke Skywalker', | ||
}, | ||
vader: { | ||
name: 'Darth Vader', | ||
}, | ||
}; | ||
|
||
const networkInterface = mockNetworkInterface( | ||
{ | ||
request: { query: primeQuery }, | ||
result: { data: data1 }, | ||
}, | ||
{ | ||
request: { query: complexQuery }, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since we are trying to ensure that the query won't be fetched, we should remove this from the network mock. |
||
result: { data: data2 }, | ||
} | ||
); | ||
|
||
const queryManager = new QueryManager({ | ||
networkInterface, | ||
store: createApolloStore(), | ||
reduxRootKey: 'apollo', | ||
}); | ||
|
||
// First, prime the cache | ||
queryManager.query({ | ||
query: primeQuery, | ||
}).then(() => { | ||
const handle = queryManager.watchQuery({ | ||
query: complexQuery, | ||
noFetch: true, | ||
}); | ||
|
||
return handle.result().then((result) => { | ||
assert.equal(result.data['luke'].name, 'Luke Skywalker'); | ||
assert.notProperty(result.data, 'vader'); | ||
}); | ||
}); | ||
}); | ||
|
||
it('supports noFetch refetching only cached data', () => { | ||
const primeQuery = gql` | ||
query primeQuery { | ||
luke: people_one(id: 1) { | ||
name | ||
} | ||
} | ||
`; | ||
|
||
const complexQuery = gql` | ||
query complexQuery { | ||
luke: people_one(id: 1) { | ||
name | ||
} | ||
vader: people_one(id: 4) { | ||
name | ||
} | ||
} | ||
`; | ||
|
||
const data1 = { | ||
luke: { | ||
name: 'Luke Skywalker', | ||
}, | ||
}; | ||
|
||
const data2 = { | ||
luke: { | ||
name: 'Luke Skywalker', | ||
}, | ||
vader: { | ||
name: 'Darth Vader', | ||
}, | ||
}; | ||
|
||
const networkInterface = mockNetworkInterface( | ||
{ | ||
request: { query: primeQuery }, | ||
result: { data: data1 }, | ||
}, | ||
{ | ||
request: { query: complexQuery }, | ||
result: { data: data2 }, | ||
} | ||
); | ||
|
||
const queryManager = new QueryManager({ | ||
networkInterface, | ||
store: createApolloStore(), | ||
reduxRootKey: 'apollo', | ||
}); | ||
|
||
// First, prime the cache | ||
queryManager.query({ | ||
query: primeQuery, | ||
}).then(() => { | ||
const handle = queryManager.watchQuery({ | ||
query: complexQuery, | ||
noFetch: true, | ||
}); | ||
let handleCount = 0; | ||
handle.subscribe({ | ||
next(result) { | ||
handleCount++; | ||
|
||
if (handleCount === 1) { | ||
assert.deepEqual(result.data, data1); | ||
handle.refetch(); | ||
} else if (handleCount === 2) { | ||
assert.deepEqual(result.data, data1); | ||
} | ||
}, | ||
}); | ||
}); | ||
}); | ||
|
||
it('runs a mutation', () => { | ||
const mutation = gql` | ||
mutation makeListPrivate { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not a valid markdown link :]
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oops