【发布时间】:2019-03-01 01:33:09
【问题描述】:
我在模式拼接中使用 apollo 链接作为访问控制层。如果用户无权访问特定操作,我不太确定如何使链接返回错误响应。我知道graphql-shield 和graphql-middleware 之类的软件包,但我很好奇,使用 apollo 链接可以实现基本的访问控制。
这是我的链接的样子:
const link = setContext((request, previousContext) => merge({
headers: {
...headers,
context: `${JSON.stringify(previousContext.graphqlContext ? _.omit(previousContext.graphqlContext, ['logger', 'models']) : {})}`,
},
})).concat(middlewareLink).concat(new HttpLink({ uri, fetch }));
middlewareLink 具有 checkPermissions,根据用户的角色返回 true 或 false
const middlewareLink = new ApolloLink((operation, forward) => {
const { operationName } = operation;
if (operationName !== 'IntrospectionQuery') {
const { variables } = operation;
const context = operation.getContext().graphqlContext;
const hasAccess = checkPermissions({ operationName, context, variables });
if (!hasAccess) {
// ...
}
}
return forward(operation);
});
如果hasAccess 是false,我该怎么办。我想我不需要转发操作,因为此时很明显用户无权访问它
更新
我想我需要做的是扩展ApolloLink类,但到目前为止我还没有设法返回错误
【问题讨论】:
标签: apollo apollo-link