Vincit / objection-graphql

GraphQL schema generator for objection.js
MIT License
307 stars 35 forks source link

How to implement virtual Attributes? #91

Open vitalizis opened 4 years ago

vitalizis commented 4 years ago

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\".",

CassandraNewell commented 3 years ago

I have a similar example working, but I had to add someNewField to the properties list in jsonSchema, and my someNewField() is a getter (not sure if the second part is important or just the first).