Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
N
next.js
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Alex
next.js
Commits
1eefd006
Unverified
Commit
1eefd006
authored
Apr 07, 2020
by
Luis Alvarez D
Committed by
GitHub
Apr 07, 2020
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
[example] Add SWR to api-routes-graphql (#11383)
* Updated example * Removed isomorphic-unfetch
parent
30870b44
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
25 additions
and
23 deletions
+25
-23
package.json
examples/api-routes-graphql/package.json
+5
-5
index.js
examples/api-routes-graphql/pages/index.js
+20
-18
No files found.
examples/api-routes-graphql/package.json
View file @
1eefd006
...
@@ -7,12 +7,12 @@
...
@@ -7,12 +7,12 @@
"start"
:
"next start"
"start"
:
"next start"
},
},
"dependencies"
:
{
"dependencies"
:
{
"apollo-server-micro"
:
"2.6.7"
,
"apollo-server-micro"
:
"2.11.0"
,
"graphql"
:
"14.4.2"
,
"graphql"
:
"14.6.0"
,
"isomorphic-unfetch"
:
"3.0.0"
,
"next"
:
"latest"
,
"next"
:
"latest"
,
"react"
:
"^16.8.6"
,
"react"
:
"16.13.1"
,
"react-dom"
:
"^16.8.6"
"react-dom"
:
"16.13.1"
,
"swr"
:
"0.1.18"
},
},
"license"
:
"ISC"
"license"
:
"ISC"
}
}
examples/api-routes-graphql/pages/index.js
View file @
1eefd006
import
fetch
from
'isomorphic-unfetch
'
import
useSWR
from
'swr
'
const
Index
=
({
users
})
=>
(
const
fetcher
=
query
=>
<
div
>
fetch
(
'/api/graphql'
,
{
{
users
.
map
((
user
,
i
)
=>
(
<
div
key
=
{
i
}
>
{
user
.
name
}
<
/div
>
))}
<
/div
>
)
Index
.
getInitialProps
=
async
()
=>
{
const
response
=
await
fetch
(
'http://localhost:3000/api/graphql'
,
{
method
:
'POST'
,
method
:
'POST'
,
headers
:
{
headers
:
{
'Content-type'
:
'application/json'
,
'Content-type'
:
'application/json'
,
},
},
body
:
JSON
.
stringify
({
query
:
'{ users { name } }'
}),
body
:
JSON
.
stringify
({
query
}),
})
})
.
then
(
res
=>
res
.
json
())
.
then
(
json
=>
json
.
data
)
const
{
export
default
function
Index
()
{
data
:
{
users
},
const
{
data
,
error
}
=
useSWR
(
'{ users { name } }'
,
fetcher
)
}
=
await
response
.
json
()
return
{
users
}
if
(
error
)
return
<
div
>
Failed
to
load
<
/div
>
}
if
(
!
data
)
return
<
div
>
Loading
...
<
/div
>
export
default
Index
const
{
users
}
=
data
return
(
<
div
>
{
users
.
map
((
user
,
i
)
=>
(
<
div
key
=
{
i
}
>
{
user
.
name
}
<
/div
>
))}
<
/div
>
)
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment