petOnNeopetsDotCom GQL improvements

We now support returning `null` from `petAppearance` when a pet genuinely has no customization data.

We also deprecate some old fields, and update our own call site to match.
This commit is contained in:
Emi Matchu 2022-09-17 22:07:34 -07:00
parent d058f46906
commit 773ec8974f
2 changed files with 65 additions and 42 deletions

View file

@ -217,6 +217,7 @@ function SubmitPetForm() {
gql` gql`
query SubmitPetForm($petName: String!) { query SubmitPetForm($petName: String!) {
petOnNeopetsDotCom(petName: $petName) { petOnNeopetsDotCom(petName: $petName) {
petAppearance {
color { color {
id id
} }
@ -224,7 +225,8 @@ function SubmitPetForm() {
id id
} }
pose pose
items { }
wornItems {
id id
} }
} }
@ -235,14 +237,25 @@ function SubmitPetForm() {
onCompleted: (data) => { onCompleted: (data) => {
if (!data) return; if (!data) return;
const { species, color, pose, items } = data.petOnNeopetsDotCom; const { petAppearance, wornItems } = data.petOnNeopetsDotCom;
if (petAppearance == null) {
toast({
title: "This pet exists, but is in a glitchy state on Neopets.com.",
description:
"Hopefully it gets fixed soon! If this doesn't sound right to you, contact us and let us know!",
status: "error",
});
return;
}
const { species, color, pose } = petAppearance;
const params = new URLSearchParams({ const params = new URLSearchParams({
name: petName, name: petName,
species: species.id, species: species.id,
color: color.id, color: color.id,
pose, pose,
}); });
for (const item of items) { for (const item of wornItems) {
params.append("objects[]", item.id); params.append("objects[]", item.id);
} }
pushHistory(`/outfits/new?${params}`); pushHistory(`/outfits/new?${params}`);

View file

@ -8,17 +8,13 @@ const typeDefs = gql`
type Pet { type Pet {
id: ID! id: ID!
name: String! name: String!
petAppearance: PetAppearance!
wornItems: [Item!]! wornItems: [Item!]!
"to be deprecated? can use petAppearance? 🤔" """
species: Species! The pet's appearance. Can be null if the pet is in an invalid buggy state
"to be deprecated? can use petAppearance? 🤔" that Neopets.com's own customization system doesn't support yet.
color: Color! """
"to be deprecated? can use petAppearance? 🤔" petAppearance: PetAppearance
pose: Pose!
"deprecated alias for wornItems"
items: [Item!]!
} }
extend type Query { extend type Query {
@ -29,19 +25,15 @@ const typeDefs = gql`
const resolvers = { const resolvers = {
Pet: { Pet: {
id: ({ name }) => name, id: ({ name }) => name,
species: ({ customPetData }) => ({
id: String(customPetData.custom_pet.species_id),
}),
color: ({ customPetData }) => ({
id: String(customPetData.custom_pet.color_id),
}),
pose: ({ customPetData, petMetaData }) =>
getPoseFromPetData(petMetaData, customPetData),
petAppearance: async ( petAppearance: async (
{ name, customPetData, petMetaData }, { name, customPetData, petMetaData },
_, _,
{ petTypeBySpeciesAndColorLoader, petStatesForPetTypeLoader } { petTypeBySpeciesAndColorLoader, petStatesForPetTypeLoader }
) => { ) => {
if (customPetData == null) {
return null;
}
const petType = await petTypeBySpeciesAndColorLoader.load({ const petType = await petTypeBySpeciesAndColorLoader.load({
speciesId: customPetData.custom_pet.species_id, speciesId: customPetData.custom_pet.species_id,
colorId: customPetData.custom_pet.color_id, colorId: customPetData.custom_pet.color_id,
@ -99,15 +91,19 @@ const resolvers = {
`sorry! Try using the Modeling Hub on Classic DTI to upload it first?` `sorry! Try using the Modeling Hub on Classic DTI to upload it first?`
); );
}, },
wornItems: ({ customPetData }) => wornItems: ({ customPetData }) => {
Object.values(customPetData.object_info_registry).map((o) => ({ if (customPetData == null) {
return [];
}
return Object.values(customPetData.object_info_registry).map((o) => ({
id: o.obj_info_id, id: o.obj_info_id,
name: o.name, name: o.name,
description: o.description, description: o.description,
thumbnailUrl: o.thumbnail_url, thumbnailUrl: o.thumbnail_url,
rarityIndex: o.rarity_index, rarityIndex: o.rarity_index,
})), }));
items: (...args) => resolvers.Pet.wornItems(...args), },
}, },
Query: { Query: {
petOnNeopetsDotCom: async ( petOnNeopetsDotCom: async (
@ -127,6 +123,7 @@ const resolvers = {
loadPetMetaData(petName), loadPetMetaData(petName),
]); ]);
if (customPetData != null) {
await saveModelingData(customPetData, petMetaData, { await saveModelingData(customPetData, petMetaData, {
db, db,
petTypeBySpeciesAndColorLoader, petTypeBySpeciesAndColorLoader,
@ -135,6 +132,7 @@ const resolvers = {
itemTranslationLoader, itemTranslationLoader,
swfAssetByRemoteIdLoader, swfAssetByRemoteIdLoader,
}); });
}
return { name: petName, customPetData, petMetaData }; return { name: petName, customPetData, petMetaData };
}, },
@ -155,10 +153,22 @@ async function loadPetMetaData(petName) {
} }
async function loadCustomPetData(petName) { async function loadCustomPetData(petName) {
try {
const response = await neopetsXmlrpcCall("CustomPetService.getViewerData", [ const response = await neopetsXmlrpcCall("CustomPetService.getViewerData", [
petName, petName,
]); ]);
return response; return response;
} catch (error) {
// If Neopets.com fails to find valid customization data, we return null.
if (
error.code === "AMFPHP_RUNTIME_ERROR" &&
error.faultString === "Unable to find body artwork for this combination."
) {
return null;
} else {
throw error;
}
}
} }
function getPoseFromPetData(petMetaData, petCustomData) { function getPoseFromPetData(petMetaData, petCustomData) {