Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

{
  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.

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading