【发布时间】:2019-09-16 20:25:10
【问题描述】:
我正在为我的 graphql 设置测试。这样做时,我意识到有时错误情况下的数据对象会像这样输出:
{
errors: [...],
data: null
}
有时:
{
errors: [...],
data: {
updateCity: null
}
}
那些测试是关于突变的。这是两个代码的示例:
解析器案例1:
updateUser(parent, args, context, info) {
logger.debug('Mutation > updateUser resolver');
return userController.user.update.user(parent, args.userInfo, context, info);
},
模式案例1:
extend type Mutation {
updateUser(userInfo: UserInfo!): User!
}
控制器案例1:
user: async (parent, args, context, info) => {
try {
logger.debug('User Controller : update User');
await controller.validate(args);
const userModel = new UserModel('member');
if (!(await userModel.findOne(args.id))) errorscb.userInputError('User does not exist');
let id = args.id;
let newArgs = args;
delete newArgs.id;
return userModel.updateById(id, newArgs);
} catch (e) {
logger.warn({ stack: e.stack, message: e.message });
throw e;
}
},
模式案例2:
extend type Mutation {
updateCity(id: Int!, name: String, countryId: Int): City
}
解析器案例2:
updateCity(obj, args, context, info) {
logger.info('City > updateCity resolver');
return cityController.city.update.city(obj, args, context, info);
},
控制器案例 2:
city: async (parent, args, context, info) => {
try {
logger.info('City Controller : update city');
await controller.validate(args);
const cityModel = new CityModel('city');
if (!(await cityModel.findOne(args.id)))
errorscb.userInputError('City does not exist');
let id = args.id;
let newArgs = args;
delete newArgs.id;
return cityModel.updateById(id, newArgs);
} catch (e) {
logger.warn({ stack: e.stack, message: e.message });
throw e;
}
我想获得一致的输出,有人知道如何解决吗?
【问题讨论】:
标签: node.js graphql graphql-js apollo-server