Skip to content

Commit

Permalink
chore(example/vite): add usage of useFragment
Browse files Browse the repository at this point in the history
  • Loading branch information
uhyo committed Dec 16, 2023
1 parent 8e0aca1 commit 08797f0
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 4 deletions.
4 changes: 4 additions & 0 deletions examples/vite/graphql.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ const nitrogql: NitrogqlExtension = {
generate: {
mode: "with-loader-ts-5.0",
schemaOutput: "./src/generated/schema.d.ts",
name: {
fragmentTypeSuffix: "Fragment",
fragmentVariableSuffix: "Fragment",
},
},
};

Expand Down
17 changes: 14 additions & 3 deletions examples/vite/src/PokemonCell/index.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,19 @@
import type { PokemonCell as PokemonCellFragment } from "./PokemonCell.graphql";
import { useFragment } from "@apollo/client";
import { PokemonCellFragment } from "./PokemonCell.graphql";

export const PokemonCell: React.FC<{
pokemon: PokemonCellFragment;
}> = ({ pokemon }) => {
id: number;
}> = ({ id }) => {
const { complete, data: pokemon } = useFragment({
from: {
__typename: "pokemon_v2_pokemonspecies",
id,
},
fragment: PokemonCellFragment,
});
if (!complete) {
throw new Error("Data is not complete");
}
const ja = pokemon.names.find((name) => name.language_id === 1);
const en = pokemon.names.find((name) => name.language_id === 9);
return (
Expand Down
2 changes: 1 addition & 1 deletion examples/vite/src/PokemonList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export const PokemonList: React.FC = () => {
</div>
<ul className="pokemonList">
{data.species.map((pokemon) => (
<PokemonCell key={pokemon.id} pokemon={pokemon} />
<PokemonCell key={pokemon.id} id={pokemon.id} />
))}
</ul>
</div>
Expand Down

0 comments on commit 08797f0

Please sign in to comment.