Skip to content

Commit

Permalink
fix(float-field): should work with codegen (#151)
Browse files Browse the repository at this point in the history
  • Loading branch information
goldcaddy77 authored Jun 28, 2019
1 parent 223c13b commit f5c16bd
Show file tree
Hide file tree
Showing 27 changed files with 265 additions and 60 deletions.
4 changes: 4 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ jobs:
- run:
name: test
command: yarn jest --coverage --ci --forceExit --detectOpenHandles --runInBand
- run:
name: test
# TODO: This should be an actual Jest test, but I figured it gives more coverage and already have it
command: yarn test:seed
- run:
name: deploy
command: yarn run semantic-release || true
Expand Down
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -231,14 +231,14 @@ Notice how we've only added a single field on the model and you get pagination,

All config is driven by environment variables. Most options can also be set by setting the value when creating your `Server` instance.

| variable | value | config option name | default |
| ---------------- | ----------------------------- | ------------------ | ------- |
| APP_HOST | App server host | appOptions.host | _none_ |
| APP_PORT | App server port | appOptions.port | 4000 |
| TYPEORM_DATABASE | DB name | _none_ | _none_ |
| TYPEORM_USERNAME | DB username | _none_ | _none_ |
| TYPEORM_PASSWORD | DB password | _none_ | _none_ |
| MOCK_DATABASE | Should we use mock sqlite DB? | _none_ | false |
| variable | value | config option name | default |
| --------------------- | ----------------------------- | ------------------ | ------- |
| WARTHOG_APP_HOST | App server host | appOptions.host | _none_ |
| WARTHOG_APP_PORT | App server port | appOptions.port | 4000 |
| WARTHOG_DB_DATABASE | DB name | _none_ | _none_ |
| WARTHOG_DB_USERNAME | DB username | _none_ | _none_ |
| WARTHOG_DB_PASSWORD | DB password | _none_ | _none_ |
| WARTHOG_MOCK_DATABASE | Should we use mock sqlite DB? | _none_ | false |

## Field/Column Decorators

Expand Down
10 changes: 10 additions & 0 deletions examples/1-simple-model/generated/binding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ export const Binding = makeBindingClass<BindingConstructor<Binding>>({ schema })
* Types
*/

export type StringEnum = 'FOO' |
'BAR'

export type UserOrderByInput = 'createdAt_ASC' |
'createdAt_DESC' |
'updatedAt_ASC' |
Expand All @@ -56,6 +59,8 @@ export type UserOrderByInput = 'createdAt_ASC' |
'age_DESC' |
'isRequired_ASC' |
'isRequired_DESC' |
'stringEnumField_ASC' |
'stringEnumField_DESC' |
'rating_ASC' |
'rating_DESC'

Expand Down Expand Up @@ -89,6 +94,7 @@ export interface UserCreateInput {
email: String
age: Float
isRequired: Boolean
stringEnumField: StringEnum
rating: Float
}

Expand All @@ -98,6 +104,7 @@ export interface UserUpdateInput {
email?: String | null
age?: Float | null
isRequired?: Boolean | null
stringEnumField?: StringEnum | null
rating?: Float | null
}

Expand Down Expand Up @@ -146,6 +153,8 @@ export interface UserWhereInput {
age_in?: Int[] | Int | null
isRequired_eq?: Boolean | null
isRequired_in?: Boolean[] | Boolean | null
stringEnumField_eq?: StringEnum | null
stringEnumField_in?: StringEnum[] | StringEnum | null
rating_eq?: Float | null
rating_gt?: Float | null
rating_gte?: Float | null
Expand Down Expand Up @@ -214,6 +223,7 @@ export interface User extends BaseGraphQLObject {
email: String
age: Int
isRequired: Boolean
stringEnumField: StringEnum
rating: Float
}

Expand Down
16 changes: 16 additions & 0 deletions examples/1-simple-model/generated/classes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { registerEnumType } from "type-graphql";
const { GraphQLJSONObject } = require("graphql-type-json");

import { BaseWhereInput, PaginationArgs } from "../../../src";
import { StringEnum } from "../src/user.model";
import { User } from "../src/user.model";

export enum UserOrderByEnum {
Expand Down Expand Up @@ -42,6 +43,9 @@ export enum UserOrderByEnum {
isRequired_ASC = "isRequired_ASC",
isRequired_DESC = "isRequired_DESC",

stringEnumField_ASC = "stringEnumField_ASC",
stringEnumField_DESC = "stringEnumField_DESC",

rating_ASC = "rating_ASC",
rating_DESC = "rating_DESC"
}
Expand Down Expand Up @@ -121,6 +125,12 @@ export class UserWhereInput extends BaseWhereInput {
@TypeGraphQLField(() => [Boolean], { nullable: true })
isRequired_in?: Boolean[];

@TypeGraphQLField(() => StringEnum, { nullable: true })
stringEnumField_eq?: StringEnum;

@TypeGraphQLField(() => [StringEnum], { nullable: true })
stringEnumField_in?: StringEnum[];

@TypeGraphQLField({ nullable: true })
rating_eq?: number;

Expand Down Expand Up @@ -166,6 +176,9 @@ export class UserCreateInput {
@TypeGraphQLField()
isRequired!: boolean;

@TypeGraphQLField(() => StringEnum)
stringEnumField!: StringEnum;

@TypeGraphQLField()
rating!: number;
}
Expand All @@ -187,6 +200,9 @@ export class UserUpdateInput {
@TypeGraphQLField({ nullable: true })
isRequired?: boolean;

@TypeGraphQLField(() => StringEnum, { nullable: true })
stringEnumField?: StringEnum;

@TypeGraphQLField({ nullable: true })
rating?: number;
}
Expand Down
12 changes: 12 additions & 0 deletions examples/1-simple-model/generated/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@ type StandardDeleteResponse {
id: ID!
}

enum StringEnum {
FOO
BAR
}

type User implements BaseGraphQLObject {
id: ID!
createdAt: DateTime!
Expand All @@ -93,6 +98,7 @@ type User implements BaseGraphQLObject {
email: String!
age: Int!
isRequired: Boolean!
stringEnumField: StringEnum!
rating: Float!
}

Expand All @@ -102,6 +108,7 @@ input UserCreateInput {
email: String!
age: Float!
isRequired: Boolean!
stringEnumField: StringEnum!
rating: Float!
}

Expand All @@ -122,6 +129,8 @@ enum UserOrderByInput {
age_DESC
isRequired_ASC
isRequired_DESC
stringEnumField_ASC
stringEnumField_DESC
rating_ASC
rating_DESC
}
Expand All @@ -132,6 +141,7 @@ input UserUpdateInput {
email: String
age: Float
isRequired: Boolean
stringEnumField: StringEnum
rating: Float
}

Expand Down Expand Up @@ -180,6 +190,8 @@ input UserWhereInput {
age_in: [Int!]
isRequired_eq: Boolean
isRequired_in: [Boolean!]
stringEnumField_eq: StringEnum
stringEnumField_in: [StringEnum!]
rating_eq: Float
rating_gt: Float
rating_gte: Float
Expand Down
3 changes: 3 additions & 0 deletions examples/1-simple-model/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@
"codegen": "warthog codegen",
"db:create": "warthog db:create",
"db:drop": "warthog db:drop",
"db:seed:dev": "dotenv -- ts-node tools/seed.ts",
"playground:open": "open http://localhost:$(dotenv -p APP_PORT)/graphql",
"start": "yarn start:ts",
"start:ts": "DEBUG=warthog* ts-node-dev --type-check src/index.ts",
"start:prod": "ts-node src/index.ts",
"//": "Allows us to use the local warthog CLI in commands above",
"warthog": "../../bin/warthog",
"watch:ts": "nodemon -e ts,graphql -x ts-node --type-check src/index.ts"
},
"dependencies": {
Expand Down
11 changes: 11 additions & 0 deletions examples/1-simple-model/src/user.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,20 @@ import {
BaseModel,
BooleanField,
EmailField,
EnumField,
FloatField,
IntField,
Model,
StringField
} from '../../../src';

// Note: this must be exported and in the same file where it's attached with @EnumField
// Also - must use string enums
export enum StringEnum {
FOO = 'FOO',
BAR = 'BAR'
}

@Model()
export class User extends BaseModel {
@StringField()
Expand All @@ -25,6 +33,9 @@ export class User extends BaseModel {
@BooleanField()
isRequired?: boolean;

@EnumField('StringEnum', StringEnum)
stringEnumField: StringEnum;

@FloatField()
rating?: number;
}
70 changes: 70 additions & 0 deletions examples/1-simple-model/tools/seed.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import * as Faker from 'faker';

import { getBindingError, logger } from '../../../src';

import { getServer } from '../src/server';

if (process.env.NODE_ENV !== 'development') {
throw 'Seeding only available in development environment';
}

const NUM_USERS = 10;

async function seedDatabase() {
const server = getServer({ openPlayground: false });
await server.start();

let binding;
try {
binding = await server.getBinding();
} catch (error) {
logger.error(error);
return process.exit(1);
}

for (let index = 0; index < NUM_USERS; index++) {
const random = new Date()
.getTime()
.toString()
.substring(8, 13);
const firstName = Faker.name.firstName();
const lastName = Faker.name.lastName();
const email = `${firstName
.substr(0, 1)
.toLowerCase()}${lastName.toLowerCase()}-${random}@fakeemail.com`;

try {
const user = await binding.mutation.createUser(
{
data: {
age: Faker.random.number({ min: 10, max: 100 }),
email,
firstName,
isRequired: Faker.random.arrayElement([true, false]),
lastName,
rating: Faker.random.number({ min: 1, max: 5 }),
stringEnumField: Faker.random.arrayElement(['FOO', 'BAR'])
}
},
`{ id email createdAt createdById }`
);
logger.info(user.email);
} catch (err) {
const error = getBindingError(err);
logger.error(email);
logger.error(error);
}
}

return server.stop();
}

seedDatabase()
.then(result => {
logger.info(result);
return process.exit(0);
})
.catch(err => {
logger.error(err);
return process.exit(1);
});
2 changes: 2 additions & 0 deletions examples/2-complex-example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
"start:ts": "ts-node --type-check src/index.ts",
"test": "dotenv -- jest --detectOpenHandles --verbose --coverage",
"test:watch": "dotenv -- jest --watch",
"//": "Allows us to use the local warthog CLI in commands above",
"warthog": "../../bin/warthog",
"watch:ts": "nodemon -e ts,graphql -x ts-node --type-check src/index.ts"
},
"dependencies": {
Expand Down
3 changes: 2 additions & 1 deletion examples/2-complex-example/src/server.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import 'reflect-metadata';

import { BaseContext, Server } from '../../../src';
import { authChecker, BaseContext, Server } from '../../../src/';

import { customLogger } from './logger';

Expand All @@ -19,6 +19,7 @@ function sleep(ms: number) {
export function getServer(AppOptions = {}, dbOptions = {}) {
return new Server<Context>(
{
authChecker,
// Inject a fake user. In a real app you'd parse a JWT to add the user
context: async () => {
// allows asynchronous resolution of user (or other items you want to put in context)
Expand Down
2 changes: 2 additions & 0 deletions examples/3-one-to-many-relationship/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
"playground:open": "open http://localhost:$(dotenv -p APP_PORT)/graphql",
"start": "yarn start:ts",
"start:ts": "ts-node --type-check src/index.ts",
"//": "Allows us to use the local warthog CLI in commands above",
"warthog": "../../bin/warthog",
"watch:ts": "nodemon -e ts,graphql -x ts-node --type-check src/index.ts"
},
"dependencies": {
Expand Down
2 changes: 2 additions & 0 deletions examples/4-many-to-many-relationship/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
"playground:open": "open http://localhost:$(dotenv -p APP_PORT)/graphql",
"start": "yarn start:ts",
"start:ts": "ts-node --type-check src/index.ts",
"//": "Allows us to use the local warthog CLI in commands above",
"warthog": "../../bin/warthog",
"watch:ts": "nodemon -e ts,graphql -x ts-node --type-check src/index.ts"
},
"dependencies": {
Expand Down
8 changes: 5 additions & 3 deletions examples/5-migrations/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,16 @@
"codegen": "warthog codegen",
"db:create": "warthog db:create",
"db:drop": "warthog db:drop",
"db:migration:generate": "yarn typeorm:cli migration:generate -n migration1",
"db:migration:run": "yarn typeorm:cli migration:run",
"db:query": "yarn typeorm:cli query 'select * from user;'",
"playground:open": "open http://localhost:$(dotenv -p APP_PORT)/graphql",
"start": "yarn start:ts",
"start:ts": "DEBUG=warthog* ts-node-dev --type-check src/index.ts",
"start:prod": "ts-node src/index.ts",
"typeorm:cli": "ts-node ./node_modules/.bin/typeorm -f ./generated/ormconfig.ts",
"db:query": "yarn typeorm:cli query 'select * from user;'",
"db:migration:generate": "yarn typeorm:cli migration:generate -n migration1",
"db:migration:run": "yarn typeorm:cli migration:run",
"//": "Allows us to use the local warthog CLI in commands above",
"warthog": "../../bin/warthog",
"watch:ts": "nodemon -e ts,graphql -x ts-node --type-check src/index.ts"
},
"dependencies": {
Expand Down
4 changes: 3 additions & 1 deletion examples/6-base-service/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "example2",
"name": "example6",
"version": "0.0.0",
"license": "MIT",
"scripts": {
Expand All @@ -14,6 +14,8 @@
"start:ts": "ts-node --type-check src/index.ts",
"test": "dotenv -- jest --detectOpenHandles --verbose --coverage",
"test:watch": "dotenv -- jest --watch",
"//": "Allows us to use the local warthog CLI in commands above",
"warthog": "../../bin/warthog",
"watch:ts": "nodemon -e ts,graphql -x ts-node --type-check src/index.ts"
},
"dependencies": {
Expand Down
Loading

0 comments on commit f5c16bd

Please sign in to comment.