Skip to content

Commit

Permalink
feat(create-many): adds create many functionality (#25)
Browse files Browse the repository at this point in the history
  • Loading branch information
goldcaddy77 authored Jan 15, 2019
1 parent 54424fb commit 91f60d3
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 6 deletions.
7 changes: 6 additions & 1 deletion src/core/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,12 @@ export class App {
initialized: false,
loaders: {}
},
request: options.request
request: options.request,
user: {
email: 'admin@test.com',
id: 'abc12345',
permissions: ['user:read', 'user:update', 'user:create', 'user:delete', 'photo:delete']
}
};
return { ...context, ...(options.context || {}) };
},
Expand Down
5 changes: 0 additions & 5 deletions src/core/binding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import { Binding, TypescriptGenerator } from 'graphql-binding';
import { introspectSchema, makeRemoteExecutableSchema } from 'graphql-tools';
import * as path from 'path';

// require('ts-node').register();

import { StringMapOptional } from '..';

const debug = Debug('binding');
Expand Down Expand Up @@ -59,9 +57,6 @@ export async function getRemoteBinding(endpoint: string, options: LinkOptions) {
if (!endpoint) {
throw new Error('getRemoteBinding: endpoint is required');
}
if (!options.token) {
throw new Error('getRemoteBinding: token is required');
}

debug('getRemoteBinding', endpoint, options);

Expand Down
2 changes: 2 additions & 0 deletions src/schema/SchemaGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
entityListToImports,
entityToOrderByEnum,
entityToWhereArgs,
entityToCreateManyArgs,
entityToWhereInput,
entityToWhereUniqueInput,
entityToCreateInput,
Expand Down Expand Up @@ -36,6 +37,7 @@ export class SchemaGenerator {
${entityToOrderByEnum(entity)}
${entityToWhereInput(entity)}
${entityToWhereArgs(entity)}
${entityToCreateManyArgs(entity)}
${entityToWhereUniqueInput(entity)}
${entityToCreateInput(entity)}
${entityToUpdateInput(entity)}
Expand Down
12 changes: 12 additions & 0 deletions src/schema/TypeORMConverter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,18 @@ export function entityToWhereArgs(entity: EntityMetadata): string {
`;
}

// Note: it would be great to inject a single `Arg` with the [entity.nameCreateInput] array arg,
// but that is not allowed by TypeGraphQL
export function entityToCreateManyArgs(entity: EntityMetadata): string {
return `
@ArgsType()
export class ${entity.name}CreateManyArgs {
@Field(type => [${entity.name}CreateInput])
data: ${entity.name}CreateInput[];
}
`;
}

export function entityToOrderByEnum(entity: EntityMetadata): string {
const ORDER_BY_BLACKLIST = ['createdById', 'updatedById', 'deletedById'];

Expand Down
22 changes: 22 additions & 0 deletions src/tgql/BaseResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,28 @@ export class BaseResolver<E extends BaseModel> {
return this.repository.save(obj as any, { reload: true });
}

async createMany(data: Array<DeepPartial<E>>, userId: string): Promise<E[]> {
data = data.map(item => {
return { ...item, createdById: userId };
});

const results = this.repository.create(data);

// Validate against the the data model
// Without `skipMissingProperties`, some of the class-validator validations (like MinLength)
// will fail if you don't specify the property
results.forEach(async obj => {
const errors = await validate(obj, { skipMissingProperties: true });
if (errors.length) {
// TODO: create our own error format that matches Mike B's format
throw new ArgumentValidationError(errors);
}
});

// TODO: remove any when this is fixed: https://github.com/Microsoft/TypeScript/issues/21592
return this.repository.save(results as any, { reload: true });
}

// TODO: There must be a more succinct way to:
// - Test the item exists
// - Update
Expand Down

0 comments on commit 91f60d3

Please sign in to comment.