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!
items: (_, args, { getCacheKey }) =>
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 {
id: ID!
petStateId: ID!
genderPresentation: GenderPresentation
emotion: Emotion
approximateThumbnailUrl: String!
@ -166,7 +167,13 @@ const resolvers = {
},
},
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 }) =>
getGenderPresentation(petState.female),
emotion: ({ petState }) => getEmotion(petState.moodId),

View file

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

View file

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