0

I would like to have an additional column with a modified value from another column: Now I have next code like this but it doesn't work:

const { Model } = require("objection");  
    class TestModel extends Model {
      static get tableName() {
        return "testTable";
      }
    
    
      static get virtualAttributes() {
        return ["someNewField"];
      }
    
      someNewField() {
        return this.someField+'testString';
      }
    
      static get jsonSchema() {
        return {
          type: "object",
          properties: {
            someField:{ type: "string" },
          },
        };
      }
    }
    
    module.exports = {
      TestModel,
    };

I got error like

"message": "Cannot query field \"someNewField\" on type \"testTable\".",

My grapql query:

{TestModels{
  someNewField,
}
befreeforlife
  • 481
  • 2
  • 7
  • 21

1 Answers1

1

Virtual attributes are created in javascript side after data has been fetched from the database. So there is no such column named someNewField generated to DB.

So now you are trying to create a query that tries to find data from DB that is not there at all.

There is no generic workaround how to make this work. If you can explain your use-case what you are trying to achieve here, I might be able to suggest something to do in that specific case.

Mikael Lepistö
  • 18,909
  • 3
  • 68
  • 70
  • I use objection and objection-graphql. I'm looking for a way that will allow modifying the date that will get the client using graphql queries, I dont understang how change it on the server side. For example I get value like 100, I would like to change it in $100 – befreeforlife Aug 27 '20 at 20:05
  • I don't know if graphql + objection supports that. – Mikael Lepistö Aug 31 '20 at 07:55