From 5d8799438c561d09f8072440fc1fb557ea25c774 Mon Sep 17 00:00:00 2001 From: Luis Alvarez Date: Thu, 26 Mar 2020 17:51:28 -0500 Subject: [PATCH 1/2] Updated example --- examples/api-routes-graphql/package.json | 9 ++--- examples/api-routes-graphql/pages/index.js | 38 ++++++++++++---------- 2 files changed, 25 insertions(+), 22 deletions(-) diff --git a/examples/api-routes-graphql/package.json b/examples/api-routes-graphql/package.json index f64edb4245404..5f391da1c3f03 100644 --- a/examples/api-routes-graphql/package.json +++ b/examples/api-routes-graphql/package.json @@ -7,12 +7,13 @@ "start": "next start" }, "dependencies": { - "apollo-server-micro": "2.6.7", - "graphql": "14.4.2", + "apollo-server-micro": "2.11.0", + "graphql": "14.6.0", "isomorphic-unfetch": "3.0.0", "next": "latest", - "react": "^16.8.6", - "react-dom": "^16.8.6" + "react": "16.13.1", + "react-dom": "16.13.1", + "swr": "0.1.18" }, "license": "ISC" } diff --git a/examples/api-routes-graphql/pages/index.js b/examples/api-routes-graphql/pages/index.js index 98edfabc2770c..e62fb7bc24f36 100644 --- a/examples/api-routes-graphql/pages/index.js +++ b/examples/api-routes-graphql/pages/index.js @@ -1,27 +1,29 @@ -import fetch from 'isomorphic-unfetch' +import useSWR from 'swr' -const Index = ({ users }) => ( -
- {users.map((user, i) => ( -
{user.name}
- ))} -
-) - -Index.getInitialProps = async () => { - const response = await fetch('http://localhost:3000/api/graphql', { +const fetcher = query => + fetch('/api/graphql', { method: 'POST', headers: { 'Content-type': 'application/json', }, - body: JSON.stringify({ query: '{ users { name } }' }), + body: JSON.stringify({ query }), }) + .then(res => res.json()) + .then(json => json.data) - const { - data: { users }, - } = await response.json() +export default function Index() { + const { data, error } = useSWR('{ users { name } }', fetcher) - return { users } -} + if (error) return
Failed to load
+ if (!data) return
Loading...
-export default Index + const { users } = data + + return ( +
+ {users.map((user, i) => ( +
{user.name}
+ ))} +
+ ) +} From 2a1e1b1061e70efcbf0b5b5148bcc41ee9f593a8 Mon Sep 17 00:00:00 2001 From: Luis Alvarez Date: Thu, 26 Mar 2020 17:53:01 -0500 Subject: [PATCH 2/2] Removed isomorphic-unfetch --- examples/api-routes-graphql/package.json | 1 - 1 file changed, 1 deletion(-) diff --git a/examples/api-routes-graphql/package.json b/examples/api-routes-graphql/package.json index 5f391da1c3f03..3002140e0991c 100644 --- a/examples/api-routes-graphql/package.json +++ b/examples/api-routes-graphql/package.json @@ -9,7 +9,6 @@ "dependencies": { "apollo-server-micro": "2.11.0", "graphql": "14.6.0", - "isomorphic-unfetch": "3.0.0", "next": "latest", "react": "16.13.1", "react-dom": "16.13.1",