Skip to content

Commit 350dcec

Browse files
committed
tweak mutual best friend setter
1 parent 6a6ac7e commit 350dcec

File tree

6 files changed

+87
-20
lines changed

6 files changed

+87
-20
lines changed

crates/generate_artifacts/src/generate_artifacts.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -291,14 +291,14 @@ fn get_artifact_path_and_content_impl<TNetworkProtocol: NetworkProtocol>(
291291
default_value: None,
292292
};
293293

294-
let (wrapped_map, variable_definitions_iter): (_, Vec<_>) =
294+
let (wrapped_map, variable_definitions_iter) =
295295
match client_scalar_selectable.refetch_strategy.as_ref().expect(
296296
"Expected a refetch strategy. \
297-
This is indicative of a bug in Isograph.",
297+
This is indicative of a bug in Isograph.",
298298
) {
299299
RefetchStrategy::RefetchFromRoot => (
300300
selection_map_wrapped(merged_selection_map.clone(), vec![]),
301-
variable_definitions_iter.collect(),
301+
variable_definitions_iter.collect::<Vec<_>>(),
302302
),
303303
RefetchStrategy::UseRefetchField(_) => {
304304
let wrapped_map = selection_map_wrapped(

demos/pet-demo/src/components/Pet/MutualBestFriendSetter.tsx

Lines changed: 40 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,15 @@ import {
44
useImperativeReference,
55
} from '@isograph/react';
66
import { iso } from '../__isograph/iso';
7-
import { Button } from '@mui/material';
7+
import { Button, Card, CardContent } from '@mui/material';
8+
9+
/**
10+
* This is a bit of a contrived demo that shows that @loadable fields on
11+
* root mutations work.
12+
*
13+
* Note that we are fetching some additional, unused, fields, so that
14+
* the PetBestFriendCard continues to work. This is somewhat awkward!
15+
*/
816

917
export const setMututalBestFriend = iso(`
1018
field Mutation.MututalBestFriendSetterMutation(
@@ -18,16 +26,19 @@ export const setMututalBestFriend = iso(`
1826
pet {
1927
id
2028
best_friend_relationship {
29+
picture_together
2130
best_friend {
2231
id
32+
name
33+
Avatar
2334
}
2435
}
2536
}
2637
}
2738
MutualBestFriendSetterOtherSide @loadable
2839
}
2940
`)(({ data }) => {
30-
if (!data.set_pet_best_friend.pet.best_friend_relationship?.best_friend.id) {
41+
if (!data.set_pet_best_friend.pet.best_friend_relationship) {
3142
throw new Error('Somehow the new best friend id was not set');
3243
}
3344

@@ -86,21 +97,33 @@ export const MutualBestFriendSetter = iso(`
8697
iso(`entrypoint Mutation.MututalBestFriendSetterMutation`),
8798
);
8899

89-
if (!mutationRef) {
90-
return (
91-
<Button
92-
onClick={() => {
93-
loadMutation({
100+
const cardContent = !mutationRef ? (
101+
<Button
102+
onClick={() => {
103+
loadMutation(
104+
{
94105
id: data.id,
95106
new_best_friend_id: '0',
96-
});
97-
}}
98-
variant="contained"
99-
>
100-
Set best friend to Makayla
101-
</Button>
102-
);
103-
} else {
104-
return <FragmentRenderer fragmentReference={mutationRef} />;
105-
}
107+
},
108+
{
109+
shouldFetch: 'Yes',
110+
},
111+
);
112+
}}
113+
variant="contained"
114+
>
115+
Set best friend to Makayla
116+
</Button>
117+
) : (
118+
<FragmentRenderer fragmentReference={mutationRef} />
119+
);
120+
121+
return (
122+
<Card
123+
variant="outlined"
124+
sx={{ width: 450, boxShadow: 3, backgroundColor: '#BBB' }}
125+
>
126+
<CardContent>{cardContent}</CardContent>
127+
</Card>
128+
);
106129
});

demos/pet-demo/src/components/__isograph/Mutation/MututalBestFriendSetterMutation/normalization_ast.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,23 @@ const normalizationAst: NormalizationAst = {
4646
fieldName: "id",
4747
arguments: null,
4848
},
49+
{
50+
kind: "Scalar",
51+
fieldName: "name",
52+
arguments: null,
53+
},
54+
{
55+
kind: "Scalar",
56+
fieldName: "picture",
57+
arguments: null,
58+
},
4959
],
5060
},
61+
{
62+
kind: "Scalar",
63+
fieldName: "picture_together",
64+
arguments: null,
65+
},
5166
],
5267
},
5368
],

demos/pet-demo/src/components/__isograph/Mutation/MututalBestFriendSetterMutation/param_type.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { type Mutation__MutualBestFriendSetterOtherSide__output_type } from '../../Mutation/MutualBestFriendSetterOtherSide/output_type';
2+
import { type Pet__Avatar__output_type } from '../../Pet/Avatar/output_type';
23
import { type LoadableField, type ExtractParameters } from '@isograph/react';
34
import { type Mutation__MutualBestFriendSetterOtherSide__param } from '../../Mutation/MutualBestFriendSetterOtherSide/param_type';
45
import type { Mutation__MututalBestFriendSetterMutation__parameters } from './parameters_type';
@@ -9,8 +10,11 @@ export type Mutation__MututalBestFriendSetterMutation__param = {
910
readonly pet: {
1011
readonly id: string,
1112
readonly best_friend_relationship: ({
13+
readonly picture_together: (string | null),
1214
readonly best_friend: {
1315
readonly id: string,
16+
readonly name: string,
17+
readonly Avatar: Pet__Avatar__output_type,
1418
},
1519
} | null),
1620
},

demos/pet-demo/src/components/__isograph/Mutation/MututalBestFriendSetterMutation/query_text.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@ export default 'mutation MututalBestFriendSetterMutation($id: ID!, $new_best_fri
55
best_friend_relationship {\
66
best_friend {\
77
id,\
8+
name,\
9+
picture,\
810
},\
11+
picture_together,\
912
},\
1013
},\
1114
},\

demos/pet-demo/src/components/__isograph/Mutation/MututalBestFriendSetterMutation/resolver_reader.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type {ComponentReaderArtifact, ExtractSecondParam, ReaderAst } from '@iso
22
import { Mutation__MututalBestFriendSetterMutation__param } from './param_type';
33
import { setMututalBestFriend as resolver } from '../../../Pet/MutualBestFriendSetter';
44
import Mutation__MutualBestFriendSetterOtherSide__entrypoint from '../../Mutation/MutualBestFriendSetterOtherSide/entrypoint';
5+
import Pet__Avatar__resolver_reader from '../../Pet/Avatar/resolver_reader';
56

67
const readerAst: ReaderAst<Mutation__MututalBestFriendSetterMutation__param> = [
78
{
@@ -48,6 +49,13 @@ const readerAst: ReaderAst<Mutation__MututalBestFriendSetterMutation__param> = [
4849
isUpdatable: false,
4950
refetchQueryIndex: null,
5051
selections: [
52+
{
53+
kind: "Scalar",
54+
fieldName: "picture_together",
55+
alias: null,
56+
arguments: null,
57+
isUpdatable: false,
58+
},
5159
{
5260
kind: "Linked",
5361
fieldName: "best_friend",
@@ -64,6 +72,20 @@ const readerAst: ReaderAst<Mutation__MututalBestFriendSetterMutation__param> = [
6472
arguments: null,
6573
isUpdatable: false,
6674
},
75+
{
76+
kind: "Scalar",
77+
fieldName: "name",
78+
alias: null,
79+
arguments: null,
80+
isUpdatable: false,
81+
},
82+
{
83+
kind: "Resolver",
84+
alias: "Avatar",
85+
arguments: null,
86+
readerArtifact: Pet__Avatar__resolver_reader,
87+
usedRefetchQueries: [],
88+
},
6789
],
6890
},
6991
],

0 commit comments

Comments
 (0)