【发布时间】:2018-04-10 08:55:26
【问题描述】:
我正在尝试学习 graphql,以便可以将其应用到 React 网站。不过,我现在遇到了一些麻烦,并且无法弄清楚为什么当我 100% 确定返回的内容不为 null 时,它会说返回 null。如果我在解析器中打印results,它会按预期打印一个用户对象数组,但是当它返回时,它说我正在为字段Query.users 返回null。任何帮助将不胜感激。
Query: { // graphql query resolver
users: function (parent, args, { User }) {
var mongoose = require('mongoose');
var array = [];
mongoose.connect('localhost:27017', function(err){
if(err) throw err;
User.find({}).then(results=>{
console.log(results);
return results;
});
});
}
}
type Query { //query typedef
users: [User]!
}
type User { // graphql schema typedef
_id: String!
username: String!,
email: String!,
joined: String,
interests: [String],
friends: [User]
}
var User = new Schema({ // mongoose schema def
username: !String,
email: !String,
joined: String,
interests: [String],
friends: [[this]]
});
【问题讨论】:
标签: node.js express graphql apollo