graphql setup

This commit is contained in:
João Geonizeli
2022-08-15 07:08:51 -03:00
parent 0e27984e8e
commit 458d9a1376
10 changed files with 585 additions and 64 deletions

View File

@@ -1,22 +0,0 @@
import { Test, TestingModule } from '@nestjs/testing';
import { AppController } from './app.controller';
import { AppService } from './app.service';
describe('AppController', () => {
let appController: AppController;
beforeEach(async () => {
const app: TestingModule = await Test.createTestingModule({
controllers: [AppController],
providers: [AppService],
}).compile();
appController = app.get<AppController>(AppController);
});
describe('root', () => {
it('should return "Hello World!"', () => {
expect(appController.getHello()).toBe('Hello World!');
});
});
});

View File

@@ -1,12 +0,0 @@
import { Controller, Get } from '@nestjs/common';
import { AppService } from './app.service';
@Controller()
export class AppController {
constructor(private readonly appService: AppService) {}
@Get()
getHello(): string {
return this.appService.getHello();
}
}

View File

@@ -1,10 +1,17 @@
import { ApolloDriver, ApolloDriverConfig } from '@nestjs/apollo';
import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { GraphQLModule } from '@nestjs/graphql';
import { join } from 'path';
import { ThingResolver } from './thing/thing.resolver';
@Module({
imports: [],
controllers: [AppController],
providers: [AppService],
imports: [
GraphQLModule.forRoot<ApolloDriverConfig>({
driver: ApolloDriver,
autoSchemaFile: join(process.cwd(), 'src/schema.gql'),
debug: true,
}),
],
providers: [ThingResolver],
})
export class AppModule {}

View File

@@ -1,8 +0,0 @@
import { Injectable } from '@nestjs/common';
@Injectable()
export class AppService {
getHello(): string {
return 'Hello World!';
}
}

11
src/schema.gql Normal file
View File

@@ -0,0 +1,11 @@
# ------------------------------------------------------
# THIS FILE WAS AUTOMATICALLY GENERATED (DO NOT MODIFY)
# ------------------------------------------------------
type Thing {
id: Int!
}
type Query {
things: [Thing!]!
}

7
src/thing/thing.model.ts Normal file
View File

@@ -0,0 +1,7 @@
import { Field, Int, ObjectType } from '@nestjs/graphql';
@ObjectType()
export class Thing {
@Field(type => Int)
id: number;
}

View File

@@ -0,0 +1,18 @@
import { Test, TestingModule } from '@nestjs/testing';
import { ThingResolver } from './thing.resolver';
describe('ThingResolver', () => {
let resolver: ThingResolver;
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [ThingResolver],
}).compile();
resolver = module.get<ThingResolver>(ThingResolver);
});
it('should be defined', () => {
expect(resolver).toBeDefined();
});
});

View File

@@ -0,0 +1,19 @@
import { Query, Resolver } from '@nestjs/graphql';
import { Thing } from './thing.model';
@Resolver(() => Thing)
export class ThingResolver {
constructor() {}
@Query(() => [Thing])
async things(): Promise<Thing[]> {
return [
{
id: 1,
},
{
id: 2,
},
];
}
}