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

225
Vistas
GraphQL query return NULL in GraphiQL, but data appears in console log

I have a GraphQL API which is supposed to return data from MySQL and PostGres database. In the resolvers, I have console.log the results and can view the data in the terminal.

address: {
      type: AddressType,
      description: "An Address",
      args: {
        id: { type: GraphQLInt },
      },
      resolve: (parent, args) => {
        // Make a connection to MySQL
        let result;
        connection.query(
          `SELECT * FROM addresses WHERE id = ${args.id}`,
          (err, res, fields) => {
            if (err) console.log(err);
            console.log("========");
            console.log(res);
            console.log("+++++++++");
            console.log(res[0]);

            // console.log(result);
          }
        );
        return result;
      },
    },

In the terminal, I can see the results when I run the query on GraphiQL:

[nodemon] starting `node schema.js`
Server is running
Connected to PSQL database.
Connected to mySQL database.
========
[
  RowDataPacket {
    id: 1,
    address_type: 'House',
    status: 'Inactive',
    entity: 'Building',
    number_and_street: 'PO BOX 276',
    suite_and_apartment: 'PO',
    city: 'Ennis',
    postal_code: '59729-0276',
    country: 'USA',
    notes: 'Dolorem quia repellendus et et nobis.',
    created_at: 2020-12-18T05:00:00.000Z,
    updated_at: 2021-05-21T04:00:00.000Z,
    latitude: null,
    longitude: null
  }
]
+++++++++
RowDataPacket {
  id: 1,
  address_type: 'House',
  status: 'Inactive',
  entity: 'Building',
  number_and_street: 'PO BOX 276',
  suite_and_apartment: 'PO',
  city: 'Ennis',
  postal_code: '59729-0276',
  country: 'USA',
  notes: 'Dolorem quia repellendus et et nobis.',
  created_at: 2020-12-18T05:00:00.000Z,
  updated_at: 2021-05-21T04:00:00.000Z,
  latitude: null,
  longitude: null
}

However on GraphiQL, I get null for data. Input:

{
  address(id: 1) {
    address_type
  }
  }

output:

{
  "data": {
    "address": null
  }
}

I am very new to GraphQL. What could I be missing here? I am attempting to get this information from the terminal to show upon query in GraphiQL. Just trying to learn more.

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

0

The classic problem with inattention: You use the res variable for the console. And nowhere are you assigning a value to result.

And the return result is executed before the query is executed. (out of context where you have data)

See the documentation for how to use the async / await syntax. You are currently using callbacks - which is not a recommended syntax.

about 4 years ago · Juan Pablo Isaza Denunciar

0

Not sure, but should be something like, you should use async/await, and await the return of query data. Also be sure to assign the value to the variable you have:

address: {
  type: AddressType,
  description: "An Address",
  args: {
    id: { type: GraphQLInt },
  },
  resolve: async (parent, args) => {
    const result = await connection.query('SELECT * FROM addresses WHERE id = $1', [args.id]);
    
    return result;
  },
},
about 4 years ago · Juan Pablo Isaza Denunciar

0

What ended up working for me was the following:

address: {
      type: AddressType,
      description: "An Address",
      args: {
        id: { type: GraphQLInt },
      },
      resolve: async (parent, args) => {
        const [rows, fields] = await promisePool.query(
          `SELECT * FROM addresses WHERE id = ${args.id}`
        );
        console.log(rows[0]);
        return rows[0];
      },
    },
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