Skip to content

Commit 71d899f

Browse files
alejandronaneztimneutkensTimer
authored
Add urql to the examples list (vercel#13006)
* Install UrQL dependencies - Created the examples folder `with-urql` - Installed the dependencies - Got a simple index.js page setup * Basic UrQL example - Connects to Pokemon GraphQL API * Loading Pokemon list + Pokemon page - Using `isomorphic-unfetch` because `urql` uses `window.fetch` to get data because we need to query the GraphQL API using Node (in `getStaticProps`) - Deleted `_app.js` since we're not using UrQL to query data on the clientside - Put all GraphQL related code in `/graphql` * Update README for UrQL example * Update urql casing * Update examples/with-urql/graphql/client.js Co-authored-by: Joe Haddad <timer150@gmail.com> * Update examples/with-urql/pages/index.js Co-authored-by: Joe Haddad <timer150@gmail.com> * Update examples/with-urql/pages/pokemon/[name].js Co-authored-by: Joe Haddad <timer150@gmail.com> * Fix linting Co-authored-by: Tim Neutkens <tim@timneutkens.nl> Co-authored-by: Joe Haddad <timer150@gmail.com> Co-authored-by: Joe Haddad <joe.haddad@zeit.co>
1 parent 589f44e commit 71d899f

File tree

7 files changed

+162
-0
lines changed

7 files changed

+162
-0
lines changed

examples/with-urql/README.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Next.js and urql
2+
3+
Use [urql](https://github.com/FormidableLabs/urql) with Next.js using SSG.
4+
5+
## Deploy your own
6+
7+
Deploy the example using [Vercel](https://vercel.com/now):
8+
9+
[![Deploy with Vercel](https://vercel.com/button)](https://vercel.com/import/project?template=https://github.com/zeit/next.js/tree/canary/examples/with-urql)
10+
11+
## How to use
12+
13+
### Using `create-next-app`
14+
15+
Execute [`create-next-app`](https://github.com/zeit/next.js/tree/canary/packages/create-next-app) with [npm](https://docs.npmjs.com/cli/init) or [Yarn](https://yarnpkg.com/lang/en/docs/cli/create/) to bootstrap the example:
16+
17+
```bash
18+
npm init next-app --example with-urql with-urql-app
19+
# or
20+
yarn create next-app --example with-urql with-urql-app
21+
```
22+
23+
### Download manually
24+
25+
Download the example:
26+
27+
```bash
28+
curl https://codeload.github.com/zeit/next.js/tar.gz/canary | tar -xz --strip=2 next.js-canary/examples/with-urql
29+
cd with-urql
30+
```
31+
32+
Install it and run:
33+
34+
```bash
35+
npm install
36+
npm run dev
37+
# or
38+
yarn
39+
yarn dev
40+
```
41+
42+
Deploy it to the cloud with [Vercel](https://vercel.com/import?filter=next.js&utm_source=github&utm_medium=readme&utm_campaign=next-example) ([Documentation](https://nextjs.org/docs/deployment)).

examples/with-urql/graphql/client.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { createClient } from 'urql'
2+
3+
export const client = createClient({
4+
url: 'https://graphql-pokemon.now.sh/',
5+
})
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { client } from './client'
2+
const pokemonQuery = `
3+
query firstTwentyPokemons($name: String!) {
4+
pokemon(name: $name) {
5+
name
6+
image
7+
}
8+
}
9+
`
10+
11+
export const getPokemon = async (name) => {
12+
const {
13+
data: { pokemon },
14+
} = await client.query(pokemonQuery, { name }).toPromise()
15+
16+
return pokemon
17+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { client } from './client'
2+
const firstTwentyPokemonsQuery = `
3+
query firstTwentyPokemons {
4+
pokemons(first: 20) {
5+
image
6+
name
7+
}
8+
}
9+
`
10+
11+
export const getPokemons = async () => {
12+
const {
13+
data: { pokemons },
14+
} = await client.query(firstTwentyPokemonsQuery).toPromise()
15+
16+
return pokemons.map((pokemon) => ({
17+
...pokemon,
18+
name: pokemon.name.toLowerCase(),
19+
}))
20+
}

examples/with-urql/package.json

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"name": "with-urql",
3+
"scripts": {
4+
"dev": "next",
5+
"build": "next build",
6+
"start": "next start"
7+
},
8+
"dependencies": {
9+
"graphql": "^15.0.0",
10+
"isomorphic-unfetch": "^3.0.0",
11+
"next": "latest",
12+
"react": "^16.13.1",
13+
"react-dom": "^16.13.1",
14+
"urql": "^1.9.7"
15+
},
16+
"license": "MIT"
17+
}

examples/with-urql/pages/index.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import Link from 'next/link'
2+
import { getPokemons } from '../graphql/getPokemons'
3+
4+
export default function Home({ pokemons }) {
5+
return (
6+
<ul>
7+
{pokemons.map((pokemon) => (
8+
<li key={pokemon.name}>
9+
<Link as={`/pokemon/${pokemon.name}`} href="/pokemon/[name]">
10+
<a>
11+
<h2 style={{ textTransform: 'capitalize' }}>{pokemon.name}</h2>
12+
<img src={pokemon.image} alt={`${pokemon.name} picture`} />
13+
</a>
14+
</Link>
15+
</li>
16+
))}
17+
</ul>
18+
)
19+
}
20+
21+
export const getStaticProps = async () => {
22+
const pokemons = await getPokemons()
23+
return {
24+
props: {
25+
pokemons,
26+
},
27+
}
28+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { getPokemon } from '../../graphql/getPokemon'
2+
import { getPokemons } from '../../graphql/getPokemons'
3+
4+
export default function Pokemon({ pokemon }) {
5+
return (
6+
<div>
7+
<h1>{pokemon.name}</h1>
8+
<img src={pokemon.image} />
9+
</div>
10+
)
11+
}
12+
13+
export const getStaticPaths = async () => {
14+
const pokemons = await getPokemons()
15+
16+
return {
17+
paths: pokemons.map(({ name }) => ({
18+
params: { name },
19+
})),
20+
fallback: true,
21+
}
22+
}
23+
24+
export const getStaticProps = async (context) => {
25+
const { name } = context.params
26+
const pokemon = await getPokemon(name)
27+
28+
return {
29+
props: {
30+
pokemon,
31+
},
32+
}
33+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy