-
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
Mutation reducers #404
Merged
Merged
Mutation reducers #404
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
dd3a916
mutations now can implement reducers for result behavior
Slava accf97c
tests for optimistic behavior
Slava eabfed3
optimistic implementation (wrong) for updateQueries
Slava 6321493
Write field shouldn't rewrite the object reference if the field value…
Slava 35dba90
Add a test with an error handling
Slava f0c55fb
update the changelog
Slava f55bad1
changelog
Slava fa5218e
Mostly cosmetic changes + comments
Slava File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -41,6 +41,7 @@ import { | |
GraphQLResult, | ||
Document, | ||
FragmentDefinition, | ||
OperationDefinition, | ||
} from 'graphql'; | ||
|
||
import { print } from 'graphql-tag/printer'; | ||
|
@@ -55,6 +56,7 @@ import { | |
|
||
import { | ||
MutationBehavior, | ||
MutationQueryReducersMap, | ||
} from './data/mutationResults'; | ||
|
||
import { | ||
|
@@ -96,7 +98,7 @@ export class ObservableQuery extends Observable<ApolloQueryResult> { | |
public startPolling: (p: number) => void; | ||
public options: WatchQueryOptions; | ||
public queryManager: QueryManager; | ||
private queryId: string; | ||
public queryId: string; | ||
|
||
constructor({ | ||
queryManager, | ||
|
@@ -282,15 +284,17 @@ export class QueryManager { | |
public mutate({ | ||
mutation, | ||
variables, | ||
resultBehaviors, | ||
resultBehaviors = [], | ||
fragments = [], | ||
optimisticResponse, | ||
updateQueries, | ||
}: { | ||
mutation: Document, | ||
variables?: Object, | ||
resultBehaviors?: MutationBehavior[], | ||
fragments?: FragmentDefinition[], | ||
optimisticResponse?: Object, | ||
updateQueries?: MutationQueryReducersMap, | ||
}): Promise<ApolloQueryResult> { | ||
const mutationId = this.generateQueryId(); | ||
|
||
|
@@ -313,6 +317,14 @@ export class QueryManager { | |
operationName: getOperationName(mutation), | ||
} as Request; | ||
|
||
// Right now the way `updateQueries` feature is implemented relies on using | ||
// `resultBehaviors`, another feature that accomplishes the same goal but | ||
// provides more verbose syntax. | ||
// In the future we want to re-factor this part of code to avoid using | ||
// `resultBehaviors` so we can remove `resultBehaviors` entirely. | ||
const updateQueriesResultBehaviors = !optimisticResponse ? [] : | ||
this.collectResultBehaviorsFromUpdateQueries(updateQueries, { data: optimisticResponse }, true); | ||
|
||
this.store.dispatch({ | ||
type: 'APOLLO_MUTATION_INIT', | ||
mutationString, | ||
|
@@ -325,7 +337,7 @@ export class QueryManager { | |
mutationId, | ||
fragmentMap: queryFragmentMap, | ||
optimisticResponse, | ||
resultBehaviors, | ||
resultBehaviors: [...resultBehaviors, ...updateQueriesResultBehaviors], | ||
}); | ||
|
||
return this.networkInterface.query(request) | ||
|
@@ -334,7 +346,10 @@ export class QueryManager { | |
type: 'APOLLO_MUTATION_RESULT', | ||
result, | ||
mutationId, | ||
resultBehaviors, | ||
resultBehaviors: [ | ||
...resultBehaviors, | ||
...this.collectResultBehaviorsFromUpdateQueries(updateQueries, result), | ||
], | ||
}); | ||
|
||
return result; | ||
|
@@ -344,7 +359,6 @@ export class QueryManager { | |
type: 'APOLLO_MUTATION_ERROR', | ||
error: err, | ||
mutationId, | ||
resultBehaviors, | ||
}); | ||
|
||
return Promise.reject(err); | ||
|
@@ -584,6 +598,64 @@ export class QueryManager { | |
}); | ||
} | ||
|
||
private collectResultBehaviorsFromUpdateQueries( | ||
updateQueries: MutationQueryReducersMap, | ||
mutationResult: Object, | ||
isOptimistic = false | ||
): MutationBehavior[] { | ||
if (!updateQueries) { | ||
return []; | ||
} | ||
const resultBehaviors = []; | ||
|
||
const observableQueriesByName: { [name: string]: ObservableQuery[] } = {}; | ||
Object.keys(this.observableQueries).forEach((key) => { | ||
const observableQuery = this.observableQueries[key].observableQuery; | ||
const queryName = getQueryDefinition(observableQuery.options.query).name.value; | ||
|
||
observableQueriesByName[queryName] = | ||
observableQueriesByName[queryName] || []; | ||
observableQueriesByName[queryName].push(observableQuery); | ||
}); | ||
|
||
Object.keys(updateQueries).forEach((queryName) => { | ||
const reducer = updateQueries[queryName]; | ||
const queries = observableQueriesByName[queryName]; | ||
if (!queries) { | ||
// XXX should throw an error? | ||
return; | ||
} | ||
|
||
queries.forEach((observableQuery) => { | ||
const queryOptions = observableQuery.options; | ||
const queryDefinition: OperationDefinition = getQueryDefinition(queryOptions.query); | ||
const previousResult = readSelectionSetFromStore({ | ||
// In case of an optimistic change, apply reducer on top of the | ||
// results including previous optimistic updates. Otherwise, apply it | ||
// on top of the real data only. | ||
store: isOptimistic ? this.getDataWithOptimisticResults() : this.getApolloState().data, | ||
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. I'd add a comment about why we have this conditional - it's because we want to apply new optimistic results on top of old optimistic results, but real results on top of real data only. |
||
rootId: 'ROOT_QUERY', | ||
selectionSet: queryDefinition.selectionSet, | ||
variables: queryOptions.variables, | ||
returnPartialData: queryOptions.returnPartialData || queryOptions.noFetch, | ||
fragmentMap: createFragmentMap(queryOptions.fragments || []), | ||
}); | ||
|
||
resultBehaviors.push({ | ||
type: 'QUERY_RESULT', | ||
newResult: reducer(previousResult, { | ||
mutationResult, | ||
queryName, | ||
queryVariables: queryOptions.variables, | ||
}), | ||
queryOptions, | ||
}); | ||
}); | ||
}); | ||
|
||
return resultBehaviors; | ||
} | ||
|
||
private fetchQueryOverInterface( | ||
queryId: string, | ||
options: WatchQueryOptions, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
I don't think this should throw an error, because it's pretty reasonable to have some that are not currently on the page... but I agree it could be useful to have something, since people might be confused about why their code is not running.
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.
Defaulting to not throwing anything here.