So I'm trying to build a resolver for graphQL which is supposed to return an array of objects. The values for these objects come from a series of TypeORM selecting operations. And when I tried asking for a response in the graphql playground I only got empty arrays, so I started debugging the resolver using console.logs, but the thing is, inside the forEach loops I use the code seems to have the desired result: an array with objects in it. But when I log the same array right before returning it is empty:
@Query(() => [response]) async getTeacherFromSubjectName( @Arg("subjectName") subjectName: string, ): Promise<response[] | undefined> { const subject = await Subject.findOne({name: subjectName}); const subjectId = subject?.id; let responseArray: response[] = []; const qb = await getConnection() .createQueryBuilder() .select("teacher") .from(Teacher, "teacher") .where(`teacher.subjectId = ${subjectId}`) .getMany() qb.forEach(async (teacher) => { const qb = await getConnection() .createQueryBuilder() .select("lectureTime") .from(LectureTime, "lectureTime") .where(`lectureTime.teacherId = ${teacher.id}`) .getMany() responseArray.push( { teacher: teacher.name, lectures: qb, } ); console.log(responseArray) // [{ teacher: 'Dirceu', lectures: [ [LectureTime] ] }, { teacher: 'Patrícia', lectures: [ [LectureTime], [LectureTime] ] } ] }) console.log(responseArray) // [] return responseArray; } What I get on the console is the following: link to image
I actually have no idea of what is going on here, you can see in the image that the order of the logs is inverted (log right before return is circled in blue).
I am certain that it is a silly problem and if you guys could point it out for me I would be very thankful.
https://stackoverflow.com/questions/66511595/graphql-response-array-getting-empty-out-of-nowhere-right-before-return March 07, 2021 at 07:05AM
没有评论:
发表评论