GraphQL + Relay:如何执行重新获取授权?
GraphQL + Relay: How can I perform authorization for refetching?
我正在使用 Express 构建 GraphQL 服务器并尝试支持中继。
对于常规的 GraphQL 查询,我可以在 resolve 函数中处理授权。例如:
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)
}
})
});
为了在后端支持 Relay 重新获取,Facebook 的 Relay 教程指导我们让 GraphQL 对象实现一个节点接口,用于将全局 ID 映射到对象,并将对象映射到 GraphQL 类型。 nodeInterface 由 graphql-relay.
中的 nodeDefinitions 函数定义
const {nodeInterface, nodeField} = nodeDefinitions(
(globalId) => {
const {type, id} = fromGlobalId(globalId);
if (type === 'bar') {
// since I don't have access to the request object here, I can't pass the user to getBar, so getBar can't perform authorization
return getBar(id);
} else {
return null;
}
},
(obj) => {
// return the object type
}
);
传递给 nodeDefinitions 的重新获取函数没有传递请求对象,只有全局 id。我如何在重新获取期间访问用户以便我可以授权这些请求?
作为完整性检查,我尝试查询经过身份验证的用户无法通过节点接口访问(也不应该)访问的节点,并取回请求的数据:
{node(id:"id_of_something_unauthorized"){
... on bar {
field_this_user_shouldnt_see
}
}}
=>
{
"data": {
"node": {
"field_this_user_shouldnt_see": "a secret"
}
}
}
事实证明,请求数据确实传递给解析。如果我们查看源代码,我们会看到 nodeDefinitions
抛出 parent
参数并传递全局 id
、context
(包含请求数据)和 info
来自 nodeField
的解析函数的参数。
最终,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
}
);
https://github.com/graphql/graphql-relay-js/blob/master/src/node/node.js#L94-L102
我正在使用 Express 构建 GraphQL 服务器并尝试支持中继。
对于常规的 GraphQL 查询,我可以在 resolve 函数中处理授权。例如:
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)
}
})
});
为了在后端支持 Relay 重新获取,Facebook 的 Relay 教程指导我们让 GraphQL 对象实现一个节点接口,用于将全局 ID 映射到对象,并将对象映射到 GraphQL 类型。 nodeInterface 由 graphql-relay.
中的 nodeDefinitions 函数定义const {nodeInterface, nodeField} = nodeDefinitions(
(globalId) => {
const {type, id} = fromGlobalId(globalId);
if (type === 'bar') {
// since I don't have access to the request object here, I can't pass the user to getBar, so getBar can't perform authorization
return getBar(id);
} else {
return null;
}
},
(obj) => {
// return the object type
}
);
传递给 nodeDefinitions 的重新获取函数没有传递请求对象,只有全局 id。我如何在重新获取期间访问用户以便我可以授权这些请求?
作为完整性检查,我尝试查询经过身份验证的用户无法通过节点接口访问(也不应该)访问的节点,并取回请求的数据:
{node(id:"id_of_something_unauthorized"){
... on bar {
field_this_user_shouldnt_see
}
}}
=>
{
"data": {
"node": {
"field_this_user_shouldnt_see": "a secret"
}
}
}
事实证明,请求数据确实传递给解析。如果我们查看源代码,我们会看到 nodeDefinitions
抛出 parent
参数并传递全局 id
、context
(包含请求数据)和 info
来自 nodeField
的解析函数的参数。
最终,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
}
);
https://github.com/graphql/graphql-relay-js/blob/master/src/node/node.js#L94-L102