I can't fetch array or object with mongoose to use it in Relay connection, actually this works fine:
let queryType = new GraphQLObjectType({
name: 'Query',
fields: () => ({
getUsers: {
type: new GraphQLList(userType),
resolve: (_, args) => {
return new Promise((resolve, reject) => {
Users.find(args, (err, users) => {
err ? reject(err) : resolve(users);
});
});
}
}
})
});
in GraphiQL i can get array. But when I try use the same "resolve" function with Relay it doesn't work!
getUsers: {
node: nodeField,
type: usersType,
resolve: (_, args) => {
return new Promise((resolve, reject) => {
Users.find(args, (err, users) => {
err ? reject(err) : resolve(users);
});
});
}
}
and I think reason is because Users.find() return Promise instead of Array or Obj. Same function if I just change "Users.find()" to any other function with return array works fine.
So main question how can I get array or object with "Users.find()" instead of Promise? (,lean(), .exec()) doesn't help too.
npm i graphql-relay-connection --save
const {connectionFromPromisedArray} = mongooseConnection;
and instead of connectionFromArray(get(), args)
we can pass Promise: connectionFromPromisedArray(collection.find(), args)
Finally you can fetch array from Promise without tons of code.