Skip to content

Commit a5a43ad

Browse files
authored
[codex] Fix no-variable operations with provided variables (#622)
* Add provided variable mutation repro * Normalize operation variables before Relay calls * Add changelog entry for provided variable fix
1 parent 34b6108 commit a5a43ad

12 files changed

Lines changed: 390 additions & 15 deletions

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# master
22

3+
- Fix operations with no user supplied variables crashing when they include provided variables.
4+
35
# 4.4.1
46

57
- Change name on internal fetch fn to avoid accidental shadowing.

packages/rescript-relay/__tests__/Test_mutation-tests.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,47 @@ describe("Mutation", () => {
159159
await t.screen.findByText("First is idle");
160160
});
161161

162+
test("mutation with no variables can spread a provided variable fragment", async () => {
163+
queryMock.mockQuery({
164+
name: "TestMutationQuery",
165+
data: {
166+
loggedInUser: {
167+
id: "user-1",
168+
firstName: "First",
169+
lastName: "Name",
170+
onlineStatus: "Online",
171+
memberOf,
172+
},
173+
},
174+
});
175+
176+
t.render(test_mutation());
177+
await t.screen.findByText("Provided variable mutation status: -");
178+
179+
queryMock.mockQuery({
180+
name: "TestMutationWithProvidedVariableFragmentMutation",
181+
variables: {
182+
__relay_internal__pv__ProvidedVariablesBool: true,
183+
},
184+
data: {
185+
updateUserAvatar: {
186+
user: {
187+
id: "user-1",
188+
someRandomArgField: "provided variable value",
189+
},
190+
},
191+
},
192+
});
193+
194+
ReactTestUtils.act(() => {
195+
t.fireEvent.click(
196+
t.screen.getByText("Run mutation with provided variable fragment")
197+
);
198+
});
199+
200+
await t.screen.findByText("Provided variable mutation status: completed");
201+
});
202+
162203
test("optimistic response works", async () => {
163204
queryMock.mockQuery({
164205
name: "TestMutationQuery",

packages/rescript-relay/__tests__/Test_mutation.res

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,16 @@ module ComplexMutation = %relay(`
2929
}
3030
`)
3131

32+
module MutationWithProvidedVariableFragment = %relay(`
33+
mutation TestMutationWithProvidedVariableFragmentMutation {
34+
updateUserAvatar {
35+
user {
36+
...TestMutationProvidedVariable_user
37+
}
38+
}
39+
}
40+
`)
41+
3242
module MutationWithOnlyFragment = %relay(`
3343
mutation TestMutationWithOnlyFragmentSetOnlineStatusMutation($onlineStatus: OnlineStatus!) @raw_response_type {
3444
setOnlineStatus(onlineStatus: $onlineStatus) {
@@ -57,6 +67,15 @@ module Fragment = %relay(`
5767
}
5868
`)
5969

70+
module ProvidedVariableFragment = %relay(`
71+
fragment TestMutationProvidedVariable_user on User
72+
@argumentDefinitions(
73+
bool: { type: "Boolean!", provider: "ProvidedVariables.Bool" }
74+
) {
75+
someRandomArgField(bool: $bool)
76+
}
77+
`)
78+
6079
module MutationWithInlineFragment = %relay(`
6180
mutation TestMutationWithInlineFragmentSetOnlineStatusMutation($onlineStatus: OnlineStatus!) @raw_response_type {
6281
setOnlineStatus(onlineStatus: $onlineStatus) {
@@ -84,6 +103,9 @@ module Test = {
84103
let data = Fragment.use(query.loggedInUser.fragmentRefs)
85104
let (mutate, isMutating) = Mutation.use()
86105
let (inlineStatus, setInlineStatus) = React.useState(_ => "-")
106+
let (providedVariableMutationStatus, setProvidedVariableMutationStatus) = React.useState(_ =>
107+
"-"
108+
)
87109
let someJsonValue =
88110
[
89111
("foo", JSON.Encode.null),
@@ -114,6 +136,11 @@ module Test = {
114136
}),
115137
)}
116138
<div> {React.string("Inline status: " ++ inlineStatus)} </div>
139+
<div>
140+
{React.string(
141+
"Provided variable mutation status: " ++ providedVariableMutationStatus,
142+
)}
143+
</div>
117144
<button
118145
onClick={_ => {
119146
let _ = {
@@ -215,6 +242,20 @@ module Test = {
215242
>
216243
{React.string("Change online status, complex")}
217244
</button>
245+
<button
246+
onClick={_ => {
247+
open MutationWithProvidedVariableFragment
248+
commitMutation(~environment, ~variables=(), ~onCompleted=(response, _) => {
249+
switch response {
250+
| {updateUserAvatar: Some({user: Some(_)})} =>
251+
setProvidedVariableMutationStatus(_ => "completed")
252+
| _ => setProvidedVariableMutationStatus(_ => "missing user")
253+
}
254+
})->RescriptRelay.Disposable.ignore
255+
}}
256+
>
257+
{React.string("Run mutation with provided variable fragment")}
258+
</button>
218259
<button
219260
onClick={_ => {
220261
let _ = {

packages/rescript-relay/__tests__/__generated__/TestMutationProvidedVariable_user_graphql.res

Lines changed: 71 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)