简体   繁体   中英

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.

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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM