I’m having trouble handling MySQL query data in the resolve function of GraphQL objects. The data returns null in GraphiQL despite printing with console.log(). I’ve tried using JSON.parse().
I have the following code:
Schema.js
const graphql = require('graphql');
const queries = require('../mysql/queries');
const {
GraphQLObjectType,
GraphQLString,
GraphQLSchema,
GraphQLID,
GraphQLInt,
GraphQLFloat,
GraphQLList,
GraphQLNonNull
} = graphql;
const AccountType = new GraphQLObjectType({
name: 'Account',
fields: () => ({
id: { type: GraphQLID },
email: { type: GraphQLString },
phone: { type: GraphQLString },
password: { type: GraphQLString }
})
});
const RootQuery = new GraphQLObjectType({
name: 'RootQueryType',
fields: {
account: {
type: AccountType,
args: {
email: { type: GraphQLString },
password: { type: GraphQLString }
},
async resolve(parent, args) {
return await queries.login(args.email, args.password);
}
}
module.exports = new GraphQLSchema({
query: RootQuery
})
queries.js
var connection = require('./connection');
var db = connection.connect();
module.exports = {
login: (email, password) => {
return new Promise((resolve) => {
var account = `SELECT id FROM accounts WHERE email="${email}" AND password="${password}"`;
db.query(account, (err, result) => {
if (err) throw err;
if (isEmpty(result[0])) return resolve(null);
else return resolve(result);
})
})
}
}