Thanks to visit codestin.com
Credit goes to github.com

Skip to content
This repository was archived by the owner on Mar 9, 2021. It is now read-only.

Commit e162501

Browse files
committed
Test GraphQL with Next
- Updated next to v9 - Added GraphQL and Apollo related dependencies - Added _app.js in pages which will add ApolloClient HOC - Added other GraphQL related config and query files
1 parent e233ff4 commit e162501

File tree

7 files changed

+3433
-19177
lines changed

7 files changed

+3433
-19177
lines changed

graphql/users.query.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import gql from 'graphql-tag';
2+
3+
export const GET_USERS = gql`
4+
query {
5+
allUsers {
6+
id
7+
email
8+
firstName
9+
lastName
10+
}
11+
}
12+
`;

package-lock.json

Lines changed: 0 additions & 15401 deletions
This file was deleted.

package.json

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,16 +66,23 @@
6666
]
6767
},
6868
"dependencies": {
69+
"@apollo/react-hooks": "^3.1.3",
70+
"apollo-cache-inmemory": "^1.6.3",
71+
"apollo-client": "^2.6.4",
72+
"apollo-link-http": "^1.5.16",
6973
"babel-plugin-emotion": "9.2.11",
7074
"date-fns": "1.30.1",
7175
"emotion": "9.2.12",
7276
"emotion-server": "9.2.12",
7377
"get-port": "5.0.0",
78+
"graphql": "^14.5.8",
79+
"graphql-tag": "^2.10.1",
7480
"grid-styled": "5.0.2",
75-
"isomorphic-unfetch": "3.0.0",
81+
"isomorphic-unfetch": "^3.0.0",
7682
"lodash.take": "4.1.1",
7783
"marked": "0.7.0",
78-
"next": "8.1.0",
84+
"next": "^9.0.5",
85+
"next-with-apollo": "^4.3.0",
7986
"nprogress": "0.2.0",
8087
"path-match": "1.2.4",
8188
"prop-types": "15.7.2",

pages/_app.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import React from 'react';
2+
import App from 'next/app';
3+
import { ApolloProvider } from '@apollo/react-hooks';
4+
5+
import withData from '../utils/apollo-client';
6+
7+
class MyApp extends App {
8+
render() {
9+
const { Component, pageProps, apollo } = this.props;
10+
return (
11+
<ApolloProvider client={apollo}>
12+
<Component {...pageProps} />
13+
</ApolloProvider>
14+
);
15+
}
16+
}
17+
18+
// Wraps all components in the tree with the data provider
19+
export default withData(MyApp);

pages/index.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ import { space } from 'styled-system';
44
import { Flex, Box } from 'grid-styled/emotion';
55
import take from 'lodash.take';
66
import Link from 'next/link';
7+
import { useQuery } from '@apollo/react-hooks';
78

9+
import { GET_USERS } from '../graphql/users.query';
810
import Hide, { Container, Button, Title, SubTitle, breakpoints } from '../utils/base.styles';
911
import { listOfSubjects } from '../utils/mock-data';
1012
import Layout from '../components/common/layout';
@@ -203,6 +205,26 @@ class UpcomingEvent extends React.Component {
203205
}
204206
}
205207

208+
const TestGraphQL = () => {
209+
const { data, loading, error } = useQuery(GET_USERS);
210+
if (error) return <div>There was an error fetching data!</div>;
211+
if (loading) return <div>We are fetching data, please wait for a while.</div>;
212+
return (
213+
<div>
214+
Users:
215+
<ul>
216+
{data.allUsers.map(({ email, firstName, lastName }, key) => (
217+
<li key={key}>
218+
<p>
219+
Email: {email}, First Name: {firstName}, Last Name: {lastName}
220+
</p>
221+
</li>
222+
))}
223+
</ul>
224+
</div>
225+
);
226+
};
227+
206228
export default () => (
207229
<Layout>
208230
<HeroSection pb={[3, 5]} px={[3, 2]}>
@@ -217,6 +239,7 @@ export default () => (
217239
</HeroSection>
218240
<LearnSection pb={[4, 5]} pt={[3, 4]} px={[3, 2]}>
219241
<Container>
242+
<TestGraphQL />
220243
<Flex flexDirection="column" alignItems="center" justifyContent="center">
221244
<Box width={1}>
222245
<Title inverted>Open Source Learning Guides</Title>

utils/apollo-client.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { ApolloClient } from 'apollo-client';
2+
import { InMemoryCache } from 'apollo-cache-inmemory';
3+
import withApollo from 'next-with-apollo';
4+
import { createHttpLink } from 'apollo-link-http';
5+
import fetch from 'isomorphic-unfetch';
6+
7+
// Update the GraphQL endpoint to any instance of GraphQL that you like
8+
const GRAPHQL_URL = 'http://localhost:8000/graphql';
9+
10+
const link = createHttpLink({
11+
fetch, // Switches between unfetch & node-fetch for client & server.
12+
uri: GRAPHQL_URL,
13+
});
14+
15+
// Export a HOC from next-with-apollo
16+
// Docs: https://www.npmjs.com/package/next-with-apollo
17+
export default withApollo(
18+
// You can get headers and ctx (context) from the callback params
19+
// e.g. ({ headers, ctx, initialState })
20+
({ initialState }) =>
21+
new ApolloClient({
22+
link,
23+
cache: new InMemoryCache()
24+
// Rehydrate the cache using the initial data passed from the server:
25+
.restore(initialState || {}),
26+
})
27+
);

0 commit comments

Comments
 (0)