提问人:Adrian Cielniak 提问时间:10/26/2023 更新时间:10/26/2023 访问量:27
如何向 apollo 服务器提供来自不同文件的解析器 (nestjs)
How to provide resolvers from different files to the apollo server (nestjs)
问:
我有 nestjs 测验 api,它为 graphql 查询和突变提供服务,我有两个解析器(用于 问题和测验)。对于apollo服务器,我需要从autoSchema获得的模式,但我不知道如何在没有构造函数的情况下提供此解析器
下面我的三个文件(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);
}
}
答: 暂无答案
评论
GraphQlModule
startStandaloneServer
GraphQlModule.forRoot()
app.listen(3000)
/graphql