【发布时间】:2020-08-24 09:27:59
【问题描述】:
我正在使用 Apollo GraphQL 服务器和指令。
这是我的简单架构。注意令牌字段上的指令,用户类型。
const typeDefs = `
directive @allow(service: String) on FIELD_DEFINITION
type User {
email: String!
pass: String!
... other fields here
token: String @allow(service: "login")
}
type Mutation {
login(email: String!, pass: String!): User
}`;
我只想在调用登录时返回令牌字段。否则,我想返回用户对象没有令牌字段,我能找到的只是抛出异常或在“令牌”字段中返回 null。
class SkipDirective extends SchemaDirectiveVisitor {
visitFieldDefinition(field, details) {
const { resolve = defaultFieldResolver } = field;
field.resolve = async function (...args) {
// If called in context different from "login"
// Here I would like to just "delete" the "token" field
else {
const result = await resolve.apply(this, args);
return result;
}
};
}
}
想法?
【问题讨论】:
标签: graphql apollo-server