Skip to content

Commit

Permalink
Allow client-side schema extensions in optimisticResponse
Browse files Browse the repository at this point in the history
Summary:
Setting client-side schema extension fields in `optimisticResponse` works fine, but `validateMutation` throws a warning. I've updated `validateMutation` to allow this.

My use case is that I have an "is_saving" flag I'm adding to an object using "Client Schema Extensions". I want to set this flag when a mutation is being performed, and unset it when the mutation is complete. I want to display the state of the mutation in several places in the UI, and using local Relay state seemed like an easier way to do that compared to having to pass some React state through several components. Setting this field using `optimisticResponse` works well because Relay will automatically revert the change after the mutation is complete.

Reviewed By: bennyhobart

Differential Revision: D20228541

fbshipit-source-id: f9ed06739bc0155b29f3da2c28bc727324fc1a16
  • Loading branch information
Daniel15 authored and facebook-github-bot committed Mar 4, 2020
1 parent aaa9588 commit b00736e
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -553,6 +553,68 @@ describe('validateOptimisticResponse', () => {
variables: null,
shouldWarn: true,
},
{
name: 'Does not log a warning for client-side schema extensions',
mutation: generateAndCompile(
`
extend type Feedback {
isSavingLike: Boolean
}
mutation FeedbackLikeMutation(
$input: FeedbackLikeInput
) {
feedbackLike(input: $input) {
feedback {
doesViewerLike
isSavingLike
}
}
}
`,
).FeedbackLikeMutation,
optimisticResponse: {
feedbackLike: {
feedback: {
id: 1,
doesViewerLike: true,
isSavingLike: true,
},
},
},
variables: null,
shouldWarn: false,
},
{
name: 'Logs a warning for invalid client-side schema extension fields',
mutation: generateAndCompile(
`
extend type Feedback {
isSavingLike: Boolean
}
mutation FeedbackLikeMutation(
$input: FeedbackLikeInput
) {
feedbackLike(input: $input) {
feedback {
doesViewerLike
isSavingLike
}
}
}
`,
).FeedbackLikeMutation,
optimisticResponse: {
feedbackLike: {
feedback: {
id: 1,
doesViewerLike: true,
someInvalidField: true,
},
},
},
variables: null,
shouldWarn: true,
},
].forEach(({name, mutation, optimisticResponse, shouldWarn, variables}) => {
it(name, () => {
jest.clearAllMocks();
Expand Down
4 changes: 4 additions & 0 deletions packages/relay-runtime/mutations/validateMutation.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,10 @@ if (__DEV__) {
});
return;
case 'ClientExtension':
selection.selections.forEach(subselection => {
validateSelection(optimisticResponse, subselection, context);
});
return;
case 'ModuleImport':
case 'LinkedHandle':
case 'ScalarHandle':
Expand Down

0 comments on commit b00736e

Please sign in to comment.