-
Notifications
You must be signed in to change notification settings - Fork 91
chore: add example SSR app built using Fastify #110
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
Merged
Merged
Changes from 9 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
8bde733
Add example SSR app built using Fastify, this was built by @bmullan91…
Joezo eb06cd0
Update example package name
Joezo 94a0365
Add readme to example
Joezo 164ca18
Update examples/fastify-ssr/README.md
jackdclark 3915d89
Remove article page.
Joezo 01b1bb1
Update eslint glob
Joezo 597a141
Update example readme
Joezo b58e9b3
Update contributing with instructions to use example app
Joezo f0455db
Don't prettier changelogs
Joezo d51b302
Add space back
Joezo 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 hidden or 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,5 @@ | ||
| node_modules | ||
| dist | ||
| es | ||
| lib | ||
| build | ||
This file contains hidden or 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
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -4,7 +4,6 @@ about: Bugs or any unexpected issues. | |
| title: '' | ||
| labels: bug | ||
| assignees: '' | ||
|
|
||
| --- | ||
|
|
||
| ### Package | ||
|
|
||
This file contains hidden or 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
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -8,3 +8,4 @@ dist | |
| es | ||
| packages/graphql-hooks/README.md | ||
| packages/*/LICENSE | ||
| build | ||
This file contains hidden or 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,6 @@ | ||
| node_modules | ||
| dist | ||
| es | ||
| lib | ||
| build | ||
| CHANGELOG.md |
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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,32 @@ | ||
| # GraphQL Hooks Fastify SSR Example | ||
|
|
||
| This example uses [Fastify](https://github.com/fastify/fastify) to serve a graphql server and do server side rendering. It's very basic but showcases the different functionality that `graphql-hooks` offers. This example is intended more for local development of the `graphql-hooks` packages. | ||
|
|
||
| ## How to use | ||
|
|
||
| ### Running as part of this repo | ||
|
|
||
| In the root of this repository run: | ||
|
|
||
| ```bash | ||
| npm install | ||
| lerna run build | ||
| cd examples/fastify-ssr | ||
| npm run watch | ||
| ``` | ||
|
|
||
| To develop `packages/` with this example locally, you'll need to run `lerna run build` from the root to rebuild files after they've been changed. | ||
|
|
||
| ### Download the example in isolation: | ||
|
|
||
| ```bash | ||
| curl https://codeload.github.com/nearform/graphql-hooks/tar.gz/master | tar -xz --strip=2 graphql-hooks-master/examples/fastify-ssr | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| cd fastify-ssr | ||
| ``` | ||
|
|
||
| Install it and run: | ||
|
|
||
| ```bash | ||
| npm install | ||
| npm run watch | ||
| ``` | ||
This file contains hidden or 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,67 @@ | ||
| { | ||
| "name": "fastify-ssr", | ||
| "version": "1.0.0", | ||
| "private": true, | ||
| "description": "", | ||
| "main": "index.js", | ||
| "scripts": { | ||
| "test": "echo \"Error: no test specified\" && exit 1", | ||
| "build:client": "webpack", | ||
| "build:server": "babel src -d lib --copy-files", | ||
| "prebuild": "rm -rf build lib", | ||
| "build": "npm run build:server && npm run build:client", | ||
| "start": "node ./lib/index.js", | ||
| "watch:client": "webpack --watch", | ||
| "watch:server": "nodemon --watch src --ignore src/client --exec 'npm run build:server && npm run start | pino-pretty'", | ||
| "prewatch": "npm run prebuild", | ||
| "watch": "npm run build:client && npm run watch:server & npm run watch:client" | ||
| }, | ||
| "keywords": [], | ||
| "author": "", | ||
| "license": "ISC", | ||
| "dependencies": { | ||
| "@reach/router": "^1.2.1", | ||
| "babel-plugin-dynamic-import-node": "^2.2.0", | ||
| "fastify": "^1.14.1", | ||
| "fastify-gql": "^0.8.0", | ||
| "fastify-static": "^1.1.0", | ||
| "graphql-hooks": "^3.0.0", | ||
| "graphql-hooks-memcache": "^1.0.4", | ||
| "graphql-hooks-ssr": "^1.0.1", | ||
| "isomorphic-unfetch": "^3.0.0", | ||
| "react": "16.8.4", | ||
| "react-dom": "16.8.4", | ||
| "react-tree-walker": "^4.3.0" | ||
| }, | ||
| "devDependencies": { | ||
| "@babel/cli": "^7.2.3", | ||
| "@babel/core": "^7.2.2", | ||
| "@babel/preset-env": "^7.3.1", | ||
| "@babel/preset-react": "^7.0.0", | ||
| "babel-loader": "^8.0.5", | ||
| "nodemon": "^1.18.9", | ||
| "pino-pretty": "^2.5.0", | ||
| "webpack": "^4.29.1", | ||
| "webpack-cli": "^3.2.3", | ||
| "webpack-manifest-plugin": "^2.0.4", | ||
| "webpack-merge": "^4.2.1" | ||
| }, | ||
| "browserslist": "> 0.25%, not dead", | ||
| "babel": { | ||
| "presets": [ | ||
| [ | ||
| "@babel/preset-env", | ||
| { | ||
| "targets": { | ||
| "node": "current" | ||
| } | ||
| } | ||
| ], | ||
| "@babel/preset-react" | ||
| ], | ||
| "plugins": [ | ||
| "@babel/plugin-proposal-object-rest-spread", | ||
| "dynamic-import-node" | ||
| ] | ||
| } | ||
| } |
This file contains hidden or 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,30 @@ | ||
| import React from 'react' | ||
| import { Link, Router } from '@reach/router' | ||
|
|
||
| // components | ||
| import NotFoundPage from './pages/NotFoundPage' | ||
| import HomePage from './pages/HomePage' | ||
| import PaginationPage from './pages/PaginationPage' | ||
|
|
||
| class AppShell extends React.Component { | ||
| render() { | ||
| return ( | ||
| <div className="app-shell-component"> | ||
| <h1>GraphQL Hooks</h1> | ||
| <nav> | ||
| <Link to="/">Home</Link> | ||
| <Link to="/users">PaginationPage</Link> | ||
| </nav> | ||
| <Router> | ||
| <HomePage path="/" /> | ||
| <PaginationPage path="/users" /> | ||
| <NotFoundPage default /> | ||
| </Router> | ||
| </div> | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| AppShell.propTypes = {} | ||
|
|
||
| export default AppShell |
This file contains hidden or 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,28 @@ | ||
| import React from 'react' | ||
| import PropTypes from 'prop-types' | ||
| import { useQuery } from 'graphql-hooks' | ||
|
|
||
| const HELLO_QUERY = ` | ||
| query Hello($name: String) { | ||
| hello(name: $name) | ||
| } | ||
| ` | ||
|
|
||
| function HelloComponent({ user }) { | ||
| const { loading, error, data } = useQuery(HELLO_QUERY, { | ||
| variables: { name: user.name } | ||
| }) | ||
|
|
||
| if (loading) return 'loading HelloComponent...' | ||
| if (error) return 'error HelloComponent' | ||
|
|
||
| return <div>{data.hello}</div> | ||
| } | ||
|
|
||
| HelloComponent.propTypes = { | ||
| user: PropTypes.shape({ | ||
| name: PropTypes.string | ||
| }) | ||
| } | ||
|
|
||
| export default HelloComponent |
This file contains hidden or 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,82 @@ | ||
| import React, { Fragment } from 'react' | ||
|
|
||
| import { useQuery, useManualQuery, useMutation } from 'graphql-hooks' | ||
|
|
||
| // components | ||
| import HelloComponent from '../components/Hello' | ||
|
|
||
| const HOMEPAGE_QUERY = ` | ||
| query HomepageQuery { | ||
| users { | ||
| name | ||
| } | ||
| } | ||
| ` | ||
|
|
||
| const GET_FIRST_USER_QUERY = ` | ||
| query FirstUser { | ||
| firstUser { | ||
| name | ||
| } | ||
| } | ||
| ` | ||
|
|
||
| const CREATE_USER_MUTATION = ` | ||
| mutation CreateUser($name: String!) { | ||
| createUser(name: $name) { | ||
| name | ||
| } | ||
| } | ||
| ` | ||
|
|
||
| function HomePage() { | ||
| const [name, setName] = React.useState('') | ||
| const { loading, data, error, refetch: refetchUsers } = useQuery( | ||
| HOMEPAGE_QUERY | ||
| ) | ||
| const [createUser] = useMutation(CREATE_USER_MUTATION) | ||
|
|
||
| const [getFirstUser, { data: firstUserData }] = useManualQuery( | ||
| GET_FIRST_USER_QUERY | ||
| ) | ||
|
|
||
| async function createNewUser() { | ||
| await createUser({ variables: { name } }) | ||
| setName('') | ||
| refetchUsers() | ||
| } | ||
|
|
||
| return ( | ||
| <div> | ||
| Home page | ||
| {loading && <div>...loading</div>} | ||
| {error && <div>error occured</div>} | ||
| {!loading && !error && data.users && ( | ||
| <Fragment> | ||
| List of users: | ||
| {data.users.length === 0 && <span> No users found</span>} | ||
| {!!data.users.length && ( | ||
| <ul> | ||
| {data.users.map((user, i) => ( | ||
| <li key={i}>{user.name}</li> | ||
| ))} | ||
| </ul> | ||
| )} | ||
| <HelloComponent user={data.users[0]} /> | ||
| </Fragment> | ||
| )} | ||
| <div> | ||
| <input | ||
| type="text" | ||
| value={name} | ||
| onChange={e => setName(e.target.value)} | ||
| /> | ||
| <button onClick={createNewUser}>Create User</button> | ||
| </div> | ||
| <button onClick={getFirstUser}>Manually trigger Query</button> | ||
| <div>First User: {firstUserData && firstUserData.firstUser.name}</div> | ||
| </div> | ||
| ) | ||
| } | ||
|
|
||
| export default HomePage |
This file contains hidden or 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,7 @@ | ||
| import React from 'react' | ||
|
|
||
| function NotFoundPage() { | ||
| return <div>404 - Not Found</div> | ||
| } | ||
|
|
||
| export default NotFoundPage |
This file contains hidden or 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,36 @@ | ||
| import React from 'react' | ||
|
|
||
| import { useQuery } from 'graphql-hooks' | ||
|
|
||
| const USERS_QUERY = ` | ||
| query UsersQuery($skip: Int, $limit: Int) { | ||
| users(skip: $skip, limit: $limit) { | ||
| name | ||
| } | ||
| } | ||
| ` | ||
|
|
||
| function PaginationPage() { | ||
| const [page, setPage] = React.useState(1) | ||
| const { data } = useQuery(USERS_QUERY, { | ||
| variables: { | ||
| limit: 1, | ||
| skip: page - 1 | ||
| } | ||
| }) | ||
|
|
||
| return ( | ||
| <div> | ||
| User Pagination Users: | ||
| <ul> | ||
| {data && | ||
| data.users && | ||
| data.users.map((user, i) => <li key={i}>{user.name}</li>)} | ||
| </ul> | ||
| <button onClick={() => setPage(page - 1)}>Prev</button> | ||
| <button onClick={() => setPage(page + 1)}>Next</button> | ||
| </div> | ||
| ) | ||
| } | ||
|
|
||
| export default PaginationPage |
This file contains hidden or 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,22 @@ | ||
| import React from 'react' | ||
| import { hydrate } from 'react-dom' | ||
|
|
||
| import AppShell from '../../app/AppShell' | ||
|
|
||
| // graphql-hooks | ||
| import { GraphQLClient, ClientContext } from 'graphql-hooks' | ||
| import memCache from 'graphql-hooks-memcache' | ||
|
|
||
| const initialState = window.__INITIAL_STATE__ | ||
| const client = new GraphQLClient({ | ||
| url: '/graphql', | ||
| cache: memCache({ initialState }) | ||
| }) | ||
|
|
||
| const App = ( | ||
| <ClientContext.Provider value={client}> | ||
| <AppShell /> | ||
| </ClientContext.Provider> | ||
| ) | ||
|
|
||
| hydrate(App, document.getElementById('app-root')) |
This file contains hidden or 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 @@ | ||
| require('./server')() |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.