Skip to content

Commit

Permalink
chore: Update to Vendure v0.16.0
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelbromley committed Oct 9, 2020
1 parent da5c990 commit 386faa2
Show file tree
Hide file tree
Showing 5 changed files with 3,606 additions and 3,793 deletions.
16 changes: 8 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,13 @@
"@types/jest": "^26.0.14",
"@typescript-eslint/eslint-plugin": "^4.4.0",
"@typescript-eslint/parser": "^4.4.0",
"@vendure/admin-ui": "^0.14.0",
"@vendure/admin-ui-plugin": "^0.14.0",
"@vendure/asset-server-plugin": "^0.14.0",
"@vendure/core": "^0.14.0",
"@vendure/create": "^0.14.0",
"@vendure/testing": "^0.14.0",
"@vendure/ui-devkit": "^0.14.0",
"@vendure/admin-ui": "^0.16.0",
"@vendure/admin-ui-plugin": "^0.16.0",
"@vendure/asset-server-plugin": "^0.16.0",
"@vendure/core": "^0.16.0",
"@vendure/create": "^0.16.0",
"@vendure/testing": "^0.16.0",
"@vendure/ui-devkit": "^0.16.0",
"concurrently": "^5.3.0",
"eslint": "^7.10.0",
"eslint-config-prettier": "^6.12.0",
Expand All @@ -55,6 +55,6 @@
"rimraf": "^3.0.2",
"ts-jest": "^26.4.1",
"ts-node": "^9.0.0",
"typescript": "3.8.3"
"typescript": "4.0.3"
}
}
15 changes: 10 additions & 5 deletions src/api/example-admin.resolver.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Args, Mutation, Resolver } from '@nestjs/graphql';
import { Permission, Allow } from '@vendure/core';
import { Allow, Ctx, Permission, RequestContext, Transaction } from '@vendure/core';

import { ExampleService } from '../service/example.service';
import { ExampleEntity } from '../entities/example.entity';
Expand All @@ -9,15 +9,20 @@ import { MutationAddExampleArgs, MutationUpdateExampleArgs } from '../generated-
export class ExampleAdminResolver {
constructor(private exampleService: ExampleService) {}

@Transaction()
@Mutation()
@Allow(Permission.SuperAdmin)
addExample(@Args() args: MutationAddExampleArgs): Promise<ExampleEntity> {
return this.exampleService.addItem(args.input);
addExample(@Ctx() ctx: RequestContext, @Args() args: MutationAddExampleArgs): Promise<ExampleEntity> {
return this.exampleService.addItem(ctx, args.input);
}

@Transaction()
@Mutation()
@Allow(Permission.SuperAdmin)
updateExample(@Args() args: MutationUpdateExampleArgs): Promise<ExampleEntity | undefined> {
return this.exampleService.updateItem(args.input);
updateExample(
@Ctx() ctx: RequestContext,
@Args() args: MutationUpdateExampleArgs,
): Promise<ExampleEntity | undefined> {
return this.exampleService.updateItem(ctx, args.input);
}
}
13 changes: 8 additions & 5 deletions src/api/example.resolver.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Args, Query, Resolver } from '@nestjs/graphql';
import { PaginatedList } from '@vendure/core';
import { Ctx, PaginatedList, RequestContext } from '@vendure/core';

import { ExampleService } from '../service/example.service';
import { ExampleEntity } from '../entities/example.entity';
Expand All @@ -10,12 +10,15 @@ export class ExampleResolver {
constructor(private exampleService: ExampleService) {}

@Query()
examples(@Args() args: QueryExamplesArgs): Promise<PaginatedList<ExampleEntity>> {
return this.exampleService.getAllItems(args.options || undefined);
examples(
@Ctx() ctx: RequestContext,
@Args() args: QueryExamplesArgs,
): Promise<PaginatedList<ExampleEntity>> {
return this.exampleService.getAllItems(ctx, args.options || undefined);
}

@Query()
example(@Args() args: { id: string }): Promise<ExampleEntity | undefined> {
return this.exampleService.getItem(args.id);
example(@Ctx() ctx: RequestContext, @Args() args: { id: string }): Promise<ExampleEntity | undefined> {
return this.exampleService.getItem(ctx, args.id);
}
}
29 changes: 15 additions & 14 deletions src/service/example.service.ts
Original file line number Diff line number Diff line change
@@ -1,45 +1,46 @@
import { Injectable, Inject } from '@nestjs/common';
import { InjectConnection } from '@nestjs/typeorm';
import { ListQueryBuilder, PaginatedList } from '@vendure/core';
import { Connection } from 'typeorm';
import { Inject, Injectable } from '@nestjs/common';
import { ListQueryBuilder, PaginatedList, RequestContext, TransactionalConnection } from '@vendure/core';

import { ExampleEntity } from '../entities/example.entity';
import { PLUGIN_INIT_OPTIONS } from '../constants';
import { PluginInitOptions } from '../types';
import { ExampleListOptions, CreateExampleInput, UpdateExampleInput } from '../generated-admin-types';
import { CreateExampleInput, ExampleListOptions, UpdateExampleInput } from '../generated-admin-types';

@Injectable()
export class ExampleService {
constructor(
@InjectConnection() private connection: Connection,
private connection: TransactionalConnection,
@Inject(PLUGIN_INIT_OPTIONS) private options: PluginInitOptions,
private listQueryBuilder: ListQueryBuilder,
) {}

async getAllItems(options?: ExampleListOptions): Promise<PaginatedList<ExampleEntity>> {
async getAllItems(
ctx: RequestContext,
options?: ExampleListOptions,
): Promise<PaginatedList<ExampleEntity>> {
return this.listQueryBuilder
.build(ExampleEntity, options)
.build(ExampleEntity, options, { ctx })
.getManyAndCount()
.then(([items, totalItems]) => ({
items,
totalItems,
}));
}

async getItem(id: string): Promise<ExampleEntity | undefined> {
return this.connection.getRepository(ExampleEntity).findOne(id);
async getItem(ctx: RequestContext, id: string): Promise<ExampleEntity | undefined> {
return this.connection.getRepository(ctx, ExampleEntity).findOne(id);
}

async addItem(args: CreateExampleInput): Promise<ExampleEntity> {
const repository = this.connection.getRepository(ExampleEntity);
async addItem(ctx: RequestContext, args: CreateExampleInput): Promise<ExampleEntity> {
const repository = this.connection.getRepository(ctx, ExampleEntity);
const example = repository.create({ name: args.name });
await repository.save(example);

return example;
}

async updateItem(args: UpdateExampleInput): Promise<ExampleEntity | undefined> {
const repository = this.connection.getRepository(ExampleEntity);
async updateItem(ctx: RequestContext, args: UpdateExampleInput): Promise<ExampleEntity | undefined> {
const repository = this.connection.getRepository(ctx, ExampleEntity);
const example = await repository.findOne({ id: args.id });

if (!example) return;
Expand Down
Loading

0 comments on commit 386faa2

Please sign in to comment.