nest new nest-product-project
npm install --save @nestjs/typeorm typeorm mysql2
npm i @nestjs/graphql @nestjs/apollo graphql apollo-server-express
code first
GraphQLModule.forRoot({
driver: ApolloDriver,
autoSchemaFile: join(process.cwd(), 'src/schema.gql'),
})
mysql 연결이 잘 되고 있지 않는거 같다.
현재 실행중인 서비스를 봤을 때 mysql 은 실행중이다. 그러면 먼가 연결에서 문제가 있는거 같다.
현재 연결할려는 데이터 베이스가 없었다...
생성을 완료 했다. 다시 nest를 실행 시켜 보자.
여전히 연결을 하지 못한다...
=> allowPublicKeyRetieval : true 로 바꾸어주니 연결이 된다.
링크텍스트
여기 내용을 보니 아폴로 서버는 무조건 하나이상의 graphQL 서버가 있어야 한다고 한다. 그러니 무시하고 이제 resolver, service, module을 만들어 보자.
import { Field, ObjectType } from "@nestjs/graphql";
import { Column, Entity, PrimaryGeneratedColumn } from "typeorm";
@Entity()
@ObjectType()
export class ProductCategory {
@PrimaryGeneratedColumn("uuid")
@Field(() => String)
id: string;
@Column()
@Field(() => String)
name: string;
}
@Injectable()
export class ProductCategoryService {
constructor(
@InjectRepository(ProductCategory)
private readonly productCategoryRepository: Repository<ProductCategory>
){}
async create({name}) {
const result = await this.productCategoryRepository.save({name});
return result;
}
}
@Resolver()
export class ProductCategoryResolver {
constructor(private readonly productCategoryService: ProductCategoryService){}
@Mutation()
createProcutCategory(@Args('name') name: string){
return this.productCategoryService.create({name});
}
}
@Module({
imports: [TypeOrmModule.forFeature([ProductCategory])],
providers: [ProductCategoryResolver, ProductCategoryService],
})
export class ProductCategoryModule{}
@Module({
imports: [
TypeOrmModule.forRoot({
type: 'mysql',
host: 'localhost',
port: 3306,
username: 'root',
password: '123123123',
database: 'test',
entities: [__dirname + 'api/**/*.entity.*'],
synchronize: true,
logging: true,
}),
GraphQLModule.forRoot<ApolloDriverConfig>({
driver: ApolloDriver,
autoSchemaFile: join(process.cwd(), 'src/schema.gql'),
}),
ProductCategoryModule,
],
controllers: [],
providers: [],
})
export class AppModule {}
Error: "Mutation.createProcutCategory" was defined in resolvers, but not in schema. If you use the @Mutation() decorator with the code first approach enabled, remember to ex
내용을 보니 createProductCategory는 resolver로 정의 되었는데 schema가 없다는거 같으니 resolver파일을 보자
@Resolver()
export class ProductCategoryResolver {
constructor(private readonly productCategoryService: ProductCategoryService){}
@Mutation(() => ProductCategory)
createProcutCategory(
@Args('name') name: string,
){
return this.productCategoryService.create({name});
}
}
mutation에 리턴값 등록 이 후 다른에러가 뜬다...
GraphQLError: Query root type must be provided.
resolver안에 더미 Query 하나를 추가해 주니 정상 작동 한다...
@Query(() => String)
sayHello(): string {
return 'Hello World!';
}
드디어 연결이 되었다...