-
Notifications
You must be signed in to change notification settings - Fork 26.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add an example with graphql-react #5984
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# Next.js example with [`graphql-react`](https://github.com/jaydenseric/graphql-react) | ||
|
||
[`graphql-react`](https://github.com/jaydenseric/graphql-react) is a lightweight but powerful [GraphQL](https://graphql.org) client for React; the first [Relay](https://facebook.github.io/relay) and [Apollo](https://apollographql.com/docs/react) alternative with server side rendering. | ||
|
||
See how it can be used in a Next.js app for GraphQL queries with server side rendering and client side data hydration: | ||
|
||
- In `pages/_app.js` a [custom `App` component](https://github.com/zeit/next.js#custom-app) is decorated with the [`withGraphQL`](https://github.com/jaydenseric/next-graphql-react/#function-withgraphql) [higher-order component](https://reactjs.org/docs/higher-order-components) from [`next-graphql-react`](https://github.com/jaydenseric/next-graphql-react), generating a `graphql` prop that populates the [`Provider`](https://github.com/jaydenseric/graphql-react#function-provider) component from [`graphql-react`](https://github.com/jaydenseric/graphql-react). | ||
- In `pages/index.js` the [`Query`](https://github.com/jaydenseric/graphql-react#function-query) component from [`graphql-react`](https://github.com/jaydenseric/graphql-react) is used to query the [GraphQL Pokémon API](https://github.com/lucasbento/graphql-pokemon) and show a picture of Pikachu. | ||
|
||
## Setup | ||
|
||
1. Download the example: | ||
|
||
```sh | ||
curl https://codeload.github.com/zeit/next.js/tar.gz/canary | tar -xz --strip=2 next.js-canary/examples/with-graphql-react | ||
``` | ||
|
||
2. Change directory to it: | ||
|
||
```sh | ||
cd with-graphql-react | ||
``` | ||
|
||
3. Install it: | ||
|
||
```sh | ||
npm install | ||
``` | ||
|
||
4. Run it: | ||
|
||
```sh | ||
npm run dev | ||
``` | ||
|
||
## Deploy | ||
|
||
[![Deploy to now](https://deploy.now.sh/static/button.svg)](https://deploy.now.sh/?repo=https://github.com/zeit/next.js/tree/master/examples/with-graphl-react) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
{ | ||
"name": "next-graphql-react-example", | ||
"private": true, | ||
"license": "ISC", | ||
"dependencies": { | ||
"cross-fetch": "^3.0.0", | ||
"graphql-react": "^6.0.0", | ||
"next": "^7.0.0", | ||
"next-graphql-react": "^1.0.1", | ||
"react": "^16.6.0", | ||
"react-dom": "^16.6.0" | ||
}, | ||
"scripts": { | ||
"dev": "next", | ||
"build": "next build", | ||
"start": "next start" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import 'cross-fetch/polyfill' | ||
import { Provider } from 'graphql-react' | ||
import { withGraphQL } from 'next-graphql-react' | ||
import App, { Container } from 'next/app' | ||
|
||
class CustomApp extends App { | ||
render () { | ||
const { Component, pageProps, graphql } = this.props | ||
return ( | ||
<Container> | ||
<Provider value={graphql}> | ||
<Component {...pageProps} /> | ||
</Provider> | ||
</Container> | ||
) | ||
} | ||
} | ||
|
||
export default withGraphQL(CustomApp) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { Query } from 'graphql-react' | ||
|
||
export default () => ( | ||
<Query | ||
loadOnMount | ||
loadOnReset | ||
fetchOptionsOverride={options => { | ||
options.url = 'https://graphql-pokemon.now.sh' | ||
}} | ||
operation={{ | ||
query: /* GraphQL */ ` | ||
{ | ||
pokemon(name: "Pikachu") { | ||
name | ||
image | ||
} | ||
} | ||
` | ||
}} | ||
> | ||
{({ data, loading }) => | ||
data ? ( | ||
<img src={data.pokemon.image} alt={data.pokemon.name} /> | ||
) : loading ? ( | ||
<p>Loading…</p> | ||
) : ( | ||
<p>Error!</p> | ||
) | ||
} | ||
</Query> | ||
) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should probably use
latest
like the other examples.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
At the time I surveyed the examples and noticed about 17 don't use
latest
for thenext
dependency. This is a good thing, since examples are not guaranteed to work when (not if) breaking changes tonext
are published in a major version.There are almost no valid use cases for
*
orlatest
. Inexperienced people that download and run examples locally are likely to use them as a boilerplate for their own projects, and they will not know thatlatest
is risky anti-pattern that will eventually blow up their project.When the next major version of
next
is published I will be sure to publish a new version ofnext-graphql-react
that supports it, with an updated peer-dependency range, and update the dependency in the example here.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seeing the major version of
next
supported by each example inpackage.json
is also useful, as when a major new version is published not every example is guaranteed to work on day 1. It lets you know how up to date the example and the third-party projects are.