type Witch {
name: String
house: String
wand: String
patronus: String
age: Int
}
const resolvers = {
Query: {
witch(parent, args, context, info) {
// the logic for finding the witch
return find(witch, { id: args.id });
},
},
}
const typeDefs = gql`
extend type Spell {
isSelected: Boolean
}
`
const SpellResolver = {
isSelected: (spell: Spell) =>
spell.isSelected === undefined ?
false : spell.isSelected,
}
const resolvers = {
Spell: SpellResolver,
}
query spells($path: String) {
spells @rest(type: "Spell", path: $path) {
id
spell
type
effect
isSelected @client
}
}
const Mutation = {
setSpellSelect:
(parent, args, { cache, getCacheKey }) => {
const id = getCacheKey({
__typename: 'Spell',
id: args.id
})
const fragment = gql`
fragment SpellSelected on Spell {
isSelected @client
}
`
const spell = cache.readFragment({ fragment, id })
const data = { ...spell, isSelected: !spell.isSelected}
cache.writeFragment({ id, data })
}
}
const resolvers = {
Spell: SpellResolver,
Mutation
}
mutation toggleSpellSelect($id: ID) {
setSpellSelect(id: $id) @clint {
id
}
}
Warning: In video there is flashing as colors change from black to white
cache.writeData({
data: {
theme: {
mode: 'light',
__typename: 'Theme'
}
}
})
const typeDefs = gql`
//...
type Theme {
mode: String
__typename: String
}
extend type Query {
theme: Theme
}
`
query getTheme {
theme @client {
mode
}
}
const Mutation = {
//...
setTheme: (_: any, __: any, { cache }: Context) => {
const query = GET_THEME;
const prev = cache.readQuery({ query });
const data = {
...prev,
theme: {
...prev.theme,
mode: prev.theme.mode === 'light' ?
'dark' : 'light',
},
};
cache.writeQuery({ query, data });
},
};
query getTheme {
theme @client {
mode
}
}
const Mutation = {
//...
setTheme: (_: any, __: any, { cache }: Context) => {
const query = GET_THEME;
const prev = cache.readQuery({ query });
const data = {
...prev,
theme: {
...prev.theme,
mode: prev.theme.mode === 'light' ?
'dark' : 'light',
},
};
cache.writeQuery({ query, data });
},
};
mutation setTheme {
setTheme @client {
mode
}
}