Skip to content

Commit

Permalink
chore(examples): clean up examples (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
goldcaddy77 authored Jan 12, 2019
1 parent 7ec77b7 commit 1575329
Show file tree
Hide file tree
Showing 28 changed files with 4,357 additions and 1 deletion.
File renamed without changes.
File renamed without changes.
File renamed without changes.
24 changes: 24 additions & 0 deletions examples/1-simple-model/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Example 1 - User

## Setup

Run `yarn bootstrap && yarn start`

## Bootstrapping the App

Running `yarn bootstrap` will do the following:

- Install packages
- Create the example DB
- Seed the database with test data

## Running the App

To run the project, run `yarn start`. This will:

- Run the API server
- Open GraphQL Playground

## Example Queries/Mutations

You can find some examples in [examples.gql](./examples.gql)
File renamed without changes.
File renamed without changes.
45 changes: 45 additions & 0 deletions examples/1-simple-model/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
{
"name": "example1",
"version": "0.0.0",
"scripts": {
"//": "make sure to install top-level packages, too",
"bootstrap": "cd ../.. && yarn && cd - && yarn && yarn db:create",
"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:ts": "ts-node --type-check src/index.ts"
},
"dependencies": {
"pgtools": "^0.3.0",
"reflect-metadata": "^0.1.12",
"typescript": "^3.2.2"
},
"devDependencies": {
"@types/express": "^4.16.0",
"@types/faker": "^4.1.4",
"@types/isomorphic-fetch": "^0.0.34",
"@types/jest": "^23.3.11",
"@types/node": "^10.12.18",
"dotenv-cli": "^1.4.0",
"faker": "^4.1.0",
"jest": "^23.6.0",
"npm-run-all": "^4.1.5",
"ts-jest": "^23.10.5",
"ts-node": "^7.0.1"
},
"jest": {
"transform": {
".ts": "ts-jest"
},
"testRegex": "\\.test\\.ts$",
"moduleFileExtensions": [
"ts",
"js"
],
"coveragePathIgnorePatterns": [
"/node_modules/",
"\\.test\\.ts$"
]
}
}
16 changes: 16 additions & 0 deletions examples/1-simple-model/src/examples.gql
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
query {
users(orderBy: createdAt_DESC) {
id
lastName
createdAt
}
}

mutation {
createUser(data: { firstName: "Test", email: "test@fakeemail.com" }) {
id
firstName
lastName
createdAt
}
}
31 changes: 31 additions & 0 deletions examples/1-simple-model/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import 'reflect-metadata';
import * as dotenv from 'dotenv';
import { Container } from 'typedi';

dotenv.config();

import { App } from '../../../src/';

async function bootstrap() {
const app = new App(
// Path written in generated classes
{
container: Container,
warthogImportPath: '../../../src'
},
{
cache: true,
synchronize: true
}
);

await app.start();
}

bootstrap().catch((error: Error) => {
console.error(error);
if (error.stack) {
console.error(error.stack!.split('\n'));
}
process.exit(1);
});
13 changes: 13 additions & 0 deletions examples/1-simple-model/src/user.entity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { BaseModel, EmailField, Model, StringField } from '../../../src';

@Model()
export class User extends BaseModel {
@StringField()
firstName?: string;

@StringField({ nullable: true })
lastName?: string;

@EmailField()
email?: string;
}
47 changes: 47 additions & 0 deletions examples/1-simple-model/src/user.resolver.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { GraphQLResolveInfo } from 'graphql';
import { Arg, Args, Ctx, Mutation, Query, Resolver } from 'type-graphql';
import { Repository } from 'typeorm';
import { InjectRepository } from 'typeorm-typedi-extensions';

import { BaseResolver, Context, StandardDeleteResponse } from '../../../src';
import { UserCreateInput, UserUpdateArgs, UserWhereArgs, UserWhereInput, UserWhereUniqueInput } from '../generated';

import { User } from './user.entity';

// Note: we have to specify `User` here instead of (of => User) because for some reason this
// changes the object reference when it's trying to add the FieldResolver and things break
@Resolver(User)
export class UserResolver extends BaseResolver<User> {
constructor(@InjectRepository(User) private readonly userRepository: Repository<User>) {
super(User, userRepository);
}

@Query(returns => [User])
async users(
@Args() { where, orderBy, limit, offset }: UserWhereArgs,
@Ctx() ctx: Context,
info: GraphQLResolveInfo
): Promise<User[]> {
return this.find<UserWhereInput>(where, orderBy, limit, offset);
}

@Query(returns => User)
async user(@Arg('where') where: UserWhereUniqueInput): Promise<User> {
return this.findOne<UserWhereUniqueInput>(where);
}

@Mutation(returns => User)
async createUser(@Arg('data') data: UserCreateInput, @Ctx() ctx: Context): Promise<User> {
return this.create(data, ctx.user.id);
}

@Mutation(returns => User)
async updateUser(@Args() { data, where }: UserUpdateArgs, @Ctx() ctx: Context): Promise<User> {
return this.update(data, where, ctx.user.id);
}

@Mutation(returns => StandardDeleteResponse)
async deleteUser(@Arg('where') where: UserWhereUniqueInput, @Ctx() ctx: Context): Promise<StandardDeleteResponse> {
return this.delete(where, ctx.user.id);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"noImplicitAny": true,
"noImplicitReturns": true,
"noImplicitThis": true,
"noUnusedLocals": true,
"noUnusedLocals": false, // Avoid noise in our resolvers when we use DI
"strict": true,
"strictNullChecks": true,
"types": ["jest", "isomorphic-fetch", "node"]
Expand Down
File renamed without changes.
5 changes: 5 additions & 0 deletions examples/2-complex-example/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
APP_HOST=localhost
APP_PORT=4100
TYPEORM_DATABASE=example1
TYPEORM_USERNAME=postgres
TYPEORM_PASSWORD=
3 changes: 3 additions & 0 deletions examples/2-complex-example/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
generated/*
!generated/index.ts
!generated/schema.ts
14 changes: 14 additions & 0 deletions examples/2-complex-example/.graphqlconfig.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
schemaPath: generated/schema.graphql
extensions:
endpoints:
default:
url: http://${env:APP_HOST}/graphql
headers:
Authorization: "Bearer ${env:GITHUB_TOKEN}"
codegen:
generator: graphql-binding
language: typescript
input:
schema: generated/schema.js
output:
binding: generated/generated-binding.ts
File renamed without changes.
1 change: 1 addition & 0 deletions examples/2-complex-example/generated/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './classes';
12 changes: 12 additions & 0 deletions examples/2-complex-example/generated/schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import * as fs from 'fs';
import { makeExecutableSchema } from 'graphql-tools';
import { GraphQLSchema } from 'graphql';

const schema: GraphQLSchema = makeExecutableSchema({
typeDefs: fs.readFileSync(__dirname + '/schema.graphql', 'utf-8'),
resolverValidationOptions: {
requireResolversForResolveType: false
}
});

export default schema;
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
23 changes: 23 additions & 0 deletions examples/2-complex-example/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"compilerOptions": {
"allowSyntheticDefaultImports": true,
"baseUrl": "src",
"lib": ["dom", "es5", "es6", "es7", "esnext", "esnext.asynciterable"],
"target": "es5",
"module": "commonjs",
"moduleResolution": "node",
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"sourceMap": true,
"keyofStringsOnly": true,
"noImplicitAny": true,
"noImplicitReturns": true,
"noImplicitThis": true,
"noUnusedLocals": false, // Avoid noise in our resolvers when we use DI
"strict": true,
"strictNullChecks": true,
"types": ["jest", "isomorphic-fetch", "node"]
},
"include": ["src/**/*"],
"exclude": ["node_modules/**/*"]
}
Loading

0 comments on commit 1575329

Please sign in to comment.