cache PetAppearance for quick pose switches

This commit is contained in:
Matt Dunn-Rankin 2020-05-02 22:37:52 -07:00
parent 6e1e0a5c0b
commit f828686ac5
4 changed files with 34 additions and 9 deletions

View file

@ -19,6 +19,15 @@ const client = new ApolloClient({
// the server again! // the server again!
items: (_, args, { getCacheKey }) => items: (_, args, { getCacheKey }) =>
args.ids.map((id) => getCacheKey({ __typename: "Item", id })), args.ids.map((id) => getCacheKey({ __typename: "Item", id })),
// Teach Apollo how to serve `petAppearance` queries from the cache. That
// way, when you switch pet poses, Apollo knows it already has the
// appearance data and doesn't need to ask the server again!
petAppearance: (_, args, { getCacheKey }) => {
const { speciesId, colorId, emotion, genderPresentation } = args;
const id = `${speciesId}-${colorId}-${emotion}-${genderPresentation}`;
return getCacheKey({ __typename: "PetAppearance", id });
},
}, },
}, },
}); });

View file

@ -48,6 +48,7 @@ const typeDefs = gql`
type PetAppearance { type PetAppearance {
id: ID! id: ID!
petStateId: ID!
genderPresentation: GenderPresentation genderPresentation: GenderPresentation
emotion: Emotion emotion: Emotion
approximateThumbnailUrl: String! approximateThumbnailUrl: String!
@ -166,7 +167,13 @@ const resolvers = {
}, },
}, },
PetAppearance: { PetAppearance: {
id: ({ petState }) => petState.id, id: ({ petType, petState }) => {
const { speciesId, colorId } = petType;
const emotion = getEmotion(petState.moodId);
const genderPresentation = getGenderPresentation(petState.female);
return `${speciesId}-${colorId}-${emotion}-${genderPresentation}`;
},
petStateId: ({ petState }) => petState.id,
genderPresentation: ({ petState }) => genderPresentation: ({ petState }) =>
getGenderPresentation(petState.female), getGenderPresentation(petState.female),
emotion: ({ petState }) => getEmotion(petState.moodId), emotion: ({ petState }) => getEmotion(petState.moodId),

View file

@ -74,6 +74,7 @@ describe("PetAppearance", () => {
query { query {
petAppearances(speciesId: "54", colorId: "75") { petAppearances(speciesId: "54", colorId: "75") {
id id
petStateId
genderPresentation genderPresentation
emotion emotion
approximateThumbnailUrl approximateThumbnailUrl

View file

@ -58,7 +58,7 @@ Object {
"approximateThumbnailUrl": "http://pets.neopets.com/cp/vghhzlgf/1/1.png", "approximateThumbnailUrl": "http://pets.neopets.com/cp/vghhzlgf/1/1.png",
"emotion": "HAPPY", "emotion": "HAPPY",
"genderPresentation": "FEMININE", "genderPresentation": "FEMININE",
"id": "17723", "id": "54-75-HAPPY-FEMININE",
"layers": Array [ "layers": Array [
Object { Object {
"id": "5995", "id": "5995",
@ -103,12 +103,13 @@ Object {
}, },
}, },
], ],
"petStateId": "17723",
}, },
Object { Object {
"approximateThumbnailUrl": "http://pets.neopets.com/cp/vghhzlgf/1/1.png", "approximateThumbnailUrl": "http://pets.neopets.com/cp/vghhzlgf/1/1.png",
"emotion": "HAPPY", "emotion": "HAPPY",
"genderPresentation": "MASCULINE", "genderPresentation": "MASCULINE",
"id": "17742", "id": "54-75-HAPPY-MASCULINE",
"layers": Array [ "layers": Array [
Object { Object {
"id": "5995", "id": "5995",
@ -153,12 +154,13 @@ Object {
}, },
}, },
], ],
"petStateId": "17742",
}, },
Object { Object {
"approximateThumbnailUrl": "http://pets.neopets.com/cp/vghhzlgf/4/1.png", "approximateThumbnailUrl": "http://pets.neopets.com/cp/vghhzlgf/4/1.png",
"emotion": "SICK", "emotion": "SICK",
"genderPresentation": "FEMININE", "genderPresentation": "FEMININE",
"id": "10014", "id": "54-75-SICK-FEMININE",
"layers": Array [ "layers": Array [
Object { Object {
"id": "5995", "id": "5995",
@ -203,12 +205,13 @@ Object {
}, },
}, },
], ],
"petStateId": "10014",
}, },
Object { Object {
"approximateThumbnailUrl": "http://pets.neopets.com/cp/vghhzlgf/4/1.png", "approximateThumbnailUrl": "http://pets.neopets.com/cp/vghhzlgf/4/1.png",
"emotion": "SICK", "emotion": "SICK",
"genderPresentation": "MASCULINE", "genderPresentation": "MASCULINE",
"id": "11089", "id": "54-75-SICK-MASCULINE",
"layers": Array [ "layers": Array [
Object { Object {
"id": "5995", "id": "5995",
@ -253,12 +256,13 @@ Object {
}, },
}, },
], ],
"petStateId": "11089",
}, },
Object { Object {
"approximateThumbnailUrl": "http://pets.neopets.com/cp/vghhzlgf/2/1.png", "approximateThumbnailUrl": "http://pets.neopets.com/cp/vghhzlgf/2/1.png",
"emotion": "SAD", "emotion": "SAD",
"genderPresentation": "FEMININE", "genderPresentation": "FEMININE",
"id": "5991", "id": "54-75-SAD-FEMININE",
"layers": Array [ "layers": Array [
Object { Object {
"id": "5995", "id": "5995",
@ -303,12 +307,13 @@ Object {
}, },
}, },
], ],
"petStateId": "5991",
}, },
Object { Object {
"approximateThumbnailUrl": "http://pets.neopets.com/cp/vghhzlgf/2/1.png", "approximateThumbnailUrl": "http://pets.neopets.com/cp/vghhzlgf/2/1.png",
"emotion": "SAD", "emotion": "SAD",
"genderPresentation": "MASCULINE", "genderPresentation": "MASCULINE",
"id": "436", "id": "54-75-SAD-MASCULINE",
"layers": Array [ "layers": Array [
Object { Object {
"id": "5995", "id": "5995",
@ -353,12 +358,13 @@ Object {
}, },
}, },
], ],
"petStateId": "436",
}, },
Object { Object {
"approximateThumbnailUrl": "http://pets.neopets.com/cp/vghhzlgf/null/1.png", "approximateThumbnailUrl": "http://pets.neopets.com/cp/vghhzlgf/null/1.png",
"emotion": null, "emotion": null,
"genderPresentation": null, "genderPresentation": null,
"id": "2", "id": "54-75-null-null",
"layers": Array [ "layers": Array [
Object { Object {
"id": "5995", "id": "5995",
@ -410,12 +416,13 @@ Object {
}, },
}, },
], ],
"petStateId": "2",
}, },
Object { Object {
"approximateThumbnailUrl": "http://pets.neopets.com/cp/vghhzlgf/null/1.png", "approximateThumbnailUrl": "http://pets.neopets.com/cp/vghhzlgf/null/1.png",
"emotion": null, "emotion": null,
"genderPresentation": null, "genderPresentation": null,
"id": "4751", "id": "54-75-null-null",
"layers": Array [ "layers": Array [
Object { Object {
"id": "5995", "id": "5995",
@ -467,6 +474,7 @@ Object {
}, },
}, },
], ],
"petStateId": "4751",
}, },
], ],
} }