Unverified Commit 00f56aa2 authored by PapatMayuri's avatar PapatMayuri Committed by GitHub

Update with urql example (#76485)

This PR updates the with-urql example for using the App Router.
Here are the changes that have been made:

- I renamed the pages folder and moved it to the app folder.
- Added the layout.tsx file as part of the App Router.
- Updated the package.json file.

CC: @samcx

---------
Co-authored-by: 's avatarMayuriPapat25 <mayuri.papat@qed42.com>
Co-authored-by: 's avatarsamcx <sam@vercel.com>
parent 052c9798
...@@ -28,6 +28,7 @@ ...@@ -28,6 +28,7 @@
npm-debug.log* npm-debug.log*
yarn-debug.log* yarn-debug.log*
yarn-error.log* yarn-error.log*
.pnpm-debug.log*
# local env files # local env files
.env*.local .env*.local
......
import type { Metadata } from "next";
export const metadata: Metadata = {
title: "With urql",
description: "Next.js example with urql",
};
export default function RootLayout({
children,
}: Readonly<{
children: React.ReactNode;
}>) {
return (
<html lang="en">
<body>{children}</body>
</html>
);
}
import Link from "next/link"; import Link from "next/link";
import { getPokemons } from "../graphql/getPokemons"; import { getPokemons } from "@/graphql/getPokemons";
type Pokemon = {
image: string;
name: string;
__typename: string;
};
export default async function Home() {
const pokemons = await getPokemons();
export default function Home({ pokemons }) {
return ( return (
<ul> <ul>
{pokemons.map((pokemon) => ( {pokemons.map((pokemon: Pokemon) => (
<li key={pokemon.name}> <li key={pokemon.name}>
<Link as={`/pokemon/${pokemon.name}`} href="/pokemon/[name]"> <Link href={`/pokemon/${pokemon.name}`}>
<h2 style={{ textTransform: "capitalize" }}>{pokemon.name}</h2> <h2 style={{ textTransform: "capitalize" }}>{pokemon.name}</h2>
<img src={pokemon.image} alt={`${pokemon.name} picture`} /> <img src={pokemon.image} alt={`${pokemon.name} picture`} />
</Link> </Link>
...@@ -15,12 +23,3 @@ export default function Home({ pokemons }) { ...@@ -15,12 +23,3 @@ export default function Home({ pokemons }) {
</ul> </ul>
); );
} }
export const getStaticProps = async () => {
const pokemons = await getPokemons();
return {
props: {
pokemons,
},
};
};
import { getPokemon as getPokemonByName } from "@/graphql/getPokemon";
export default async function PokemonPage({
params,
}: {
params: { name?: string };
}) {
const pokemon = await getPokemonByName(params.name);
return (
<div>
<h1 style={{ textTransform: "capitalize" }}>{pokemon.name}</h1>
<img src={pokemon.image} alt={pokemon.name} />
</div>
);
}
import { createClient } from "urql"; import { cacheExchange, createClient, fetchExchange } from "urql";
export const client = createClient({ export const client = createClient({
url: "https://graphql-pokemon2.vercel.app/", url: "https://graphql-pokemon2.vercel.app/",
exchanges: [cacheExchange, fetchExchange],
}); });
import { client } from "./client"; import { client } from "./client";
const pokemonQuery = ` const pokemonQuery = `
query firstTwentyPokemons($name: String!) { query firstTwentyPokemons($name: String!) {
pokemon(name: $name) { pokemon(name: $name) {
......
import { client } from "./client"; import { client } from "./client";
const firstTwentyPokemonsQuery = ` const firstTwentyPokemonsQuery = `
query firstTwentyPokemons { query firstTwentyPokemons {
pokemons(first: 20) { pokemons(first: 20) {
......
{ {
"private": true, "private": true,
"scripts": { "scripts": {
"dev": "next", "dev": "next dev",
"build": "next build", "build": "next build",
"start": "next start" "start": "next start"
}, },
"dependencies": { "dependencies": {
"graphql": "^15.0.0",
"isomorphic-unfetch": "^3.0.0",
"next": "latest", "next": "latest",
"react": "^18.2.0", "react": "^19.0.0",
"react-dom": "^18.2.0", "react-dom": "^19.0.0",
"urql": "^1.9.7" "urql": "^4.2.1"
},
"devDependencies": {
"@types/node": "^22.13.5",
"@types/react": "^19.0.10",
"typescript": "^5.7.3"
} }
} }
import { getPokemon } from "../../graphql/getPokemon";
import { getPokemons } from "../../graphql/getPokemons";
export default function Pokemon({ pokemon }) {
return (
<div>
<h1>{pokemon.name}</h1>
<img src={pokemon.image} />
</div>
);
}
export const getStaticPaths = async () => {
const pokemons = await getPokemons();
return {
paths: pokemons.map(({ name }) => ({
params: { name },
})),
fallback: true,
};
};
export const getStaticProps = async (context) => {
const { name } = context.params;
const pokemon = await getPokemon(name);
return {
props: {
pokemon,
},
};
};
{
"compilerOptions": {
"target": "ES2017",
"lib": ["dom", "dom.iterable", "esnext"],
"allowJs": true,
"skipLibCheck": true,
"strict": true,
"noEmit": true,
"esModuleInterop": true,
"module": "esnext",
"moduleResolution": "bundler",
"resolveJsonModule": true,
"isolatedModules": true,
"jsx": "preserve",
"incremental": true,
"plugins": [
{
"name": "next"
}
],
"paths": {
"@/*": ["./*"]
}
},
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", ".next/types/**/*.ts"],
"exclude": ["node_modules"]
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment