如何在另一个对象的解析器中查询对象?

时间:2019-03-04 13:25:19

标签: node.js graphql apollo-server

在Apollo Server中,我可以作为一种ORM来访问/浏览图形吗?

看这里:

@ObjectType()
User {
    @Field()
    public id!: ID;
    @Field()
    public email!: string;
    /* this is for FieldResolver to return actual Role objects */
    rolesIds: number[]; 
}

@ObjectType()
Role {
    @Field()
    public id!: ID;
    @Field()
    public label!: String;
}

@Resolver(type => User)
UserResolver {
    @Query()
    public getUser(userId: number) {
        return new User();
    }

    @FieldResolver(() => [Role])
    public roles(@Root() root: User) {

    }
}

@ObjectType()
Project { /**/ }

@Resolver(type => Project)
ProjectResolver {
    @Query()
    public updateProject(@Ctx() auth, input: ProjectUpdateInput) {
        const userProxy = someApolloExecutorService.query('getUser(userId: 132)');
        const roles = userProxy.roles();
        console.log(roles); // logs Role objects resolved via `roles()` function from UserResolver graph object.

        const adminRole = roles.find(label === 'ADMIN');
        if (!adminRole) {
            throw new Error('Sorry, you\'re not allowed to edit this project');
        }
        // ...
    }
}

我浏览了没有运气好的服务的文档,这些服务可以像上面虚构的片段中的someApolloExecutorService一样使用。

有什么可以这样工作的吗?

0 个答案:

没有答案