Skip to content

Commit

Permalink
use create react app
Browse files Browse the repository at this point in the history
  • Loading branch information
Sam Kvale committed Apr 14, 2018
0 parents commit a0d4983
Show file tree
Hide file tree
Showing 34 changed files with 10,006 additions and 0 deletions.
1 change: 1 addition & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
NODE_PATH=.
23 changes: 23 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"extends": ["eslint:recommended", "plugin:react/recommended"],
"globals": {
"document": true,
"expect": true,
"process": true,
"test": true,
"jest": true,
"beforeEach": true,
"window": true
},
"parser": "babel-eslint",
"plugins": [
"jsx-a11y",
"react"
],
"rules": {
"jsx-quotes": ["error", "prefer-single"],
"no-multiple-empty-lines": ["error", { "max": 1 }],
"react/jsx-indent": [1, 2],
"react/prop-types": 1
}
}
21 changes: 21 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# See https://help.github.com/ignore-files/ for more about ignoring files.

# dependencies
/node_modules

# testing
/coverage

# production
/build

# misc
.DS_Store
.env.local
.env.development.local
.env.test.local
.env.production.local

npm-debug.log*
yarn-debug.log*
yarn-error.log*
119 changes: 119 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
## React Postgraphile

### Why did you build this thing?

I wanted to learn about Graphql and JWT tokens in Postgres

### What is it

A React app communicating with a Postgresql database
Using React-Apollo and Postgraphile's API generation.

```
.+------+ +------+ +------+ +------+.
.' | .'| /| /| |\ |\ |`. | `.
+---+--+' | +-+----+ | | +----+-+ | `+--+---+
| | | | | | | | | | | | | | | |
| React| <-> |Apollo| |<-> Postgraphile <-> Postgresql
|.' | .' |/ |/ \| \| `. | `. |
+------+' +------ \ +------+ `+------+
\
\ +------+
|\ |\
| +----+-+
| | | |
.Github API
\| \|
+------+
```

### So what does the app do?
Creating users and and authenticate with JWTs in Postgres.
Add a Github auth token and manage your Pull Requests / Repos

### What do you need?
* [Postgresql](https://www.postgresql.org/download/)
* [Postgraphile](https://www.graphile.org/postgraphile) `yarn global add postgraphile` or `npm install -g postgraphile`
* react-postgraphile `git clone https://github.com/skvale/react-postgraphile`

### Getting it to work
```bash
cd react-postgraphile
```

Start Postgres

```bash
# for MacOS
brew services start postgres
# or
# postgres -D /usr/local/var/postgres
```

Create a database:

```bash
createdb react_postgraphile
```
Populate it

```bash
psql -f database/create.sql -d react_postgraphile
```

Start Postgraphile to create a Graphql layer over the created database

```bash
yarn run postgraphile
```

You can check your Graphql out at [localhost:5000/graphql](http://localhost:5000/graphql)

Run the React app in a new terminal

```bash
yarn start
```

and open [localhost:1234](http://localhost:1234) to see:

<img alt='User sign in' src='./images/image1.png' height=250 />

Registering a user creates a user as well as a jwt token and stores the token as a cookie called `jwt_token`. Apollo adds that token to the header requests

```js
const authLink = token =>
setContext((_, { headers }) => ({
headers: {
...headers,
...token ? {Authorization: `Bearer ${token}`} : {}
}
}))
```

and Postgraphile uses that to know which user you are.

When the app runs the query

```js
export default graphql(gql`
{
currentPerson {
fullName
}
}
`)(App)
```
Postgres gets the user identified by the jwt token on the request. No API server, just a database function.

```sql
create function react_postgraphile.current_person() returns react_postgraphile.person as $$
select *
from react_postgraphile.person
where id = current_setting('jwt.claims.person_id')::integer
$$ language sql stable;
```

and passes it into the component.

<img alt='User authenticated' src='./images/image2.png' />
42 changes: 42 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{
"name": "pull-request-notes",
"version": "0.1.0",
"private": true,
"description": "Learning about Graphql + Postgres + Apollo + Postgraphile + Github v4 api",
"license": "MIT",
"dependencies": {
"apollo-client": "^2.2.8",
"apollo-client-preset": "^1.0.8",
"apollo-link-context": "^1.0.8",
"bulma": "^0.7.0",
"graphql": "0.12.3",
"graphql-tag": "^2.8.0",
"normalizr": "^3.2.4",
"prop-types": "^15.6.1",
"react": "^16.3.1",
"react-apollo": "^2.1.3",
"react-dom": "^16.3.1",
"react-icons": "^2.2.7",
"react-redux": "^5.0.7",
"react-scripts": "1.1.4",
"redux": "^3.7.2",
"redux-little-router": "^15.1.0",
"roughjs": "^2.1.1"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"postgraphile": "postgraphile --connection postgres://localhost:5432/react_postgraphile --schema react_postgraphile --token react_postgraphile.jwt_token --default-role react_postgraphile_anonymous --secret your_secret --cors",
"test": "react-scripts test --env=jsdom",
"eject": "react-scripts eject"
},
"devDependencies": {
"apollo-utilities": "^1.0.11",
"eslint": "^4.19.1",
"eslint-plugin-jsx-a11y": "^6.0.3",
"eslint-plugin-react": "^7.7.0",
"jest": "^22.4.3",
"jest-fetch-mock": "^1.5.0",
"postgraphile": "^4.0.0-beta.5"
}
}
Binary file added public/favicon.ico
Binary file not shown.
40 changes: 40 additions & 0 deletions public/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta name="theme-color" content="#000000">
<!--
manifest.json provides metadata used when your web app is added to the
homescreen on Android. See https://developers.google.com/web/fundamentals/engage-and-retain/web-app-manifest/
-->
<link rel="manifest" href="%PUBLIC_URL%/manifest.json">
<link rel="shortcut icon" href="%PUBLIC_URL%/favicon.ico">
<!--
Notice the use of %PUBLIC_URL% in the tags above.
It will be replaced with the URL of the `public` folder during the build.
Only files inside the `public` folder can be referenced from the HTML.
Unlike "/favicon.ico" or "favicon.ico", "%PUBLIC_URL%/favicon.ico" will
work correctly both with client-side routing and a non-root public URL.
Learn how to configure a non-root public URL by running `npm run build`.
-->
<title>React App</title>
</head>
<body>
<noscript>
You need to enable JavaScript to run this app.
</noscript>
<div id="root"></div>
<!--
This HTML file is a template.
If you open it directly in the browser, you will see an empty page.
You can add webfonts, meta tags, or analytics to this file.
The build step will place the bundled scripts into the <body> tag.
To begin the development, run `npm start` or `yarn start`.
To create a production bundle, use `npm run build` or `yarn build`.
-->
</body>
</html>
15 changes: 15 additions & 0 deletions public/manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"short_name": "React App",
"name": "Create React App Sample",
"icons": [
{
"src": "favicon.ico",
"sizes": "64x64 32x32 24x24 16x16",
"type": "image/x-icon"
}
],
"start_url": "./index.html",
"display": "standalone",
"theme_color": "#000000",
"background_color": "#ffffff"
}
39 changes: 39 additions & 0 deletions src/apollo-provider.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import React from 'react'
import PropTypes from 'prop-types'
import { ApolloProvider } from 'react-apollo'
import { connect } from 'react-redux'
import { push } from 'redux-little-router'
import postgraphileClient from 'src/clients/postgraphile'
import Routes from 'src/routes'

class Provider extends React.Component {
componentDidMount() {
const { navigateToSignIn, token } = this.props
if(!token) {
navigateToSignIn()
}
}

render() {
const { token } = this.props
return (
<ApolloProvider client={postgraphileClient(token)}>
<Routes />
</ApolloProvider>
)
}
}

Provider.propTypes = {
navigateToSignIn: PropTypes.func,
token: PropTypes.string
}

export default connect(
({ tokens }) => ({
token: tokens.token
}),
dispatch => ({
navigateToSignIn: () => dispatch(push('/sign-in'))
})
)(Provider)
63 changes: 63 additions & 0 deletions src/app/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import React from 'react'
import PropTypes from 'prop-types'
import gql from 'graphql-tag'
import { connect } from 'react-redux'
import { graphql } from 'react-apollo'
import { Link } from 'redux-little-router'
import Github from 'src/app/github/github'
import { setUser } from 'src/reducers/users'

class App extends React.Component {
componentDidUpdate() {
const { data, setUser, user } = this.props
if (data
&& data.currentPerson
&& (!user || data.currentPerson.id !== user.id)) {
setUser(data.currentPerson)
}
}

render () {
const { data, user } = this.props
if (data.loading) {
return <div className='loading-text'></div>
}
if (!user) {
return <Link className='button' href='/sign-in'>Sign in</Link>
}

const GithubComponent = Github(user.githubAuth)
return (
<div className='section main'>
<div className='container'>
<GithubComponent />
</div>
</div>
)
}
}

export const QUERY = gql`
{
currentPerson {
fullName
githubAuth
id
}
}
`

App.propTypes = {
loading: PropTypes.bool,
user: PropTypes.shape({
fullName: PropTypes.string,
githubAuth: PropTypes.string
})
}

export default connect(
({ tokens, users }) => ({ token: tokens.token, user: users.user }),
({
setUser
})
)(graphql(QUERY)(App))
Loading

0 comments on commit a0d4983

Please sign in to comment.