GraphQL API 만들기~

hwakyungChoi·2021년 1월 27일
0
post-custom-banner
//restaurant.entity.ts
import { Field, ObjectType } from "@nestjs/graphql";

// our sample API needs to be able to fetch a list of authors and their posts, so we should define the Author type and Post type to support this functionality.

// If we were using the schema first approach, we'd define such a schema with SDL like this:


@ObjectType()
export class Restaurant {
    @Field(type => String)
    name: string;
    @Field(type => Boolean)
    isVegan: boolean;
    @Field(type => String)
    address: string;
    @Field(type => String)
    ownersName: string;

}
//create-restaurant.dto.ts
import { ArgsType, Field } from "@nestjs/graphql";
import { IsBoolean, IsString, Length } from "class-validator"

//https://aerocode.net/364?category=787107 argstype 정리
@ArgsType()
export class CreateRestaurantDto {
    @Field(type => String)
    @IsString()
    @Length(5, 10)
    name: string;

    @Field(type => Boolean)
    @IsBoolean()
    isVegan: boolean;

    @Field(type => String)
    @IsString()
    address: string;

    @Field(type => String)
    @IsString()
    ownersName: string;
}

// restaurants.resolver.ts
import { Args, Mutation, Query, Resolver } from "@nestjs/graphql";
import { CreateRestaurantDto } from "./dtos/create-restaurant.dto";
import { Restaurant } from "./entities/restaurant.entity";
//graphql 에서  인자값을 지정할 수 있게 함
@Resolver(of => Restaurant)
export class RestaurantResolver {
    @Query(returns => [Restaurant])
    restaurants(@Args("veganOnly") veganOnly: boolean): Restaurant[] {
        return [];
    }

    @Mutation(returns => Boolean)
    createRestaurant(@Args() createRestaurantDto: CreateRestaurantDto): boolean {

        return true;
    }
}
post-custom-banner

0개의 댓글