GraphQL API 만들기~ :)

hwakyungChoi·2021년 1월 26일
0
post-custom-banner
import { Module } from '@nestjs/common';
import { GraphQLModule } from "@nestjs/graphql"
import { RestaurantsModule } from "./restaurants/restaurants.module"
// main.tsfh import 되는 유일한 모듈 app 모듈임
//entity란 데이터베이스 안의 모델
@Module({

  imports: [
    RestaurantsModule,
    GraphQLModule.forRoot({
      autoSchemaFile: true,
    }),

  ],
  controllers: [],
  providers: [],
})
export class AppModule { }

// restaurants.module.ts

import { Module } from "@nestjs/common";
import { RestaurantResolver } from "./restaurants.resolver";

@Module({
    providers: [RestaurantResolver]
})

export class RestaurantsModule { }

//restaurants.resolver.ts
import { Query, Resolver } from "@nestjs/graphql";
import { Restaurant } from "./entities/restaurant.entity"

@Resolver()
export class RestaurantResolver {
    @Query(returns => Restaurant)
    Restaurant() {
        return true;
    }
}

//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, { nullable: true })
    isGood?: boolean;
}
post-custom-banner

0개의 댓글