【发布时间】:2021-12-31 11:16:42
【问题描述】:
我一直在对源代码进行一些更改,但无法让它们显示在 Apollo Explorer Sandbox 中。例如,我在源代码中的一个解析器中添加了一些突变和一个额外的查询,但它们没有出现在 Apollo Sandbox 中。有什么想法吗?
import { Arg, Ctx, Int, Mutation, Query, Resolver } from "type-graphql";
import { Post } from "src/entities/Post";
import { MyContext } from "src/types";
import { idText } from "typescript";
@Resolver()
export class PostResolver {
@Query(() => [Post])
posts(@Ctx() { em }: MyContext): Promise<Post[]> {
return em.find(Post, {});
}
@Query(() => Post, { nullable: true })
post(@Arg("id", () => Int) id: number, @Ctx() { em }: MyContext): Promise<Post | null> {
return em.findOne(Post, { id });
}
@Mutation(() => Post)
async createPost(
@Arg("title") title: string,
@Ctx() { em }: MyContext
): Promise<Post> {
const post = em.create(Post, {title});
await em.persistAndFlush(post);
return post;
}
@Mutation(() => Post, {nullable: true})
async updatePost(
@Arg("id") id: number,
@Arg("title", () => String, { nullable: true }) title: string,
@Ctx() { em }: MyContext
): Promise<Post | null> {
const post = await em.findOne(Post, { id });
if (!post) {
return null;
}
if (typeof title !== 'undefined') {
post.title = title;
await em.persistAndFlush(post);
}
return post;
}
}
【问题讨论】:
标签: node.js typescript graphql apollo-server