Technologies in use:
- NodeJS to run the http server
- ExpressJS framework
- Apollo-server-express for graphQL
- Sequelize to connect to postgres DB
- Postgresql to store data
I am trying to get graphQL working on my node server. Where I am specifically having issues is with the graphql resolver.
I have the following code, which works fine.
let User = require('./server/models').User;
let data = User.find({where: {email: "Test@gmail.com"}})
.then(function (foundUser) {
console.log(foundUser.dataValues);
return foundUser.dataValues;
});
const resolvers = {
Query: {
user(root, args){
return data;
}
}
};
When the client queries the graphql endpoint:
query Query{
user(email:"literallyanything@cangohereATM.com") {
email
}
}
They always get:
{
"data": {
"user": {
"email": "Test@gmail.com"
}
}
}
Which is fine! That is the expected behavior. As is, any parameter can get passed into the query with user(email:"literallyanything@cangohereATM.com") and the results will be the same. Again, that's expected.
When I move the code however, so that the request for data from the database is IN the resolver like so:
const resolvers = {
Query: {
user(root, args){
User.find({where: {email: "Test@gmail.com"}})
.then(function (foundUser) {
console.log(foundUser.dataValues);
return foundUser.dataValues;
});
}
}
};
the client will always get this as a response from the graphql endpoint using the same query as before (the console.log outputs the appropriate information however):
{
"data": {
"user": null
}
}
I suspect I'm failing to understand how apollo-server-express / graphql-tools handles the promise, but I'm not really sure how to move forward from this point. Any help is much appreciated.