2

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.

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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;
  },
},

Comments

-1

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];
      },
    },

1 Comment

Stop putting args directly in raw query!!! this cause SQL-Iniection. Right way was shown by Kamko.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.