Skip to content

Commit

Permalink
feat: add nested data for otherFormes. If you previously requested …
Browse files Browse the repository at this point in the history
…`otherFormes` you will need to update your query to `otherFormes { species }` to get the nested data.
  • Loading branch information
favna committed Dec 14, 2023
1 parent e3d363b commit 3df710c
Show file tree
Hide file tree
Showing 12 changed files with 42,817 additions and 50 deletions.
2 changes: 1 addition & 1 deletion graphql/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ type Pokemon {
learnsets: GenerationalPokemonLearnset

"Any other forms for a Pokémon"
otherFormes: [String!]
otherFormes: [Pokemon!]

"The preevolutions for a Pokémon, if any"
preevolutions: [Pokemon!]
Expand Down
10 changes: 9 additions & 1 deletion scripts/manual-tests/get-all-data.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,6 @@
maximumHatchTime
minimumHatchTime
num
otherFormes
serebiiPage
shinyBackSprite
shinySprite
Expand Down Expand Up @@ -252,6 +251,15 @@
...FullDataFragment
}
}
otherFormes {
...FullDataFragment
evolutions {
...FullDataFragment
}
preevolutions {
...FullDataFragment
}
}
preevolutions {
...FullDataFragment
evolutions {
Expand Down
45 changes: 34 additions & 11 deletions src/lib/mappers/pokemonMapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,7 @@ export function mapPokemonDataToPokemonGraphQL({
};

const evolutionChain: Pokemon[] = [];
const otherFormsChain: Pokemon[] = [];
const preevolutionChain: Pokemon[] = [];

const pokemonData: Pokemon = {} as Pokemon;
Expand Down Expand Up @@ -481,13 +482,6 @@ export function mapPokemonDataToPokemonGraphQL({
requestedFields,
fieldAccessor: `${resolvedRecursingAs}baseSpecies`
});
addPropertyToObjectFieldBased({
objectTarget: pokemonData,
propertyKey: 'otherFormes',
propertyValue: data.otherFormes,
requestedFields,
fieldAccessor: `${resolvedRecursingAs}otherFormes`
});
addPropertyToObjectFieldBased({
objectTarget: pokemonData,
propertyKey: 'cosmeticFormes',
Expand Down Expand Up @@ -685,7 +679,7 @@ export function mapPokemonDataToPokemonGraphQL({
data: pokedex.get(toLowerSingleWordCase(prevoPokemon.species))!,
requestedFields,
...basePokemonArgs,
parsingPokemon: parseDataForEvolutionRecursion(data, prevoPokemon),
parsingPokemon: parseDataForEvolutionRecursion(data),
recursingAs: 'preevolutions'
})
);
Expand All @@ -709,7 +703,7 @@ export function mapPokemonDataToPokemonGraphQL({
data: pokedex.get(toLowerSingleWordCase(evoPokemon.species))!,
requestedFields,
...basePokemonArgs,
parsingPokemon: parseDataForEvolutionRecursion(data, evoPokemon),
parsingPokemon: parseDataForEvolutionRecursion(data),
recursingAs: 'evolutions'
})
);
Expand All @@ -725,6 +719,35 @@ export function mapPokemonDataToPokemonGraphQL({
});
}

if (
(requestedFields as GraphQLSet<string>).has(`${resolvedRecursingAs}otherFormes`) &&
data.otherFormes &&
data.otherFormes[0] !== parsingPokemon
) {
for (const otherForm of data.otherFormes) {
const otherFormPokemon = pokedex.get(toLowerSingleWordCase(otherForm));
if (otherFormPokemon) {
otherFormsChain.push(
mapPokemonDataToPokemonGraphQL({
data: pokedex.get(toLowerSingleWordCase(otherFormPokemon.species))!,
requestedFields,
...basePokemonArgs,
parsingPokemon: parseDataForEvolutionRecursion(data),
recursingAs: 'otherFormes'
})
);
}
}

addPropertyToObjectFieldBased({
objectTarget: pokemonData,
propertyKey: 'otherFormes',
propertyValue: otherFormsChain,
requestedFields,
fieldAccessor: `${resolvedRecursingAs}otherFormes`
});
}

return pokemonData;
}

Expand Down Expand Up @@ -814,7 +837,7 @@ function parseSpeciesForSerebiiPage(pokemonName: string, pokemonNumber: number,
return `${serebiiBaseUrl}-sv/${pokemonName.replace(/ /g, '').toLowerCase()}`;
}

function parseDataForEvolutionRecursion(basePokemonData: PokemonTypes.DexEntry, _: PokemonTypes.DexEntry) {
function parseDataForEvolutionRecursion(basePokemonData: PokemonTypes.DexEntry) {
if (basePokemonData.forme) {
return toLowerSingleWordCase(basePokemonData.species);
}
Expand Down Expand Up @@ -870,5 +893,5 @@ interface MapPokemonDataToPokemonGraphQLParameters {
reverseFlavorTexts: boolean;
referencedCall?: PokemonReferencedCallIdentifier;
parsingPokemon?: string;
recursingAs?: 'preevolutions' | 'evolutions' | false;
recursingAs?: 'preevolutions' | 'evolutions' | 'otherFormes' | false;
}
2 changes: 1 addition & 1 deletion src/lib/types/graphql-mapped-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ interface Pokemon {
/** The dex number for a Pokémon */
num: number;
/** Any other forms for a Pokémon */
otherFormes?: Maybe<Array<string>>;
otherFormes?: Maybe<Array<Pokemon>>;
/** The preevolutions for a Pokémon, if any */
preevolutions?: Maybe<Array<Pokemon>>;
/** Serebii page for a Pokémon */
Expand Down
21 changes: 10 additions & 11 deletions tests/testUtils/full-data-responses/beldum.json
Original file line number Diff line number Diff line change
Expand Up @@ -754,7 +754,10 @@
"evYields": { "hp": 0, "attack": 0, "defense": 1, "specialattack": 0, "specialdefense": 0, "speed": 0 },
"evolutionLevel": null,
"flavorTexts": [
{ "flavor": "They converse with one another by using magnetic pulses. In a swarm, they move in perfect unison.", "game": "Shining Pearl" }
{
"flavor": "All its cells are magnets. It communicates with others of its kind by transmitting magnetic forces from its body.",
"game": "Violet"
}
],
"forme": null,
"formeLetter": null,
Expand All @@ -766,7 +769,6 @@
"maximumHatchTime": 10536,
"minimumHatchTime": 10280,
"num": 374,
"otherFormes": null,
"serebiiPage": "https://www.serebii.net/pokedex-swsh/beldum",
"shinyBackSprite": "https://play.pokemonshowdown.com/sprites/ani-back-shiny/beldum.gif",
"shinySprite": "https://play.pokemonshowdown.com/sprites/ani-shiny/beldum.gif",
Expand Down Expand Up @@ -1597,12 +1599,7 @@
"eggGroups": ["Mineral"],
"evYields": { "hp": 0, "attack": 0, "defense": 2, "specialattack": 0, "specialdefense": 0, "speed": 0 },
"evolutionLevel": "20",
"flavorTexts": [
{
"flavor": "It is formed by two Beldum joining together. Its steel body won't be scratched if it collides with a jet.",
"game": "Shining Pearl"
}
],
"flavorTexts": [{ "flavor": "It adores magnetic minerals, so it pursues Nosepass at speeds exceeding 60 mph.", "game": "Violet" }],
"forme": null,
"formeLetter": null,
"gender": { "female": "0%", "male": "0%" },
Expand All @@ -1613,7 +1610,6 @@
"maximumHatchTime": 10536,
"minimumHatchTime": 10280,
"num": 375,
"otherFormes": null,
"serebiiPage": "https://www.serebii.net/pokedex-swsh/metang",
"shinyBackSprite": "https://play.pokemonshowdown.com/sprites/ani-back-shiny/metang.gif",
"shinySprite": "https://play.pokemonshowdown.com/sprites/ani-shiny/metang.gif",
Expand Down Expand Up @@ -10350,7 +10346,10 @@
"evYields": { "hp": 0, "attack": 0, "defense": 3, "specialattack": 0, "specialdefense": 0, "speed": 0 },
"evolutionLevel": "45",
"flavorTexts": [
{ "flavor": "Metang combined to form it. With four brains, it has the intelligence of a supercomputer.", "game": "Shining Pearl" }
{
"flavor": "It firmly pins its prey using its four claws and large body. Then Metagross uses the mouth on its stomach to chew its prey to bits.",
"game": "Violet"
}
],
"forme": null,
"formeLetter": null,
Expand All @@ -10362,7 +10361,6 @@
"maximumHatchTime": 10536,
"minimumHatchTime": 10280,
"num": 376,
"otherFormes": ["metagrossmega"],
"serebiiPage": "https://www.serebii.net/pokedex-swsh/metagross",
"shinyBackSprite": "https://play.pokemonshowdown.com/sprites/ani-back-shiny/metagross.gif",
"shinySprite": "https://play.pokemonshowdown.com/sprites/ani-shiny/metagross.gif",
Expand Down Expand Up @@ -10458,6 +10456,7 @@
"preevolutions": null
}
],
"otherFormes": null,
"preevolutions": null
}
}
Expand Down
4 changes: 1 addition & 3 deletions tests/testUtils/full-data-responses/dragonair.json
Original file line number Diff line number Diff line change
Expand Up @@ -9817,7 +9817,6 @@
"maximumHatchTime": 10536,
"minimumHatchTime": 10280,
"num": 148,
"otherFormes": null,
"serebiiPage": "https://www.serebii.net/pokedex-sv/dragonair",
"shinyBackSprite": "https://play.pokemonshowdown.com/sprites/ani-back-shiny/dragonair.gif",
"shinySprite": "https://play.pokemonshowdown.com/sprites/ani-shiny/dragonair.gif",
Expand Down Expand Up @@ -19693,7 +19692,6 @@
"maximumHatchTime": 10536,
"minimumHatchTime": 10280,
"num": 149,
"otherFormes": null,
"serebiiPage": "https://www.serebii.net/pokedex-sv/dragonite",
"shinyBackSprite": "https://play.pokemonshowdown.com/sprites/ani-back-shiny/dragonite.gif",
"shinySprite": "https://play.pokemonshowdown.com/sprites/ani-shiny/dragonite.gif",
Expand Down Expand Up @@ -19776,6 +19774,7 @@
"preevolutions": null
}
],
"otherFormes": null,
"preevolutions": [
{
"abilities": {
Expand Down Expand Up @@ -29594,7 +29593,6 @@
"maximumHatchTime": 10536,
"minimumHatchTime": 10280,
"num": 147,
"otherFormes": null,
"serebiiPage": "https://www.serebii.net/pokedex-sv/dratini",
"shinyBackSprite": "https://play.pokemonshowdown.com/sprites/ani-back-shiny/dratini.gif",
"shinySprite": "https://play.pokemonshowdown.com/sprites/ani-shiny/dratini.gif",
Expand Down
Loading

0 comments on commit 3df710c

Please sign in to comment.