Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

147
Vistas
GraphQL can't receive mysql data

I am trying to use GraphQL with mysql in a node js express server.

but I am getting this error everytime I run my query

here is the error:

{
  "errors": [
    {
      "message": "Expected Iterable, but did not find one for field \"RootQueryType.getAllGoals\".",
      "locations": [
        {
          "line": 2,
          "column": 3
        }
      ],
      "path": [
        "getAllGoals"
      ]
    }
  ],
  "data": {
    "getAllGoals": null
  }
}

here is my Graphql Query:

query {
  getAllGoals {
    title
    progress
    goal
  }
}

I get the result expected from "SELECT * FROM (my table)", but it gives me an error when I try to return it for GraphQL from the resolver as shown in the code below:

const RootQuery = new GraphQLObjectType({
    name: "RootQueryType",
    fields: {
        getAllGoals: {
            type: new GraphQLList(GoalType),
            resolve(parent, args) {
                return db.query("SELECT * FROM myTable", (err, result) => {
                    if (err) throw err

                    console.log(JSON.parse(JSON.stringify(result)))
                    return JSON.parse(JSON.stringify(result))
                })
            }
        }
    }
})

I have checked to see if I have any conflicts in my GraphQLObjectType GoalType, but I had none.

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

I have fixed it, I just needed to make a promise that included the query (as shown below):

async resolve(parent, args) {
                var p = new Promise((resolve, reject) => {
                    db.query("SELECT * FROM myTable", (err, result) => {
                        console.log(JSON.parse(JSON.stringify(result)))
                        resolve(JSON.parse(JSON.stringify(result)))
                    })
                })
                return p
            }
about 4 years ago · Juan Pablo Isaza Denunciar

0

Your resolve method returns whatever db.query returns, but this is not what GraphQL expects: It expects the result of the query, or a promise that resolves to this result. You can use util.promisify to obtain such a promise.

If you also want to log the result, use a .then method as shown here:

resolve(parent, args) {
  return util.promisify(db.query)("SELECT * FROM myTable")
  .then(result => {
    console.log(result);
    return result;
  });
}

(What's the purpose of JSON.parse(JSON.stringify(...))?)

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda