Skip to content

Instantly share code, notes, and snippets.

@jtushman
Created July 1, 2019 19:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jtushman/e30fc4166cd2e6fcefefd958c8c2e414 to your computer and use it in GitHub Desktop.
Save jtushman/e30fc4166cd2e6fcefefd958c8c2e414 to your computer and use it in GitHub Desktop.
// index.ts
import "reflect-metadata";
import { ApolloServer } from 'apollo-server'
import {AccountResolver} from "./modules/account.resolver";
import * as path from "path";
import {buildSchema} from "type-graphql";
import {createConnection, useContainer} from "typeorm";
import {Container} from "typedi";
async function bootstrap() {
useContainer(Container);
await createConnection().then(
() => {console.log('yay that worked')},
( reason) => { console.log('nope', reason)}
)
const schema = await buildSchema({
resolvers: [AccountResolver],
emitSchemaFile: path.resolve(__dirname, "schema.gql"),
})
// Create GraphQL server
const server = new ApolloServer({
schema,
// enable GraphQL Playground
playground: true,
});
// Start the server
const { url } = await server.listen(4000);
console.log(`Server is running, GraphQL Playground available at ${url}`);
}
bootstrap();
// account.entity.ts
import {
Column,
CreateDateColumn,
Entity,
PrimaryGeneratedColumn,
UpdateDateColumn,
} from 'typeorm';
import {Field, ID, ObjectType} from "type-graphql";
@ObjectType()
@Entity()
export class Account {
@Field(() => ID)
@PrimaryGeneratedColumn('uuid')
id: string;
@Field()
@Column()
name: string;
@Field({nullable: true})
@Column({nullable: true})
description: string;
@Field()
@CreateDateColumn()
createdAt: Date;
@Field()
@UpdateDateColumn()
updatedAt: Date;
}
// AccountResolver.ts
import {ArgsType, Field, InputType, Int, Mutation, Query, Resolver} from "type-graphql";
import {Account} from "./account.entity";
import {Arg} from "type-graphql/dist/decorators/Arg";
import {MaxLength, Min, Max, MinLength} from "class-validator";
import {Args} from "type-graphql/dist/decorators/Args";
import {InjectRepository} from "typeorm-typedi-extensions";
import {Repository} from "typeorm";
import {Service} from "typedi/decorators/Service";
@InputType()
class NewAccountInput {
@Field()
@MinLength(3)
@MaxLength(30)
name: string;
@Field()
@MaxLength(200)
description: string;
}
@ArgsType()
class AccountArgs {
@Field(() => Int)
@Min(0)
skip: number = 0;
@Field(() => Int)
@Min(1)
@Max(50)
take: number = 25;
}
@Resolver(() => Account)
@Service()
export class AccountResolver {
//
// @InjectRepository(Account)
// private readonly accountRepo: Repository<Account>
constructor(
@InjectRepository(Account)
private readonly accountRepo: Repository<Account>
) {}
@Query(() => Account)
async account(@Arg("id") id: string) {
return this.accountRepo.findOneOrFail(id);
}
@Query(() => [Account])
async accounts(@Args() {skip, take}: AccountArgs) {
return this.accountRepo.find({
skip, take
})
}
@Mutation(() => Account)
async createAccount(
@Arg("newAccountData") newAccountData: NewAccountInput
): Promise<Account> {
console.log('TUNA')
console.log(JSON.stringify(this.accountRepo))
return this.accountRepo.create(newAccountData)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment