0
votes

I've got an apollo schema like this one:

  type Batch {
    Batch: String,
    ItemCode: String,
    PV: String,
    ExpirationDate: DateTime,
    Freezed: Boolean,
    Item:Item,
    Quantity: Float, 
    Fefo: Boolean
  }

and a query in sequelize like that:

await models.Batch.findAll({
          attributes:  ['ItemCode','Lotto','ExpirationDate','Freezed', [Sequelize.literal(`CASE WHEN "Batch" ='${Batch}' THEN true ELSE false END`), "Fefo"] ],
          where: whereClause,      
          include: [models.Item],
          order: [
            // Will escape title and validate DESC against a list of valid direction parameters
            ['ExpirationDate'] 
          ]
          // attributes: models.mapAttribute(models.Batches, info)
        })  

returning an array of Batch objects.

All runs without problems, graphql query works well:

{
    getFefoBatches (ItemCode:"200200201",Batch:"20201118B")
    {
        Batch, ExpirationDate, Fefo, Item { ItemCode ItemName }
    }
}

but the "Fefo" field is not valorized with the "sequelize.literal" value. It's always null.

Maybe I don't known the proper syntax, but I tried everything possible, and also I searched for a solution but without success.

How is it possible to make a correct mapping between sequelize field and Apollo server schema?

Thanks in advance.

1
try to look at generated SQL queryAnatoly
The query is correct, and data returned are okay. The only difference is that the table field are named as "Batch"."nameofthefield" while the "case when" generated file is named "Fefo" without the table prefix.Hammeronthenet
I tried something like attributes: ['id', [sequelize.literal('case when total_amount > 0 then 1 else 0 end'), 'test_alias']] and it worked fine.Anatoly

1 Answers

0
votes

Okay, I discovered that the problem is not on sequelize query or on Apollo gql schema but is related to the model.

I added a virtual field on model with the same name of my query/schema field and everything works fine.