How to provide resolvers from different files to the apollo server (nestjs)

How to provide resolvers from different files to the apollo server (nestjs)


0

I have nestjs quiz api which serves graphql queries and mutations, i have two resolvers (for
questions and quizes). To the apollo server i need schema which i have from autoSchema but i dont know how to provide this resolvers without constructors

Below my three files (main.ts / quiz.resolver.ts / question.resolver.ts)

import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { ValidationPipe } from '@nestjs/common';
import { ApolloServer } from '@apollo/server';
import { startStandaloneServer } from '@apollo/server/standalone';
import { readFileSync } from 'fs';
import { QuestionResolver } from './question/question.resolver';
import { QuizResolver } from './quiz/quiz.resolver';


async function bootstrap() {
 const app = await NestFactory.create(AppModule);
 app.useGlobalPipes(new  ValidationPipe());

 const typeDefs = readFileSync('./schema.gql', { encoding: 'utf-8' });;

 const resolvers = {
   QuestionResolver,
   QuizResolver
 };

 const server = new ApolloServer({
   typeDefs, 
   resolvers,
 });

 const { url } = await startStandaloneServer(server, {
   listen: { port: 3000 },
 });

 console.log(🚀  Server ready at ${url});

}
bootstrap();
import { Resolver, Query, Mutation, Args, Int } from '@nestjs/graphql';
import { QuestionService } from './question.service';
import { Question } from './entities/question.entity';

@Resolver(() => Question)
export class QuestionResolver {
    constructor(private readonly questionService: QuestionService) {}

    @Query(() => [Question], { name: 'getAllQuestions'})
        async getAllQuestions() {
        return this.questionService.getAllQuestions();
    }

    @Query(() => [Question], { name: "findQuizQuestions" })
        async findQuizQuestions(@Args('quiz_id', {type: () => Int}) quiz_id: number) {
        return this.questionService.findQuizQuestions(quiz_id);
    }

    @Query(() => [Question], {name: "findFullQuestions"})
        async findFullQuestions(@Args('quiz_id', {type: () => Int}) quiz_id: number){
        return this.questionService.findFullQuestions(quiz_id);
    }
}
import { Resolver, Query, Mutation, Args, Int } from '@nestjs/graphql';
import { QuizService } from './quiz.service';
import { Quiz, QuizResult } from './entities/quiz.entity';
import { CreateQuizInput } from './dto/create-quiz.input';
import { Question } from 'src/question/entities/question.entity';
import { checkQuestionInput } from 'src/question/dto/create-question.input';

@Resolver(() => Quiz)
export class QuizResolver {
    constructor(
        private readonly quizService: QuizService
    ) {}

    @Mutation(() => Quiz)
    createQuiz(@Args('createQuizInput') createQuizInput: CreateQuizInput) {
        return this.quizService.createQuiz(createQuizInput)
    }

    @Query(() => [Quiz], { name: 'findAllQuizzes' })
    async findAllQuizzes() {
        return this.quizService.findAllQuizzes();
    }

    @Query(() => Quiz, { name: 'findQuiz' })
    async findQuiz(@Args('quiz_id', { type: () => Int }) quiz_id: number) {
        return this.quizService.findQuiz(quiz_id);
    }

    @Query(() => QuizResult)
    async checkAnswers(
        @Args('quiz_id', { type: () => Int }) quiz_id: number,
        @Args('student_answers', { type: () => [checkQuestionInput] }) student_answers: checkQuestionInput[]
    ): Promise<QuizResult> {
        return this.quizService.checkAnswers(quiz_id, student_answers);
    }
}


Load 3 more related questions


Show fewer related questions

0



Leave a Reply

Your email address will not be published. Required fields are marked *