Skip to content

Commit 8d6b908

Browse files
authored
Merge pull request #15 from bineetNaidu/issue#12
Issue#12
2 parents 2556bc0 + 33fc73e commit 8d6b908

File tree

28 files changed

+452
-68
lines changed

28 files changed

+452
-68
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
[
2+
{
3+
"name": "development",
4+
"type": "postgres",
5+
"host": "localhost",
6+
"port": 5432,
7+
"username": "postgres",
8+
"password": "DB_PASSWORD",
9+
"database": "DB_DEV_DATABASE",
10+
"logging": true,
11+
"synchronize": true,
12+
"entities": ["src/entities/**/*.ts"]
13+
},
14+
{
15+
"name": "test",
16+
"type": "postgres",
17+
"host": "localhost",
18+
"port": 5432,
19+
"username": "postgres",
20+
"password": "DB_PASSWORD",
21+
"database": "DB_DEV_DATABASE",
22+
"logging": false,
23+
"synchronize": true,
24+
"entities": ["src/entities/**/*.ts"]
25+
}
26+
]

packages/templates/gql-psql/package.json

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,20 @@
44
"description": "A Graphql + postgresql api bootstraped w/ create-ts-api",
55
"main": "index.js",
66
"scripts": {
7-
"start": "node dist/index.js",
8-
"dev": "nodemon dist/index.js",
9-
"dev:api": "nodemon src/index.ts",
7+
"start": "NODE_ENV=production node dist/index.js",
8+
"dev": "NODE_ENV=development nodemon dist/index.js",
9+
"dev:api": "NODE_ENV=development nodemon src/index.ts",
1010
"watch": "tsc -w",
11-
"build": "tsc"
11+
"build": "tsc",
12+
"test": "NODE_ENV=test jest",
13+
"test:watch": "NODE_ENV=test jest --watchAll --no-cache"
14+
},
15+
"jest": {
16+
"preset": "ts-jest",
17+
"testEnvironment": "node",
18+
"setupFilesAfterEnv": [
19+
"./src/test/setup.ts"
20+
]
1221
},
1322
"keywords": [],
1423
"author": "BineetNaidu (https://bineetnaidu.github.io/)",
@@ -24,7 +33,11 @@
2433
"typeorm": "^0.2.34"
2534
},
2635
"devDependencies": {
36+
"@types/jest": "^26.0.24",
37+
"apollo-server-testing": "^2.25.2",
38+
"jest": "^27.0.6",
2739
"nodemon": "^2.0.7",
40+
"ts-jest": "^27.0.4",
2841
"ts-node": "^10.0.0",
2942
"typescript": "^4.3.2"
3043
}

packages/templates/gql-psql/src/index.ts

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,9 @@ import dotenv from 'dotenv';
33
import { ApolloServer } from 'apollo-server';
44
import { buildSchema } from 'type-graphql';
55
import { HelloResolver } from './resolvers/Hello';
6-
import { Tweet } from './entities/Tweet';
7-
import { createConnection } from 'typeorm';
86
import { ___prod___ } from './utils/contants';
97
import { TweetResolvers } from './resolvers/tweets';
8+
import { createTypeORMConnection } from './utils/createTypeORMConnection';
109

1110
dotenv.config();
1211

@@ -15,13 +14,7 @@ const bootstrap = async () => {
1514
throw new Error('??>> {" DATABASE_URI must be defined!! "} ');
1615
}
1716

18-
const conn = await createConnection({
19-
type: 'postgres',
20-
url: process.env.DATABASE_URI,
21-
logging: true,
22-
synchronize: !___prod___,
23-
entities: [Tweet],
24-
});
17+
const conn = await createTypeORMConnection();
2518

2619
if (!conn.isConnected) {
2720
throw new Error('Database Connection has not been established yet!');
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
3+
* IMPORTANT:
4+
IF YOU HAVE ANY BETTER GRAPHQL TESTS IMPLEMENTATIONS,
5+
THEN PLEASE SEND THEM TO ME VIA A GITHUB ISSUE OR PR,
6+
I'LL UPDATE THIS FILE
7+
8+
*/
9+
10+
import { gql } from 'apollo-server';
11+
import { ApolloServerTestClient } from 'apollo-server-testing';
12+
import { testServer } from '../../test/testServer';
13+
14+
let server: ApolloServerTestClient;
15+
16+
testServer()
17+
.then((s) => {
18+
return (server = s);
19+
})
20+
.catch((err) => {
21+
console.error(err);
22+
});
23+
24+
it('should return a hello world message', async () => {
25+
const query = gql`
26+
query {
27+
hello
28+
}
29+
`;
30+
const result = await server.query({ query });
31+
expect(result.data).toEqual({ hello: 'Hello World 👋🌎' });
32+
});
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
3+
* IMPORTANT:
4+
IF YOU HAVE ANY BETTER GRAPHQL TESTS IMPLEMENTATIONS,
5+
THEN PLEASE SEND THEM TO ME VIA A GITHUB ISSUE OR PR,
6+
I'LL UPDATE THIS FILE
7+
8+
*/
9+
10+
import { gql } from 'apollo-server';
11+
import { ApolloServerTestClient } from 'apollo-server-testing';
12+
import { testServer } from '../../test/testServer';
13+
14+
let server: ApolloServerTestClient;
15+
16+
testServer()
17+
.then((s) => {
18+
return (server = s);
19+
})
20+
.catch((err) => {
21+
console.error(err);
22+
});
23+
24+
it('should return all tweets', async () => {
25+
const query = gql`
26+
query Tweets {
27+
tweets {
28+
id
29+
body
30+
username
31+
}
32+
}
33+
`;
34+
const result = await server.query({ query });
35+
expect(result.data.tweets).toBeInstanceOf(Array);
36+
});
37+
38+
it('should create a tweet', async () => {
39+
const mutation = gql`
40+
mutation CreateTweet($body: String!, $username: String!) {
41+
createTweet(body: $body, username: $username) {
42+
id
43+
body
44+
username
45+
}
46+
}
47+
`;
48+
const variables = {
49+
body: 'test tweet',
50+
username: 'test_user',
51+
};
52+
const result = await server.mutate({ mutation, variables });
53+
expect(result.data.createTweet).toBeDefined();
54+
expect(result.data.createTweet.body).toEqual(variables.body);
55+
expect(result.data.createTweet.username).toEqual(variables.username);
56+
});
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { Connection } from 'typeorm';
2+
import { createTypeORMConnection } from '../utils/createTypeORMConnection';
3+
4+
let conn: Connection;
5+
6+
beforeAll(async () => {
7+
conn = await createTypeORMConnection();
8+
});
9+
10+
afterAll(async () => {
11+
await conn.dropDatabase();
12+
await conn.close();
13+
});
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
3+
* IMPORTANT:
4+
IF YOU HAVE ANY BETTER GRAPHQL TESTS IMPLEMENTATIONS,
5+
THEN PLEASE SEND THEM TO ME VIA A GITHUB ISSUE OR PR,
6+
I'LL UPDATE THIS FILE
7+
8+
*/
9+
import 'reflect-metadata';
10+
import { ApolloServer } from 'apollo-server';
11+
import { buildSchema } from 'type-graphql';
12+
import path from 'path';
13+
import { createTestClient } from 'apollo-server-testing';
14+
15+
export const testServer = async () => {
16+
const server = new ApolloServer({
17+
schema: await buildSchema({
18+
validate: false,
19+
resolvers: [path.join(__dirname, '../resolvers/**/*.ts')],
20+
}),
21+
});
22+
23+
return createTestClient(server);
24+
};
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { createConnection, getConnectionOptions } from 'typeorm';
2+
3+
export const createTypeORMConnection = async () => {
4+
const options = await getConnectionOptions(process.env.NODE_ENV);
5+
return createConnection({ ...options, name: 'default' });
6+
};

packages/templates/gql/package.json

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,15 @@
66
"scripts": {
77
"start": "node dist/index.js",
88
"dev": "nodemon dist/index.js",
9-
"dev:api": "nodemon src/index.ts",
9+
"dev:ts": "nodemon src/index.ts",
1010
"watch": "tsc -w",
11-
"build": "tsc"
11+
"build": "tsc",
12+
"test": "NODE_ENV=test jest",
13+
"test:watch": "NODE_ENV=test jest --watchAll --no-cache"
14+
},
15+
"jest": {
16+
"preset": "ts-jest",
17+
"testEnvironment": "node"
1218
},
1319
"keywords": [],
1420
"author": "BineetNaidu (https://bineetnaidu.github.io/)",
@@ -21,7 +27,11 @@
2127
"type-graphql": "^1.1.1"
2228
},
2329
"devDependencies": {
30+
"@types/jest": "^26.0.24",
31+
"apollo-server-testing": "^2.25.2",
32+
"jest": "^27.0.6",
2433
"nodemon": "^2.0.7",
34+
"ts-jest": "^27.0.4",
2535
"ts-node": "^10.0.0",
2636
"typescript": "^4.3.2"
2737
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
3+
* IMPORTANT:
4+
IF YOU HAVE ANY BETTER GRAPHQL TESTS IMPLEMENTATIONS,
5+
THEN PLEASE SEND THEM TO ME VIA A GITHUB ISSUE OR PR,
6+
I'LL UPDATE THIS FILE
7+
8+
*/
9+
10+
import { gql } from 'apollo-server';
11+
import { ApolloServerTestClient } from 'apollo-server-testing';
12+
import { testServer } from '../../test/testServer';
13+
14+
let server: ApolloServerTestClient;
15+
16+
testServer()
17+
.then((s) => {
18+
return (server = s);
19+
})
20+
.catch((err) => {
21+
console.error(err);
22+
});
23+
24+
it('should return a hello world message', async () => {
25+
const query = gql`
26+
query {
27+
hello
28+
}
29+
`;
30+
const result = await server.query({ query });
31+
expect(result.data).toEqual({ hello: 'Hello World 👋🌎' });
32+
});

0 commit comments

Comments
 (0)