Express 图形ql+;中继:如何执行重新蚀刻的授权?
我正在使用Express构建GraphQL服务器,并试图支持中继 对于常规GraphQL查询,我可以在resolve函数中处理授权。例如:Express 图形ql+;中继:如何执行重新蚀刻的授权?,express,graphql,relay,Express,Graphql,Relay,我正在使用Express构建GraphQL服务器,并试图支持中继 对于常规GraphQL查询,我可以在resolve函数中处理授权。例如: var queryType = new GraphQLObjectType({ name: 'RootQueryType', fields: () => ({ foo: { type: new GraphQLList(bar), description: 'I should
var queryType = new GraphQLObjectType({
name: 'RootQueryType',
fields: () => ({
foo: {
type: new GraphQLList(bar),
description: 'I should have access to some but not all instances of bar',
resolve: (root, args, request) => getBarsIHaveAccessTo(request.user)
}
})
});
为了在后端支持中继重蚀刻,Facebook的中继教程指导我们让GraphQL对象实现一个节点接口,用于将全局ID映射到对象,并将对象映射到GraphQL类型。nodeInterface由中的nodeDefinitions函数定义
传递给nodeDefinitions的重新蚀刻函数不会传递请求对象,而只传递全局id。如何在重新蚀刻期间访问用户,以便授权这些请求
作为一项健全性检查,我尝试通过节点接口查询经过身份验证的用户无权(也不应该)访问的节点,并获得了请求的数据:
{node(id:"id_of_something_unauthorized"){
... on bar {
field_this_user_shouldnt_see
}
}}
=>
事实证明,请求数据实际上是传递给解析的。如果我们查看源代码,就会看到
nodedeDefinitions
抛出parent
参数,并从nodeField
的resolve函数传递全局id
、context
(包含请求数据)和info
参数
最终,resolve
调用将获得以下参数:
(parent, args, context, info)
而idFetcher
将获得:
(id, context, info)
因此,我们可以按如下方式实施授权:
const {nodeInterface, nodeField} = nodeDefinitions(
(globalId, context) => {
const {type, id} = fromGlobalId(globalId);
if (type === 'bar') {
// get Bar with id==id if context.user has access
return getBar(context.user, id);
} else {
return null;
}
},
(obj) => {
// return the object type
}
);
(id, context, info)
const {nodeInterface, nodeField} = nodeDefinitions(
(globalId, context) => {
const {type, id} = fromGlobalId(globalId);
if (type === 'bar') {
// get Bar with id==id if context.user has access
return getBar(context.user, id);
} else {
return null;
}
},
(obj) => {
// return the object type
}
);