Skip to content

Commit

Permalink
GraphQL CRUD
Browse files Browse the repository at this point in the history
  • Loading branch information
TannerGabriel committed Jul 3, 2019
1 parent 411ce8c commit 3746c2d
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 8 deletions.
18 changes: 18 additions & 0 deletions nest-graph-ql/schema.gql
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,24 @@
# !!! DO NOT MODIFY THIS FILE BY YOURSELF !!!
# -----------------------------------------------

input ItemInput {
title: String!
price: Int!
description: String!
}

type ItemType {
id: ID!
title: String!
price: Int!
description: String!
}

type Mutation {
createItem(input: ItemInput!): ItemType!
}

type Query {
items: [ItemType!]!
hello: String!
}
10 changes: 9 additions & 1 deletion nest-graph-ql/src/items/dto/create-item.dto.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
export interface CreateItemDto {
import { ObjectType, Field, Int, ID } from 'type-graphql';

@ObjectType()
export class ItemType {
@Field(() => ID)
readonly id: string;
@Field()
readonly title: string;
@Field(() => Int)
readonly price: number;
@Field()
readonly description: string;
}
11 changes: 11 additions & 0 deletions nest-graph-ql/src/items/input-items.input.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { InputType, Field, Int } from 'type-graphql';

@InputType()
export class ItemInput {
@Field()
readonly title: string;
@Field(() => Int)
readonly price: number;
@Field()
readonly description: string;
}
22 changes: 17 additions & 5 deletions nest-graph-ql/src/items/items.resolver.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,23 @@
import { Resolver, Query } from '@nestjs/graphql';
import { Resolver, Query, Mutation, Args } from '@nestjs/graphql';
import { ItemsService } from './items.service';
import { ItemType } from './dto/create-item.dto';
import { ItemInput } from './input-items.input';

@Resolver()
export class ItemsResolver {
/* constructor(
private readonly authorsService: AuthorsService,
private readonly postsService: PostsService,
) {} */
constructor(
private readonly itemsService: ItemsService,
) {}

@Query(() => [ItemType])
async items() {
return this.itemsService.findAll();
}

@Mutation(() => ItemType)
async createItem(@Args('input') input: ItemInput) {
return this.itemsService.create(input);
}

@Query(() => String)
async hello() {
Expand Down
5 changes: 3 additions & 2 deletions nest-graph-ql/src/items/items.service.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import { Injectable } from '@nestjs/common';
import { InjectModel } from '@nestjs/mongoose';
import { Model } from 'mongoose';
import { CreateItemDto } from './dto/create-item.dto';
import { ItemType } from './dto/create-item.dto';
import { Item } from './interfaces/item.interface';
import { ItemInput } from './input-items.input';

@Injectable()
export class ItemsService {
constructor(@InjectModel('Item') private itemModel: Model<Item>) {}

async create(createItemDto: CreateItemDto): Promise<Item> {
async create(createItemDto: ItemInput): Promise<Item> {
const createdItem = new this.itemModel(createItemDto);
return await createdItem.save();
}
Expand Down

0 comments on commit 3746c2d

Please sign in to comment.