Skip to content

Commit

Permalink
remove code and restart
Browse files Browse the repository at this point in the history
  • Loading branch information
sam-kvale-sap committed Aug 8, 2019
1 parent 231b321 commit 04e5596
Show file tree
Hide file tree
Showing 41 changed files with 6,441 additions and 6,092 deletions.
1 change: 0 additions & 1 deletion .env

This file was deleted.

1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
src/schema.ts
23 changes: 0 additions & 23 deletions .eslintrc

This file was deleted.

27 changes: 27 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
module.exports = {
extends: ['react-app', 'prettier'],
globals: {
document: true,
expect: true,
process: true,
test: true,
jest: true,
beforeEach: true,
window: true
},
parser: '@typescript-eslint/parser',
plugins: [
'jsx-a11y',
'react',
'react-hooks',
'@typescript-eslint',
'prettier'
],
rules: {
'jsx-quotes': ['error', 'prefer-single'],
'no-multiple-empty-lines': ['error', { max: 1 }],
'react/jsx-indent': [1, 2],
'react/prop-types': 1,
'prettier/prettier': 'error'
}
}
77 changes: 15 additions & 62 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
React Postgraphile
----
# React Postgraphile

### Why?

I wanted to learn about Graphql and JWT tokens in Postgres

### What is it
:construction:

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

```
```sh
.+------+ +------+ +------+ +------+.
.' | .'| /| /| |\ |\ |`. | `.
+---+--+' | +-+----+ | | +----+-+ | `+--+---+
Expand All @@ -28,22 +23,16 @@ Using React-Apollo and Postgraphile's API generation.
+------+
```

Creating users and and authenticate with JWTs in Postgres.
Add a Github auth token and manage your Pull Requests / Repos

### You'll 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`

### Starting it
```bash
cd react-postgraphile
```

Start Postgres

```bash
```sh
# for MacOS
brew services start postgres
# or
Expand All @@ -52,68 +41,32 @@ brew services start postgres

Create a database:

```bash
```sh
createdb react_postgraphile
```

Populate it

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

Start Postgraphile to create a Graphql layer over the created database

```bash
```sh
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 />
Generate the Types from the running schema

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}`} : {}
}
}))
```sh
yarn run types:gen
```

and Postgraphile uses that to know which user you are.

When the app runs the query
Run the React app in a new terminal

```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;
```sh
yarn start
```

and passes it into the component.

<img alt='User authenticated' src='./images/image2.png' />
10 changes: 10 additions & 0 deletions codegen.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
schema: http://localhost:5000/graphql
generates:
src/schema.ts:
- add:
content:
- '/* eslint-disable */'
- '/*------------------'
- ' This file is generated and should not be edited'
- '--------------------*/'
- typescript
Loading

0 comments on commit 04e5596

Please sign in to comment.