special color mutation actually working!

Note that there's a bug when switching back to the null case… when I look in the Apollo dev tools, it's definitely getting set in the cache correctly at the right time… but the query isn't updating for some reason? I'm hoping it's an Apollo bug that will fix itself someday with an upgrade!
This commit is contained in:
Emi Matchu 2020-08-01 00:04:11 -07:00
parent a1d5669ac6
commit 0a9d736957
2 changed files with 40 additions and 11 deletions

View file

@ -79,6 +79,7 @@ function SpecialColorFields({ item }) {
gql` gql`
query ItemSupportDrawerManualSpecialColor($itemId: ID!) { query ItemSupportDrawerManualSpecialColor($itemId: ID!) {
item(id: $itemId) { item(id: $itemId) {
id
manualSpecialColor { manualSpecialColor {
id id
} }
@ -118,8 +119,8 @@ function SpecialColorFields({ item }) {
colorId: $colorId colorId: $colorId
supportSecret: $supportSecret supportSecret: $supportSecret
) { ) {
id
manualSpecialColor { manualSpecialColor {
__typename
id id
} }
} }
@ -141,6 +142,8 @@ function SpecialColorFields({ item }) {
? "Loading…" ? "Loading…"
: "Default: Auto-detect from item description" : "Default: Auto-detect from item description"
} }
value={itemData?.item?.manualSpecialColor?.id}
isDisabled={mutationLoading}
icon={ icon={
colorsLoading || itemLoading || mutationLoading ? ( colorsLoading || itemLoading || mutationLoading ? (
<Spinner /> <Spinner />
@ -148,19 +151,10 @@ function SpecialColorFields({ item }) {
<CheckCircleIcon /> <CheckCircleIcon />
) : undefined ) : undefined
} }
value={itemData?.item?.manualSpecialColor?.id}
onChange={(e) => { onChange={(e) => {
const colorId = e.target.value; const colorId = e.target.value || null;
const color = const color =
colorId != null ? { __typename: "Color", id: colorId } : null; colorId != null ? { __typename: "Color", id: colorId } : null;
console.log({
__typename: "Mutation",
setManualSpecialColor: {
__typename: "Item",
id: item.id,
manualSpecialColor: color,
},
});
mutate({ mutate({
variables: { variables: {
itemId: item.id, itemId: item.id,

View file

@ -171,6 +171,14 @@ const typeDefs = gql`
petOnNeopetsDotCom(petName: String!): Outfit petOnNeopetsDotCom(petName: String!): Outfit
} }
type Mutation {
setManualSpecialColor(
itemId: ID!
colorId: ID
supportSecret: String!
): Item!
}
`; `;
const resolvers = { const resolvers = {
@ -476,6 +484,32 @@ const resolvers = {
return outfit; return outfit;
}, },
}, },
Mutation: {
setManualSpecialColor: async (
_,
{ itemId, colorId, supportSecret },
{ db }
) => {
if (supportSecret !== process.env["SUPPORT_SECRET"]) {
throw new Error(`Support secret is incorrect. Try setting up again?`);
}
const [
result,
] = await db.execute(
`UPDATE items SET manual_special_color_id = ? WHERE id = ? LIMIT 1`,
[colorId, itemId]
);
if (result.affectedRows !== 1) {
throw new Error(
`Expected to affect 1 item, but affected ${result.affectedRows}`
);
}
return { id: itemId };
},
},
}; };
let lastSvgLogger = null; let lastSvgLogger = null;
@ -522,6 +556,7 @@ const config = {
return { return {
svgLogger, svgLogger,
db,
...buildLoaders(db), ...buildLoaders(db),
}; };
}, },