Я использую директивы graphql для авторизации пользователя для запросов и мутаций. В настоящее время я беру заголовок 'authorization' и затем внедряю объект пользователя в root.
Object.keys(fields).forEach(fieldName => {
const field = fields[fieldName];
const { resolve = defaultFieldResolver } = field;
field.resolve = async function (...args) {
const requiredRole =
field._requiredAuthRole || objectType._requiredAuthRole;
if (!requiredRole) {
return resolve.apply(this, args);
}
const requestData = args[2];
const strategyResult = await ruleStrategy(requestData, requiredRole);
requestData.fruit = 'apple';
console.log(args); // this prints all the objects injected into args with no problem
if (!strategyResult) {
throw new Error("not authorized");
}
return resolve.apply(this, args);
}.bind(this);
Однако, когда я пытаюсь получить внедренные объекты в резольвере, root
равен undefined
test: async (root, args, context) => {
console.log(root); // this doesn't show the injected objects. :(
return 'test';
},
Куда делась ошибка?