Skip to content

Commit

Permalink
feat(playground): automatically open playground once server starts up (
Browse files Browse the repository at this point in the history
  • Loading branch information
goldcaddy77 authored Jan 20, 2019
1 parent 3533893 commit c40787f
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 22 deletions.
5 changes: 3 additions & 2 deletions examples/1-simple-model/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
"db:create": "createdbjs $(dotenv -p TYPEORM_DATABASE) 2>&1 || :",
"db:drop": "dropdbjs $(dotenv -p TYPEORM_DATABASE) 2>&1 || :",
"playground:open": "open http://localhost:$(dotenv -p APP_PORT)/graphql",
"start": "DEBUG=warthog* run-p start:ts playground:open",
"start:ts": "ts-node-dev --type-check src/index.ts",
"start": "yarn start:ts",
"start:ts": "DEBUG=warthog* ts-node-dev --type-check src/index.ts",
"start:prod": "ts-node src/index.ts",
"watch:ts": "nodemon -e ts,graphql -x ts-node --type-check src/index.ts"
},
"dependencies": {
Expand Down
2 changes: 1 addition & 1 deletion examples/2-complex-example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"db:seed:dev": "dotenv -- ts-node tools/seed.ts",
"lint": "tslint --fix -c ./tslint.json -p ./tsconfig.json",
"playground:open": "open http://localhost:$(dotenv -p APP_PORT)/playground",
"start": "npm-run-all --parallel start:ts playground:open",
"start": "yarn start:ts",
"start:debug": "yarn start:ts --inspect",
"start:ts": "ts-node --type-check src/index.ts",
"typeorm:cli": "ts-node -r tsconfig-paths/register ./node_modules/typeorm/cli.js -f ./ormconfig",
Expand Down
33 changes: 19 additions & 14 deletions examples/2-complex-example/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,24 @@ interface Context extends BaseContext {
};
}

export function getApp() {
return new App<Context>({
container: Container,
// Inject a fake user. In a real app you'd parse a JWT to add the user
context: request => {
return {
user: {
email: 'admin@test.com',
id: 'abc12345',
permissions: ['user:read', 'user:update', 'user:create', 'user:delete', 'photo:delete']
}
};
export function getApp(AppOptions = {}, dbOptions = {}) {
return new App<Context>(
{
container: Container,
// Inject a fake user. In a real app you'd parse a JWT to add the user
context: request => {
return {
user: {
email: 'admin@test.com',
id: 'abc12345',
permissions: ['user:read', 'user:update', 'user:create', 'user:delete', 'photo:delete']
}
};
},
// Path written in generated classes (only needed because we're in same repo)
warthogImportPath: '../../../src',
...AppOptions
},
warthogImportPath: '../../../src' // Path written in generated classes
});
dbOptions
);
}
2 changes: 1 addition & 1 deletion examples/3-one-to-many-relationship/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"db:create": "createdbjs $(dotenv -p TYPEORM_DATABASE) 2>&1 || :",
"db:drop": "dropdbjs $(dotenv -p TYPEORM_DATABASE) 2>&1 || :",
"playground:open": "open http://localhost:$(dotenv -p APP_PORT)/graphql",
"start": "npm-run-all --parallel start:ts playground:open",
"start": "yarn start:ts",
"start:ts": "ts-node --type-check src/index.ts",
"watch:ts": "nodemon -e ts,graphql -x ts-node --type-check src/index.ts"
},
Expand Down
2 changes: 1 addition & 1 deletion examples/4-many-to-many-relationship/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"db:create": "createdbjs $(dotenv -p TYPEORM_DATABASE) 2>&1 || :",
"db:drop": "dropdbjs $(dotenv -p TYPEORM_DATABASE) 2>&1 || :",
"playground:open": "open http://localhost:$(dotenv -p APP_PORT)/graphql",
"start": "npm-run-all --parallel start:ts playground:open",
"start": "yarn start:ts",
"start:ts": "ts-node --type-check src/index.ts",
"watch:ts": "nodemon -e ts,graphql -x ts-node --type-check src/index.ts"
},
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
"@types/mkdirp": "^0.5.2",
"@types/node": "^10.12.18",
"@types/node-emoji": "^1.8.0",
"@types/opn": "^5.1.0",
"@types/pg": "^7.4.11",
"@types/prettier": "^1.15.2",
"@types/shortid": "^0.0.29",
Expand All @@ -75,6 +76,7 @@
"lodash": "^4.17.11",
"mkdirp": "^0.5.1",
"node-emoji": "^1.8.1",
"opn": "^5.4.0",
"pg": "^7.7.1",
"reflect-metadata": "^0.1.12",
"shortid": "^2.2.14",
Expand Down
18 changes: 15 additions & 3 deletions src/core/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { Binding } from 'graphql-binding';
import { Server as HttpServer } from 'http';
import { Server as HttpsServer } from 'https';
import * as mkdirp from 'mkdirp';
import opn = require('opn');
import * as path from 'path';
import { AuthChecker, buildSchema, useContainer as TypeGraphQLUseContainer } from 'type-graphql'; // formatArgumentValidationError
import { Container } from 'typedi';
Expand All @@ -32,6 +33,7 @@ export interface AppOptions<T> {
host?: string;
generatedFolder?: string;
middlewares?: any[]; // TODO: fix
openPlayground?: boolean;
port?: string | number;
warthogImportPath?: string;
}
Expand All @@ -46,6 +48,7 @@ export class App<C extends BaseContext> {
graphQLServer!: ApolloServer;
httpServer!: HttpServer | HttpsServer;
logger: Logger;
openPlayground: boolean;
schema?: GraphQLSchema;

constructor(private appOptions: AppOptions<C>, private dbOptions: Partial<ConnectionOptions> = {}) {
Expand Down Expand Up @@ -77,6 +80,10 @@ export class App<C extends BaseContext> {
return {};
};
this.context = this.appOptions.context || returnEmpty;
this.openPlayground =
typeof this.appOptions.openPlayground !== 'undefined'
? this.appOptions.openPlayground
: !!(process.env.NODE_ENV === 'development');

this.createGeneratedFolder();
}
Expand Down Expand Up @@ -152,9 +159,14 @@ export class App<C extends BaseContext> {

this.graphQLServer.applyMiddleware({ app, path: '/graphql' });

this.httpServer = app.listen({ port: this.appPort }, () =>
this.logger.info(`🚀 Server ready at http://${this.appHost}:${this.appPort}${this.graphQLServer.graphqlPath}`)
);
const url = `http://${this.appHost}:${this.appPort}${this.graphQLServer.graphqlPath}`;

this.httpServer = app.listen({ port: this.appPort }, () => this.logger.info(`🚀 Server ready at ${url}`));

// Open playground in the browser
if (this.openPlayground) {
opn(url);
}

return this;
}
Expand Down
19 changes: 19 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,13 @@
resolved "https://registry.npmjs.org/@types/node/-/node-9.6.41.tgz#e57c3152eb2e7ec748c733cebd0c095b437c5d37"
integrity sha512-sPZWEbFMz6qAy9SLY7jh5cgepmsiwqUUHjvEm8lpU6kug2hmmcyuTnwhoGw/GWpI5Npue4EqvsiQQI0eWjW/ZA==

"@types/opn@^5.1.0":
version "5.1.0"
resolved "https://registry.npmjs.org/@types/opn/-/opn-5.1.0.tgz#bff7bc371677f4bdbb37884400e03fd81f743927"
integrity sha512-TNPrB7Y1xl06zDI0aGyqkgxjhIev3oJ+cdqlZ52MTAHauWpEL/gIUdHebIfRHFZk9IqSBpE2ci1DT48iZH81yg==
dependencies:
"@types/node" "*"

"@types/pg-types@*":
version "1.11.4"
resolved "https://registry.npmjs.org/@types/pg-types/-/pg-types-1.11.4.tgz#8d7c59fb509ce3dca3f8bae589252051c639a9a8"
Expand Down Expand Up @@ -3928,6 +3935,11 @@ is-windows@^1.0.2:
resolved "https://registry.npmjs.org/is-windows/-/is-windows-1.0.2.tgz#d1850eb9791ecd18e6182ce12a30f396634bb19d"
integrity sha512-eXK1UInq2bPmjyX6e3VHIzMLobc4J94i4AWn+Hpq3OU5KkrRC96OAcR3PRJ/pGu6m8TRnBHP9dkXQVsT/COVIA==

is-wsl@^1.1.0:
version "1.1.0"
resolved "https://registry.npmjs.org/is-wsl/-/is-wsl-1.1.0.tgz#1f16e4aa22b04d1336b66188a66af3c600c3a66d"
integrity sha1-HxbkqiKwTRM2tmGIpmrzxgDDpm0=

isarray@0.0.1:
version "0.0.1"
resolved "https://registry.npmjs.org/isarray/-/isarray-0.0.1.tgz#8a18acfca9a8f4177e09abfc6038939b05d1eedf"
Expand Down Expand Up @@ -5890,6 +5902,13 @@ opener@^1.5.1:
resolved "https://registry.npmjs.org/opener/-/opener-1.5.1.tgz#6d2f0e77f1a0af0032aca716c2c1fbb8e7e8abed"
integrity sha512-goYSy5c2UXE4Ra1xixabeVh1guIX/ZV/YokJksb6q2lubWu6UbvPQ20p542/sFIll1nl8JnCyK9oBaOcCWXwvA==

opn@^5.4.0:
version "5.4.0"
resolved "https://registry.npmjs.org/opn/-/opn-5.4.0.tgz#cb545e7aab78562beb11aa3bfabc7042e1761035"
integrity sha512-YF9MNdVy/0qvJvDtunAOzFw9iasOQHpVthTCvGzxt61Il64AYSGdK+rYwld7NAfk9qJ7dt+hymBNSc9LNYS+Sw==
dependencies:
is-wsl "^1.1.0"

optimist@^0.6.1:
version "0.6.1"
resolved "https://registry.npmjs.org/optimist/-/optimist-0.6.1.tgz#da3ea74686fa21a19a111c326e90eb15a0196686"
Expand Down

0 comments on commit c40787f

Please sign in to comment.