1
votes

Technologies in use:

  • Apollo-server-express for graphQL
  • Sequelize to connect to SQL Server

I have inherited a database where fields don't follow any naming convention. Some have spaces and special characters. To avoid any issue, I used an SQL alias for these fields in the resolver queries :

const productAttributes = [
  'SAP', 
  ['Unit/V', 'UnitVol']];

...

  Query: {
    product: async (parent, {SAP} , { models }) => {
      const result = await models.Product.find( {
        where: {
          SAP: SAP
        },
        attributes:productAttributes,
      });
      return result;
    },
...

GraphQL schema is set to be :

type Product {
    SAP: String!
    UnitVol: Int
}

When executing the query from the playground, the field "SAP" coming from the model is well filled up, but the field alias "UnitVol" is set to null (see the image GraphQL result).

Looking in detail, Sequelize well executes the following SQL statement :

Executing (default): SELECT [SAP], [Unit/V] AS [UnitVol] FROM [product codes] WHERE [product codes].[SAP] = N'22736';

The Query result described above returns the values with the alias (extract of console.log(result)):

product codes {
  dataValues:
   { SAP: '22736',
     UnitVol: 9 }
 },

So it seems that there is a missing link between the SQL alias returned by the resolver and the graphQL schema. Any help is much appreciated.

1
_____________________________Ned

1 Answers

1
votes

I found a solution :

Instead of returning the sequelize result, I return the 'datavalues' array in the result:

return !result ? [] : result.dataValues;

I had to control that the result is not null (no entry for the graphQL request). If anyone has a cleaner answer, I'll be happy to test it! Thanks.