Skip to content
This repository was archived by the owner on Oct 7, 2018. It is now read-only.

Commit 99f5bcb

Browse files
Kun Ranpradel
Kun Ran
authored andcommitted
feat: create react-graphql-typescript example (#162)
* refactor: copy react-typescript to react-graphql-typescript * refactor: change accounts.ts to use GraphQL client * feat: implement features * fix: not be able to load home * fix: not loading user ip and ua * docs: update readme * docs: update readme * docs: update readme with guides * docs: update readme * docs: update comments * fix: two factor not working * feat: html tag * fix: two factor authentication * add nodemon as dev dep * add tests to the ci * try to trigger ci * update @acounts deps * update to 0.3.0-beta.20 * Fix client build
1 parent eafcb89 commit 99f5bcb

30 files changed

+12360
-1
lines changed

.circleci/config.yml

+2
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,5 @@ jobs:
1010
- run: yarn test
1111
- run: cd react-typescript && yarn install --frozen-lockfile && yarn test
1212
- run: cd rest-express-typescript && yarn install --frozen-lockfile && yarn test
13+
- run: cd react-graphql-typescript && yarn install --frozen-lockfile && yarn test
14+
- run: cd graphql-server-typescript && yarn install --frozen-lockfile && yarn test

.gitignore

+4-1
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,7 @@ npm-debug.log
55
yarn-error.log
66
.idea
77
dump.rdb
8-
build
8+
build/
9+
.vscode
10+
package-lock.json
11+
*/package-lock.json

README.md

+12
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,15 @@
11
# Accounts-js examples
22

33
This repository provides examples on how to use the [accounts-js](https://github.com/accounts-js/accounts) packages.
4+
5+
REST Server + REST client example:
6+
7+
- Server: [rest-express-typescript](./rest-express-typescript)
8+
- Client: [react-typescript](./react-typescript)
9+
10+
GraphQL Server + GraphQL Client example:
11+
12+
- Server: [graphql-server-typescript](./graphql-server-typescript)
13+
- Client: [react-graphql-typescript](./react-graphql-typescript)
14+
15+
Please go to corresponding packages and see its README.

graphql-server-typescript/.DS_Store

6 KB
Binary file not shown.

graphql-server-typescript/README.md

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# graphql-server-typescript
2+
3+
This example demonstrate how to use [accounts-js](https://github.com/accounts-js/accounts).
4+
5+
## Prerequisites
6+
7+
You will need a MongoDB server to run this server. If you don't have a MongoDB server running already, and you have Docker & Docker Compose, you can do
8+
9+
```bash
10+
docker-compose up -d
11+
```
12+
13+
to start a new one.
14+
15+
## Getting Started
16+
17+
```bash
18+
git clone https://github.com/accounts-js/examples.git
19+
cd graphql-server-typescript
20+
yarn install
21+
```
22+
23+
Start the app.
24+
25+
```bash
26+
yarn start
27+
```
28+
29+
-> [Start the client side](../react-graphql-typescript).
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
version: '3.6'
2+
services:
3+
db-mongo-accounts:
4+
image: mongo:3.6.5-jessie
5+
ports:
6+
- "27017:27017"
7+
restart: always
+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
{
2+
"name": "rest-express-typescript",
3+
"private": true,
4+
"version": "1.0.0",
5+
"main": "lib/index.js",
6+
"license": "MIT",
7+
"scripts": {
8+
"start-only": "NODE_ENV=development nodemon -w src -x ts-node src/index.ts",
9+
"start": "yarn start-only",
10+
"start-services": "docker-compose up -d",
11+
"prestart": "yarn start-services",
12+
"build": "tsc",
13+
"test": "yarn build"
14+
},
15+
"dependencies": {
16+
"@accounts/database-manager": "0.3.0-beta.20",
17+
"@accounts/graphql-api": "0.3.0-beta.20",
18+
"@accounts/mongo": "0.3.0-beta.20",
19+
"@accounts/password": "0.3.0-beta.20",
20+
"@accounts/rest-express": "0.3.0-beta.20",
21+
"@accounts/server": "0.3.0-beta.20",
22+
"@types/mongoose": "5.2.0",
23+
"apollo-server": "2.0.0-rc.5",
24+
"cors": "2.8.4",
25+
"express": "4.16.3",
26+
"graphql": "0.13.2",
27+
"graphql-tools": "3.0.5",
28+
"mongoose": "5.2.2"
29+
},
30+
"devDependencies": {
31+
"@types/node": "10.5.2",
32+
"nodemon": "1.17.5",
33+
"ts-node": "7.0.0",
34+
"typescript": "2.9.2"
35+
}
36+
}
+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import * as mongoose from 'mongoose';
2+
import { AccountsServer } from '@accounts/server';
3+
import { AccountsPassword } from '@accounts/password';
4+
import { ApolloServer, makeExecutableSchema } from 'apollo-server';
5+
import MongoDBInterface from '@accounts/mongo';
6+
import { createAccountsGraphQL, accountsContext } from '@accounts/graphql-api';
7+
import { DatabaseManager } from '@accounts/database-manager';
8+
9+
const start = async () => {
10+
// Create database connection
11+
await mongoose.connect(
12+
'mongodb://localhost:27017/accounts-js-graphql-example'
13+
);
14+
const mongoConn = mongoose.connection;
15+
16+
// Build a storage for storing users
17+
const userStorage = new MongoDBInterface(mongoConn);
18+
// Create database manager (create user, find users, sessions etc) for Accounts-js
19+
const accountsDb = new DatabaseManager({
20+
sessionStorage: userStorage,
21+
userStorage,
22+
});
23+
24+
// Create accounts server that holds a lower level of all accounts operations
25+
const accountsServer = new AccountsServer(
26+
{ db: accountsDb, tokenSecret: 'secret' },
27+
{
28+
password: new AccountsPassword(),
29+
}
30+
);
31+
32+
// Create GraphQL schema (with resolvers) for accounts server, exposes a GraphQL API
33+
const { typeDefs, resolvers } = createAccountsGraphQL(accountsServer, {
34+
extend: false,
35+
});
36+
37+
// Only schema is not enough, we need to hook on resolvers with its provided function.
38+
// @ts-ignore
39+
const finalSchema = makeExecutableSchema({ typeDefs, resolvers });
40+
41+
// Create the Apollo Server that takes a schema and configures internal stuff
42+
const server = new ApolloServer({
43+
schema: finalSchema,
44+
context: ({ req }) => accountsContext(req),
45+
});
46+
47+
server.listen(4000).then(({ url }) => {
48+
console.log(`🚀 Server ready at ${url}`);
49+
});
50+
};
51+
52+
start();
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"compilerOptions": {
3+
"outDir": "./lib",
4+
"target": "es5",
5+
"lib": ["es2015", "esnext.asynciterable"],
6+
"sourceMap": true
7+
},
8+
"include": ["./src/**/*"],
9+
"exclude": ["node_modules", "lib"]
10+
}

0 commit comments

Comments
 (0)