Skip to content

Commit

Permalink
Add @DefaultValue decorator
Browse files Browse the repository at this point in the history
  • Loading branch information
robak86 committed Jan 6, 2018
1 parent f7add96 commit 4e40f5a
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 16 deletions.
4 changes: 4 additions & 0 deletions lib/decorators/fields.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ export const params = (argsType:ArgsType):PropertyDecorator => createFieldDecora
fieldConfig.setParamsType(argsType)
});

export const defaultValue = (val):PropertyDecorator => createFieldDecorator(fieldConfig => {
fieldConfig.setDefaultValue(val)
});

export const paramsThunk = (argsType:() => ArgsType):PropertyDecorator => createFieldDecorator(fieldConfig => {
fieldConfig.setParamsThunk(argsType)
});
Expand Down
6 changes: 3 additions & 3 deletions spec/integration/buildingSchema.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ function createdSchemaFromDecoratedClasses():GraphQLSchema {

function createSchemaFromDefinition():GraphQLSchema {
const definition = `
"""User search address params"""
# User search address params
input UserSearchAddressParams {
street: String
}
Expand All @@ -28,7 +28,7 @@ function createSchemaFromDefinition():GraphQLSchema {
firstNameUpperCase: String
address: Address
employers: [Company!]!
role: UserRole!
roles: [UserRole!]!
}
type Address {
Expand All @@ -47,7 +47,7 @@ function createSchemaFromDefinition():GraphQLSchema {
}
type Mutation {
createUser(firstName: String!, lastName: String!, address: CreateAddressParams): User
createUser(firstName: String!, lastName: String!, address: CreateAddressParams, roles: [UserRole] = []): User
createAddress(streetName: String, city: String): Address
createCompany(companyName: String): Company
createCompanyWrapped(input: CreateCompanyParams!): Company
Expand Down
8 changes: 4 additions & 4 deletions spec/integration/queries.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ describe("root query", () => {
id
firstName
firstNameUpperCase
role
roles
employers {
id
name
Expand All @@ -33,7 +33,7 @@ describe("root query", () => {
id: '1',
firstName: 'Jane',
firstNameUpperCase: 'JANE',
role: 'admin',
roles: ['admin'],
employers: [
{
id: "1",
Expand All @@ -45,7 +45,7 @@ describe("root query", () => {
id: '2',
firstName: 'John',
firstNameUpperCase: 'JOHN',
role: 'stuff',
roles: ['stuff'],
employers: [
{
id: "2",
Expand All @@ -57,7 +57,7 @@ describe("root query", () => {
id: '3',
firstName: 'Adam',
firstNameUpperCase: 'ADAM',
role: 'guest',
roles: ['guest'],
employers: [
{
id: "3",
Expand Down
8 changes: 4 additions & 4 deletions spec/integration/resolvers/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {Company} from "../types/Company";
import {UserRole} from "../types/UserRole";

export type CompanyEntity = { id:string; name:string; employeesIds:string[], type:'company' };
export type UserEntity = { id:string; firstName:string; employersIds:string[], role:UserRole, type:'user' };
export type UserEntity = { id:string; firstName:string; employersIds:string[], roles:UserRole[], type:'user' };

export function getCompanyById(id:string):CompanyEntity {
return getAllCompanies().filter(company => company.id === id)[0];
Expand All @@ -18,9 +18,9 @@ export function getAllCompanies():CompanyEntity[] {

export function getAllUsers():UserEntity[] {
return [
{id: '1', firstName: 'Jane', role: UserRole.admin, employersIds: ['1'], type: 'user'},
{id: '2', firstName: 'John', role: UserRole.stuff, employersIds: ['2'], type: 'user'},
{id: '3', firstName: 'Adam', role: UserRole.guest, employersIds: ['3'], type: 'user'},
{id: '1', firstName: 'Jane', roles: [UserRole.admin], employersIds: ['1'], type: 'user'},
{id: '2', firstName: 'John', roles: [UserRole.stuff], employersIds: ['2'], type: 'user'},
{id: '3', firstName: 'Adam', roles: [UserRole.guest], employersIds: ['3'], type: 'user'},
]
}

Expand Down
8 changes: 6 additions & 2 deletions spec/integration/types/CreateUserParams.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {field, input, nonNull} from "../../../lib";
import {defaultValue, field, input, list, nonNull} from "../../../lib";
import {GraphQLString} from "graphql";
import {CreateAddressParams} from "./CreateAddressParams";
import {UserRole} from "./UserRole";

@input()
export class CreateUserParams {
Expand All @@ -11,5 +12,8 @@ export class CreateUserParams {
lastName:string;

@field(CreateAddressParams)
address:CreateAddressParams
address:CreateAddressParams;

@list(UserRole) @defaultValue([])
roles: UserRole[];
}
6 changes: 3 additions & 3 deletions spec/integration/types/User.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {Company} from "./Company";
import {field, id, listThunk, nonNull, nonNullItems, resolve} from "../../../lib/";
import {GraphQLString} from "graphql";
import {type} from "../../../lib";
import {list, type} from "../../../lib";
import {resolveEmployersForUser} from "../resolvers/queries";
import {UserRole} from "./UserRole";
import {Address} from "./Address";
Expand All @@ -25,6 +25,6 @@ export class User {
@resolve(resolveEmployersForUser)
employers:Company[];

@field(UserRole) @nonNull()
role:UserRole
@list(UserRole) @nonNull() @nonNullItems()
roles:UserRole[]
}

0 comments on commit 4e40f5a

Please sign in to comment.