GraphQL+Sequelize解析器返回空数据 技术问题求助
Hey there! Let's dig into why your GraphQL resolver is returning empty data even though your Sequelize query works when you run it directly. I’ve seen this issue a few times with Apollo + Sequelize setups, so here are the most likely fixes:
1. You’re not returning the Promise chain to your resolver
GraphQL resolvers need to receive a Promise (or a direct value) to resolve the data correctly. Your current code logs the data, but if you’re not returning the entire Promise chain or the final value, the resolver will resolve to undefined (empty data).
Fix it with .then():
// In your resolver getUser: () => { return User.find({ where: { email: "Test@gmail.com" } }) .then(foundUser => { console.log(foundUser.dataValues); // Return the data to the resolver—this is crucial! return foundUser ? foundUser.dataValues : null; }) .catch(err => { // Don’t swallow errors—throw them so GraphQL can report them console.error("Error fetching user:", err); throw err; }); }
Or use async/await (cleaner and easier to debug):
getUser: async () => { try { const foundUser = await User.find({ where: { email: "Test@gmail.com" } }); console.log(foundUser?.dataValues); // Return the data (or null if no user found) return foundUser ? foundUser.dataValues : null; } catch (err) { console.error("Error fetching user:", err); throw err; } }
2. Check if your GraphQL type matches the Sequelize data
Make sure your GraphQL type definition includes all the fields you’re trying to return, and that they match the keys in foundUser.dataValues. For example:
# Your typeDefs should look like this (match Sequelize model fields!) type User { id: ID! email: String! name: String # If your model has this field } type Query { getUser(email: String!): User }
If your resolver returns fields that aren’t defined in the GraphQL type, they’ll be stripped out—making it look like you’re getting empty data.
3. Verify your Sequelize query is actually finding data
Wait, PostgreSQL is case-sensitive by default! If your database has test@gmail.com (lowercase T) but you’re querying Test@gmail.com, the query will return null. Try adjusting the case or using a case-insensitive query:
// Case-insensitive lookup for PostgreSQL const foundUser = await User.find({ where: { email: { [Op.iLike]: "Test@gmail.com" } } }); // Don’t forget to import Op from Sequelize: const { Op } = require('sequelize');
4. Update Sequelize if you’re using deprecated methods
If you’re on Sequelize v5 or newer, the find() method is deprecated—use findOne() instead. This avoids any unexpected behavior from outdated methods:
const foundUser = await User.findOne({ where: { email: "Test@gmail.com" } });
5. Add debugging to your Apollo Server
Turn on debug mode in Apollo Server to see detailed error messages in your GraphQL playground (or response):
const server = new ApolloServer({ typeDefs, resolvers, debug: true, // Enable this in development });
This will show you if there’s an error being thrown that you’re not catching in your resolver.
Start with checking if you’re returning the Promise correctly—that’s the most common culprit here. If that doesn’t fix it, move on to verifying your query matches the database data and your GraphQL type definitions align with your Sequelize model.
内容的提问来源于stack exchange,提问作者Gordon Clark

