|
1 |
| -# nextjs-with-apollo |
2 |
| -Apollo hook to work with NextJS |
| 1 | +[![NPM Status][npm-image]][npm-url] |
| 2 | +[![GitHub license][license-image]][license-url] |
| 3 | +[![LGTM Status][lgtm-image]][lgtm-url] |
| 4 | + |
| 5 | +# ⚓ nextjs-with-apollo |
| 6 | +Apollo HOC for NextJS. |
| 7 | + |
| 8 | + |
| 9 | +## Install |
| 10 | + |
| 11 | +Install the package with npm |
| 12 | + |
| 13 | +```sh |
| 14 | +npm install nextjs-with-apollo |
| 15 | +``` |
| 16 | + |
| 17 | +or with yarn |
| 18 | + |
| 19 | +```sh |
| 20 | +yarn add nextjs-with-apollo |
| 21 | +``` |
| 22 | + |
| 23 | +## Basic Usage |
| 24 | + |
| 25 | +1. Create a HOC |
| 26 | + |
| 27 | +Create the HOC using a basic setup. |
| 28 | + |
| 29 | +```js |
| 30 | +// hocs/withApollo.js |
| 31 | +import withApollo from 'nextjs-with-apollo'; |
| 32 | +import ApolloClient from 'apollo-client'; |
| 33 | +import { InMemoryCache } from 'apollo-cache-inmemory'; |
| 34 | + |
| 35 | +const GRAPHQL_URL = 'https://your-graphql-url'; |
| 36 | + |
| 37 | +const createApolloClient = ({ initialState, headers }) => |
| 38 | + new ApolloClient({ |
| 39 | + uri: GRAPHQL_URL, |
| 40 | + cache: new InMemoryCache().restore(initialState || {}) // hydrate cache |
| 41 | + }); |
| 42 | + |
| 43 | +export default withApollo(createApolloClient); |
| 44 | +``` |
| 45 | +Parameters `initialState` and `headers` are received in the hoc. |
| 46 | + |
| 47 | +If the render is happening in server, all headers received by the server can be accessed via `headers`. |
| 48 | +If the render is happening in browser, we hydrate the client cache with the initial stated created in server. |
| 49 | + |
| 50 | +1. Now use the HOC |
| 51 | + |
| 52 | +```js |
| 53 | +import React from 'react'; |
| 54 | +import { useQuery } from '@apollo/react-hooks'; |
| 55 | + |
| 56 | +import withApollo from 'hocs/withApollo'; |
| 57 | + |
| 58 | +const QUERY = gql` |
| 59 | + query Profile { |
| 60 | + profile { |
| 61 | + name |
| 62 | + displayname |
| 63 | + } |
| 64 | + } |
| 65 | +`; |
| 66 | + |
| 67 | +const ProfilePage = () => { |
| 68 | + const { loading, error, data } = useQuery(PROFILE_QUERY); |
| 69 | + |
| 70 | + if (loading) { |
| 71 | + return <p>loading..</p>; |
| 72 | + } |
| 73 | + |
| 74 | + if (error) { |
| 75 | + return JSON.stringify(error); |
| 76 | + } |
| 77 | + |
| 78 | + return ( |
| 79 | + <> |
| 80 | + <p>user name: {data.profile.displayname}</p> |
| 81 | + <p>name: {data.profile.name}</p> |
| 82 | + </> |
| 83 | + ); |
| 84 | +}; |
| 85 | + |
| 86 | +export default withApollo(ProfilePage); |
| 87 | + |
| 88 | +``` |
| 89 | + |
| 90 | +Thats all. Now Profile page will be rendered in the server. You do not need to do anything in `getInitialProps`. All queries are resolved in the sever. |
| 91 | + |
| 92 | +If you dont want to SSR the above page then you can pass `{ssr: false}` to the hoc. |
| 93 | + |
| 94 | +``` |
| 95 | +export default withApollo(ProfilePage, { ssr: false }); |
| 96 | +``` |
| 97 | + |
| 98 | +If you want, you can also access instance of `apolloClient` in `getInitialProps`. |
| 99 | + |
| 100 | +```js |
| 101 | +ProfilePage.getInitialProps = ctx => { |
| 102 | + const apolloClient = ctx.apolloClient; |
| 103 | +}; |
| 104 | +``` |
| 105 | + |
| 106 | +## SSR with auth |
| 107 | + |
| 108 | +Often graphQL server requires `AuthorizationToken` for authorizing requests. We can use the headers received in server to parse token from client side cookies. |
| 109 | + |
| 110 | +```js |
| 111 | +// hocs/withApollo.js |
| 112 | +import withApollo from 'nextjs-with-apollo'; |
| 113 | +import fetch from 'isomorphic-unfetch'; |
| 114 | +import { InMemoryCache } from 'apollo-cache-inmemory'; |
| 115 | +import ApolloClient from 'apollo-client'; |
| 116 | +import { HttpLink } from 'apollo-link-http'; |
| 117 | +import { ApolloLink } from 'apollo-link'; |
| 118 | +import { setContext } from 'apollo-link-context'; |
| 119 | +import cookie from 'cookie'; |
| 120 | +import get from 'lodash/get'; |
| 121 | + |
| 122 | +const isServer = typeof window === 'undefined'; |
| 123 | + |
| 124 | +const getToken = headers => { |
| 125 | + const COOKIE_NAME = 'your_auth_cookie_name' |
| 126 | + const cookies = isServer ? get(headers, 'cookie', '') : document.cookie; |
| 127 | + |
| 128 | + return get(cookie.parse(cookies), COOKIE_NAME, ''); |
| 129 | +}; |
| 130 | + |
| 131 | +const attachAuth = headers => () => { |
| 132 | + const token = getToken(headers); |
| 133 | + |
| 134 | + return { |
| 135 | + headers: { |
| 136 | + authorization: `Bearer ${token}` |
| 137 | + } |
| 138 | + }; |
| 139 | +}; |
| 140 | + |
| 141 | +const createApolloClient = ({ initialState, headers = {} }) => { |
| 142 | + const authLink = () => setContext(attachAuth(headers)); |
| 143 | + |
| 144 | + const httpLink = new HttpLink({ |
| 145 | + credentials: 'include', |
| 146 | + uri: GRAPHQL_ENDPOINT, |
| 147 | + fetch |
| 148 | + }); |
| 149 | + |
| 150 | + return new ApolloClient({ |
| 151 | + ssrMode: isServer, |
| 152 | + link: ApolloLink.from([authLink(), httpLink]), |
| 153 | + cache: new InMemoryCache().restore(initialState || {}) |
| 154 | + }); |
| 155 | +}; |
| 156 | + |
| 157 | +export default withApollo(createApolloClient); |
| 158 | +``` |
| 159 | + |
| 160 | +## License |
| 161 | +Feel free to use the code, it's released using the MIT license. |
| 162 | + |
| 163 | +[npm-image]:https://img.shields.io/npm/v/nextjs-with-apollo.svg |
| 164 | +[npm-url]:https://www.npmjs.com/package/nextjs-with-apollo |
| 165 | +[license-image]:https://img.shields.io/github/license/adikari/nextjs-with-apollo.svg |
| 166 | +[license-url]:https://github.com/adikari/nextjs-with-apollo/blob/master/LICENSE |
| 167 | + |
| 168 | +[lgtm-image]:https://img.shields.io/lgtm/grade/javascript/g/adikari/nextjs-with-apollo.svg?logo=lgtm&logoWidth=18 |
| 169 | +[lgtm-url]:https://lgtm.com/projects/g/adikari/nextjs-with-apollo/context:javascript |
0 commit comments